#include <bits/stdc++.h>
#define pb push_back
#define ins insert
#define mkp make_pair
#define mt make_tuple
using namespace std;

typedef long long int ll;

const int N = 10000 + 5;



int n, k;

vector<int> neig[N];
int parent[N];
int as[N];
int bs[N];
int cs[N];
int first[N];
int depth[N];
int way[N];
int wayCnt = 0;
int tree[4 * N];

int getPredok(int a, int b) {
    int mini = 1000000000;
    int ans = -1;
    for (int i = min(first[a], first[b]); i <= max(first[a], first[b]); i++) {
        if (mini > depth[way[i]]) {
            ans = way[i];
            mini = depth[way[i]];
        }
    }
    return ans;
}

void dfsWay(int v, int d) {
    first[v] = wayCnt;
    depth[v] = d;
    way[wayCnt++] = v;
    for (auto it : neig[v]) {
        dfsWay(it, d + 1);
        way[wayCnt++] = v;
    }
}

int getCurr(int a, int b, int c, int d) {
    if (a == b || c == d)
        return 0;

    int pred = getPredok(a, c);
    if (depth[pred] <= min(depth[b], depth[d]))
        return 0;

    return max(0, depth[pred] - max(depth[d], depth[b]));
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

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

    cin >> n >> k;

    for (int i = 1; i < n; i++) {
        cin >> parent[i];
        parent[i]--;
        neig[parent[i]].pb(i);
    }

    dfsWay(0, 0);

    for (int i = 0; i < k; i++) {
        cin >> as[i] >> bs[i];
        as[i]--;
        bs[i]--;
        cs[i] = getPredok(as[i], bs[i]);
    }

    int maxi = 0, a = 0, b = 1;

    for (int i = 0; i < k; i++) {
        for (int z = i + 1; z < k; z++) {
            int curr = 0;
            curr += getCurr(as[i], cs[i], as[z], cs[z]);
            curr += getCurr(as[i], cs[i], bs[z], cs[z]);
            curr += getCurr(bs[i], cs[i], as[z], cs[z]);
            curr += getCurr(bs[i], cs[i], bs[z], cs[z]);
            if (curr > maxi) {
                maxi = curr;
                a = i;
                b = z;
            }
        }
    }

    cout << maxi << "\n" << a + 1 << " " << b + 1;
}