#include <iostream>
#include <vector>
#include <map>
using namespace std;
int mat[4100][4100];
int pr1ev[4100][4100];
vector<pair<int,int> > paths[1100];

int main()
{
    freopen("twopaths.in","r",stdin);
    freopen("twopaths.out","w",stdout);
    int n;
    int r;
    cin >> n >> r;
    for (int k=1;k<=n;k++)
    {
        for (int i=1;i<=n;i++)
        {mat[i][k]=20000000;}}
    for (int i=2;i<=n;i++)
    {
        int s;
        cin >> s;
        mat[s][i]=1;mat[i][s]=1;
    }

    for (int k=1;k<=n;k++)
    {
        for (int i=1;i<=n;i++)
        {
            for (int j=1;j<=n;j++)
            {
                if (i!=j&&(mat[i][k]+mat[k][j]<mat[i][j]))
                {
                    mat[i][j]=mat[i][k]+mat[k][j];
                    pr1ev[i][j]=k;
                    pr1ev[j][i]=k;
                }
            }
        }
    }

    for (int i=0;i<r;i++)
    {
        int a,b;
        cin >> a >> b;
        if (a>b){swap(a,b);}
        if (mat[a][b]==1){paths[i].push_back(make_pair(a,b));continue;}
        if (mat[pr1ev[a][b]][b]==1){swap(a,b);}
        //prev[a][b] рядом с a
        int y=a;
        while(mat[b][y]!=1)
        {
            int hj=min(y,pr1ev[y][b]);
            int jh=max(y,pr1ev[y][b]);
            paths[i].push_back(make_pair(hj,jh));
           // cout << i << ' ' << hj << ' ' << jh << endl;
            y=pr1ev[y][b];
        }
        paths[i].push_back(make_pair(min(y,b),max(y,b)));
        //cout << i << ' ' << y << ' ' << b << endl;
    }
    int m=0;
    int i1=0;
    int i2=1;
    for (int i=0;i<r;i++)
    {
        for (int j=i+1;j<r;j++)
        {

            int kol=0;
            for (int z=0;z<paths[i].size();z++)
            {
                for (int x=0;x<paths[j].size();x++)
                {
                    if (paths[i][z]==paths[j][x]){  kol++;}
                }
            }
            //cout << i << ' ' << j <<' ' <<  kol << endl;
            if (kol>m){m=kol;i1=i;i2=j;}
        }
    }
    cout << m << endl << i1+1 << ' ' << i2+1 << endl;
    return 0;
}
