#include <bits/stdc++.h>

using namespace std;

//#define SOL_48

vector<vector<int> > g;
vector<int> d;

#ifdef SOL_48

vector<int> up[20];

void dfs(int v)
{
    for (int j = 1; j < 20; j++)
        up[j][v] = up[j - 1][up[j - 1][v]];
    for (int &u : g[v])
    {
        up[0][u] = v;
        d[u] = d[v] + 1;
        dfs(u);
    }
}

int go_up(int v, int k)
{
    for (int i = 19; i >= 0; i--)
    {
        if ((1 << i) <= k)
        {
            k -= (1 << i);
            v = up[i][v];
        }
    }
    return v;
}

void cnt_lca(int n)
{
    for (auto &i : up)
        i.resize(n);
    up[0][0] = 0;
    dfs(0);
}

int lca(int a, int b)
{
    if (d[a] > d[b])
        swap(a, b);
    b = go_up(b, d[b] - d[a]);
    if (b == a)
        return a;
    for (int i = 19; i >= 0; i--)
    {
        if (up[i][a] != up[i][b])
        {
            a = up[i][a];
            b = up[i][b];
        }
    }
    return up[0][a];
}

#else

struct sp_t
{
    int n;
    vector<int> arr;
    vector<int> t[20];
    vector<int> p2;
    void cnt_p2()
    {
        p2.resize(n + 1);
        p2[1] = 0;
        for (int i = 2; i <= n; i++)
            p2[i] = p2[i / 2] + 1;
    }
    int min_p(int a, int b)
    {
        return arr[a] < arr[b] ? a : b;
    }
    sp_t(vector<int> arr = vector<int>(0)) : arr(arr)
    {
        n = arr.size();
        cnt_p2();
        for (int i = 0; i < 20; i++)
            t[i].resize(max(n - (1 << i) + 1, 0));
        for (int i = 0; i < n; i++)
            t[0][i] = i;
        for (int p = 1; p < 20; p++)
            for (int i = 0; i < t[p].size(); i++)
            {
                t[p][i] = min_p(t[p - 1][i], t[p - 1][i + (1 << (p - 1))]);
            }
    }
    int s_min(int l, int r)
    {
        int mp2 = p2[r - l];
        return min_p(t[mp2][l], t[mp2][r - (1 << mp2)]);
    }
} lca_t;

vector<int> v_arr;
vector<int> d_arr;
vector<int> f_inc, l_inc;

void dfs(int v)
{
    f_inc[v] = l_inc[v] = v_arr.size();
    d_arr.push_back(d[v]);
    v_arr.push_back(v);
    for (int u : g[v])
    {
        d[u] = d[v] + 1;
        dfs(u);
        l_inc[v] = v_arr.size();
        d_arr.push_back(d[v]);
        v_arr.push_back(v);
    }
}

void cnt_lca(int n)
{
    f_inc.resize(n);
    l_inc.resize(n);
    dfs(0);
    lca_t = sp_t(v_arr);
}

int lca(int a, int b)
{
    if (f_inc[a] > l_inc[b])
        swap(a, b);
    return v_arr[lca_t.s_min(f_inc[a], l_inc[b])];
}

#endif // SOL_48

int len(int a, int b)
{
    int v = lca(a, b);
    return d[a] - d[v] + d[b] - d[v];
}


int main()
{
    ifstream cin("twopaths.in");
    ofstream cout("twopaths.out");
    int n, m;
    cin >> n >> m;
    g.resize(n);
    d.resize(n);
    for (int i = 1; i <= n - 1; i++)
    {
        int x;
        cin >> x;
        g[x - 1].push_back(i);
    }
    d[0] = 0;
    cnt_lca(n);
    int l[m], r[m], c[m];
    for (int i = 0; i < m; i++)
    {
        cin >> l[i] >> r[i];
        l[i]--;
        r[i]--;
        c[i] = lca(l[i], r[i]);
    }
    int ai, aj;
    int ans = 0;
    for (int i = 0; i < m; i++)
        for (int j = 0; j < m; j++) if (i != j && d[c[i]] >= d[c[j]])
            for (int k = 0; k < 2; k++, swap(l[j], r[j]))
            {
                int la = lca(l[i], l[j]);
                if (d[la] < d[c[i]])
                    la = c[i];
                int ra = lca(r[i], r[j]);
                if (d[ra] < d[c[i]])
                    ra = c[i];
                int na = len(la, ra);
                if(ans <= na)
                {
                    ans = na;
                    ai = i;
                    aj = j;
                }
            }
    cout << ans << "\n" << ai + 1 << " " << aj + 1 << " ";
    return 0;
}
