#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
vector<int> mat[200100];
int ans[200100];
//lca[200100][20];
int k1[200100];
int k2[200100];
 int tim2;
 int pre[200100];
 int post[200100];

void dfs(int z,int p)
{
    pre[p]=tim2;
    tim2++;
    if (z==-2)
    {
       //lca[p][0]=z;
        int j=0;
        //while (lca[z][j]!=0){lca[p][j+1]=lca[z][j];j++;}
    }
    for (int i=0;i<mat[p].size();i++)
    {
        dfs(p,mat[p][i]);
    }
    post[p]=tim2;
    tim2++;
}
// a predok b
bool inner(int a,int b)
{
    return (pre[a]<=pre[b]&&post[a]>=post[b]);
}
int main()
{
    freopen("twopaths.in","r",stdin);
    freopen("twopaths.out","w",stdout);
    int n;
    int k;
    cin >> n >> k;
    for (int i=2;i<=n;i++)
    {
        int s;
        cin >> s;
        mat[s].push_back(i);
        ans[i]=s;
    }

    tim2=1;
    dfs(-1,1);
    for (int i=1;i<=k;i++)
    {
        int a,b;
        cin >> a >> b;

        if (inner(a,b))
        {
            k1[i]=a;
            k2[i]=b;
        }
        else
        {
            k1[i]=b;
            k2[i]=a;
        }
    }
    int m=0;
    int i1,j1;
    i1=1;
    j1=2;
    for (int i=1;i<=k;i++)
    {
        for (int j=i+1;j<=k;j++)
        {
            int kol=0;
            if (inner(k1[i],k1[j])&&inner(k1[j],k2[i]))
            {
                vector<int> p1;
                vector<int> p2;
                int y=k2[i];
                while(y!=k1[j])
                {

                    p1.push_back(y);
                    y=ans[y];
                }
                y=k2[j];
                while(y!=k1[j])
                {
                    p2.push_back(y);
                    y=ans[y];
                }
                int ju=p1.size()-1;
                int ku=p2.size()-1;
                while(ju>=0&&ku>=0&&p1[ju]==p2[ku]){ju--;ku--;kol++;}
                if (kol>m){m=kol;i1=i;j1=j;}
              //  cout << kol << endl;
                continue;
            }
            swap(i,j);
            if (inner(k1[i],k1[j])&&inner(k1[j],k2[i]))
            {
                vector<int> p1;
                vector<int> p2;
                int y=k2[i];
                while(y!=k1[j])
                {

                    p1.push_back(y);
                    y=ans[y];
                }
                y=k2[j];
                while(y!=k1[j])
                {
                    p2.push_back(y);
                    y=ans[y];
                }
                int ju=p1.size()-1;
                int ku=p2.size()-1;
                while(ju>=0&&ku>=0&&p1[ju]==p2[ku]){ju--;ku--;kol++;}
                if (kol>m){m=kol;i1=i;j1=j;}
               // cout << kol << endl;
            }
            swap(i,j);
        }
    }
    cout << m << endl << i1 << ' ' << j1 << endl;
    return 0;
}
