#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

vector<vector<int>> g;
vector<vector<int>> cnt;

ifstream in("twopaths.in");
ofstream out("twopaths.out");

bool dfs(int now, int pred, int finish, int numb) {
    if (now == finish) {
        cnt[numb].push_back(now);
        return true;
    }

    for (int x : g[now]) {
        if (x != pred) {
            if (dfs(x, now, finish, numb)) {
                cnt[numb].push_back(now);
                return true;
            }
        }
    }

    return false;
}

int main() {
    int n, k, pi;
    in >> n >> k;

    g.resize(n);

    for (int i = 1; i < n;++i) {
        in >> pi;

        g[i].push_back(pi - 1);
        g[pi - 1].push_back(i);
    }
    int ans = 0, la = 1, lb = 2;

    cnt.resize(n);

    int s, f;
    for (int i = 0; i < k; ++i) {
        in >> s >> f;
        dfs(s - 1, -1, f - 1, i);
        sort(cnt[i].begin(), cnt[i].end());
    }

    for (int i = 0; i < k - 1; ++i) {
        for (int j = i + 1; j < k; ++j) {
            int sums = 0;
            int l = 0, r = 0;

            while (l < cnt[i].size() && r < cnt[j].size()) {
                if (cnt[i][l] == cnt[j][r]) {
                    sums++;
                    l++;
                    r++;

                    continue;
                }

                if (cnt[i][l] < cnt[j][r])
                    l++;
                else
                    r++;
            }
            sums -= 1;
            if (sums > ans) {
                ans = sums;
                la = i + 1;
                lb = j + 1;
            }
        }
    }

    out << ans << endl << la << " " << lb;

    return 0;
}
