#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <cstring>
#include <bitset>
#include <cmath>

using namespace std;

const int MAX_N = 5001;
const int MAX_LOG = 20;

vector<vector<int> > v(MAX_N);
vector<pair<int, int> > cur, rebra;
int timer, in[MAX_N], out[MAX_N], gl[MAX_N], dp[MAX_LOG][MAX_N], n, k, lc[MAX_N], num[MAX_N], lca_[MAX_N][MAX_N];
pair<int, int> pd[MAX_N];

void dfs(int p)
{
    in[p] = timer;
    timer++;
    for (int i : v[p])
    {
        gl[i] = gl[p] + 1;
        dfs(i);
    }
    out[p] = timer;
}

bool upper(int a, int b)
{
    return in[a] <= in[b] && out[b] <= out[a];
}

void dfs_lca(int p, int pr)
{
    if (pr == -1)
    {
        for (int i = 0; i < n; i++)
            lca_[p][i] = 0;
    }
    else
    {
        for (int i = 0; i < n; i++)
        {
            int x = lca_[pr][i];
            if (upper(p, i))
                lca_[p][i] = p;
            else
                lca_[p][i] = x;
        }
    }
    for (int i : v[p])
        dfs_lca(i, p);
}

int lca(int u, int v)
{
    return lca_[u][v];
    if (upper(u, v))
        return u;
    if (upper(v, u))
        return v;
    for (int d = MAX_LOG - 1; d >= 0; d--)
    {
        if (upper(dp[d][u], v))
            continue;
        u = dp[d][u];
    }
    return dp[0][u];
}

int go_up(int len, int p)
{
    if (len < 0)
        return p;
    bitset<32> y = len;
    for (int i = 0; i < MAX_LOG; i++)
        if (y[i])
            p = dp[i][p];
    return p;
}

int dist(int a, int b)
{
    return abs(gl[a] - gl[b]);
}

int check(int s1, int f1, int p)
{
    return upper(s1, p) && upper(p, f1);
}

int per_way(int s1, int f1, int s2, int f2)
{
    if (s1 == f1 || f2 == s2)
        return 0;
    if (!upper(s1, s2) && !upper(s2, s1))
        return 0;
    if (upper(s2, s1))
    {
        swap(s1, s2);
        swap(f1, f2);
    }
    if (!upper(s2, f1))
        return 0;
    return dist(s2, lca(f1, f2));
}

int per(int a, int b)
{
    int ans = 0;
    ans += per_way(lc[a], cur[a].first, lc[b], cur[b].first);
    ans += per_way(lc[a], cur[a].second, lc[b], cur[b].first);
    ans += per_way(lc[a], cur[a].second, lc[b], cur[b].second);
    ans += per_way(lc[a], cur[a].first, lc[b], cur[b].second);
    return ans;
}

int main()
{
    #define fname "twopaths"
    freopen(fname".in", "r", stdin);
    freopen(fname".out", "w", stdout);
    scanf("%d%d", &n, &k);
    dp[0][0] = 0;
    num[0] = 0;
    for (int i = 1; i < n; i++)
    {
        int to;
        scanf("%d", &to);
        to--;
        v[to].push_back(i);
        num[i] = v.size() - 1;
        rebra.push_back(make_pair(to, i));
        dp[0][i] = to;
    }
    timer = 0;
    gl[0] = 0;
    dfs(0);
    dfs_lca(0, -1);
    for (int i = 0; i < k; i++)
    {
        int s, f;
        scanf("%d%d", &s, &f);
        s--, f--;
        cur.push_back(make_pair(s, f));
    }
    for (int i = 1; i < MAX_LOG; i++)
        for (int j = 0; j < n; j++)
            dp[i][j] = dp[i - 1][dp[i - 1][j]];
    for (int i = 0; i < k; i++)
    {
        lc[i] = lca(cur[i].first, cur[i].second);
        pd[i].first = go_up(dist(cur[i].first, lc[i]) - 1, cur[i].first);
        pd[i].second = go_up(dist(cur[i].second, lc[i]) - 1, cur[i].second);
    }
    int ansl = 0;
    int ansr = 0;
    int ans = 0;
    for (int i = 0; i < k; i++)
        for(int j = 0; j < k; j++)
            if (i != j && ans < per(i, j))
            {
                ans = per(i, j);
                ansl = i;
                ansr = j;
            }
    if (ansl == ansr)
    {
        ansl = 0;
        ansr = 1;
    }
    cout << ans << endl << ansl + 1 << " " << ansr + 1;
    return 0;
}
