#include <bits/stdc++.h>
using namespace std;

int w[101];
vector<int> wrs[101];

void mcount(int k, int a, int b) {
    int i = a;
    while (i >= 0) {
        int j = b;
        while (j != -1 && j != i) j = w[j];
        if (j == -1) {wrs[k].push_back(i); i = w[i]; continue;}

        j = b;
        if (j != -1) wrs[k].push_back(j);
        if (w[i] != -1) wrs[k].push_back(w[i]);
        while (j >= 0 && j != i) {if (w[j] != -1) wrs[k].push_back(w[j]); j = w[j];}
        break;
    }
    //for (int i: wrs[k]) cout << i << " ";
    //cout << endl;
    sort(wrs[k].begin(), wrs[k].end());
    return;
}

int main() {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    //freopen("twopaths.in", "r", stdin);
    //freopen("twopaths.out", "w", stdout);
    int n, k;
    cin >> n >> k;
    w[0] = -1;
    for (int i = 1; i < n; i++) {
        cin >> w[i];
        w[i]--;
    }

    for (int i = 0; i < k; i++) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        mcount(i, a, b);
    }

    int maxi = -1;
    int amax = 0; int bmax = 0;
    for (int i = 0; i < k; i++) {
        for (int j = i + 1; j < k; j++) {
            int p1 = 0, p2 = 0;
            int kol = 0;

            int pod = 0;
            while(p1 < wrs[i].size() && p2 < wrs[j].size()) {
                while (wrs[i][p1] < wrs[j][p2] && p1 < wrs[i].size()) {if (pod > 0) kol--; pod = 0; p1++;}
                while (wrs[j][p2] < wrs[i][p1] && p2 < wrs[j].size()) {if (pod > 0) kol--; pod = 0; p2++;}
                if (wrs[i][p1] == wrs[j][p2]) {
                    kol++;
                    p1++; p2++;
                    pod++;
                }
            }
            if (pod > 0) kol--;
            if (kol > maxi) {maxi = kol; amax = i; bmax = j;}
        }
    }

    cout << maxi << endl;
    cout << amax + 1 << " " << bmax + 1 << endl;
    return 0;
}
