#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

int totalWorkers, totalCouriers;
int logtw;
vector< vector<int> > graph;
int** parents;
int** distances;
int* lca_tin;
int* lca_tout;
int lca_timer;

void lca_dfs(int v) {

    lca_tin[v] = lca_timer++;
    for(int i=0; i<graph[v].size(); ++i)
        lca_dfs(graph[v][i]);
    lca_tout[v] = lca_timer++;

}

char isParent(int a, int b) {

    return (lca_tin[a] <= lca_tin[b]) && (lca_tout[a] >= lca_tout[b]);

}

int lca(int a, int b) {

    if(a == b)
        return a;

    if(lca_tin[a] > lca_tin[b])
        swap(a, b);

    if(lca_tout[a] > lca_tout[b])
        return a;


    for(int k=logtw; k>=0; --k) {

        if(!isParent(parents[a][k], b))
            a = parents[a][k];

    }

    return parents[a][0];

}

int cntDist(int p, int a) {

    if(p == a)
        return 0;

    if(p == parents[a][logtw])
        return distances[a][logtw];

    int ans = 0;

    for(int k=logtw; k>=0; --k) {

        if(parents[a][k] == p)
            return ans + distances[a][k];

        if(lca_tin[parents[a][k]] > lca_tin[p]) {

            ans += distances[a][k];
            a = parents[a][k];

        }

    }

    return ans + distances[a][0];

}

int main() {

    freopen("twopaths.in", "r", stdin);
    freopen("twopaths.out", "w", stdout);

    cin >> totalWorkers >> totalCouriers;
    parents = new int*[totalWorkers];
    distances = new int*[totalWorkers];
    graph.resize(totalWorkers);
    logtw = 1;
    while((1<<logtw) < totalWorkers)
        ++logtw;
    for(int i=0; i<totalWorkers; ++i) {

        parents[i] = new int[logtw+1];
        distances[i] = new int[logtw+1];

    }

    parents[0][0] = 0;
    distances[0][0] = 0;
    for(int i=1; i<totalWorkers; ++i) {

        cin >> parents[i][0];
        --parents[i][0];
        distances[i][0] = 1;
        graph[parents[i][0]].push_back(i);

    }

    for(int k=1; k<=logtw; ++k)
        for(int i=0; i<totalWorkers; ++i) {
            parents[i][k] = parents[parents[i][k-1]][k-1];
            distances[i][k] = distances[i][k-1] + distances[parents[i][k-1]][k-1];
        }

    lca_tin = new int[totalWorkers];
    lca_tout = new int[totalWorkers];
    lca_timer = 0;

    lca_dfs(0);

    pair<int,int>* couriers = new pair<int,int>[totalCouriers];
    int* crlca = new int[totalCouriers];

    for(int i=0; i<totalCouriers; ++i) {

        cin >> couriers[i].first >> couriers[i].second;
        --couriers[i].first;
        --couriers[i].second;
        crlca[i] = lca(couriers[i].first, couriers[i].second);

    }

    int answer = 0;
    pair<int,int> cous;
    cous.first = 0;
    cous.second = 1;

    for(int i=0; i<totalCouriers; ++i) {

        for(int j=i+1; j<totalCouriers; ++j) {

            int commlca = lca(crlca[i], crlca[j]);

            if(commlca != crlca[i] && commlca != crlca[j])
                continue;

            pair<int,int> ways[2][2];
            ways[0][0].first = crlca[i];
            ways[0][1].first = crlca[i];
            ways[1][0].first = crlca[j];
            ways[1][1].first = crlca[j];

            ways[0][0].second = couriers[i].first;
            ways[0][1].second = couriers[i].second;
            ways[1][0].second = couriers[j].first;
            ways[1][1].second = couriers[j].second;

            int curans = 0;

            for(int m=0; m<2; ++m) {

                for(int n=0; n<2; ++n) {

                    pair<int,int> w1 = ways[0][m];
                    pair<int,int> w2 = ways[1][n];

                    //cout << "DEBUG: " << w1.first << ' ' << w1.second << ' ' << w2.first << ' ' << w2.second << ' ';

                    if(!isParent(w1.first, w2.first))
                        swap(w1, w2);
                    if(!isParent(w1.first, w2.first))
                        continue;

                    if(!isParent(w2.first, w1.second))
                        continue;

                    w2.second = lca(w1.second, w2.second);

                    if(!isParent(w2.first, w2.second))
                        continue;

                    curans += cntDist(w2.first, w2.second);
                    //cout << cntDist(w2.first, w2.second) << endl;

                }

            }

            //cout << "DEBUG: " << i << ' ' << j << ' ' << curans << endl;

            if(curans > answer) {

                answer = curans;
                cous.first = i;
                cous.second = j;

            }


        }

    }

    cout << answer << endl;
    cout << cous.first+1 << ' ' << cous.second+1 << endl;

    //cout << "DEBUG\n";
    //for(int i=0; i<totalWorkers; ++i)
    //    for(int j=i+1; j<totalWorkers; ++j) {
    //        cout << "lca(" << i << ", " << j << ") = " << lca(i, j) << endl;
    //        cout << "distance from " << i << " to " << lca(i, j) << " = " << cntDist(lca(i,j), i) << endl;
    //        cout << "distance from " << j << " to " << lca(i, j) << " = " << cntDist(lca(i,j), j) << endl;
    //    }
    //cout << "distances[]\n";
    //for(int k=0; k<=logtw; ++k) {

    //    for(int i=0; i<totalWorkers; ++i)
    //        cout << distances[i][k] << ' ';
    //    cout << endl;

    //}
    //cout << "Also:\n";
    //cout << cntDist(1, 4) << endl;

    return 0;

}
