#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

#define f first
#define s second

bool dfs(int16_t i, int16_t f, vector < int16_t >& visited, vector < int16_t >& gr2, vector < vector < int16_t > >& gr1, vector < vector < int16_t > >& res, vector < int16_t >& path, int16_t k)
{
    visited[i] = 0;
    if (i == f)
    {
        res[k] = path;
        return true;
    }

    for (int16_t j = 0; j < gr1[i].size(); j++)
    {
        if (visited[gr1[i][j]])
        {
            if (gr2[i] == gr1[i][j])
                path.push_back(i);
            else
                path.push_back(gr1[i][j]);
            if (dfs(gr1[i][j], f, visited, gr2, gr1, res, path, k))
                return true;
            path.pop_back();
        }
    }
    return false;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    #ifndef LOCAL_DEBUG
    freopen("twopaths.in", "rt", stdin);
    freopen("twopaths.out", "wt", stdout);
    #endif
    int16_t n, k, asd, dsa;
    cin >> n >> k;
    vector < vector < int16_t > > gr(n);
    vector < int16_t > gr2(n);
    for (int16_t i = 1; i < n; i++)
    {
        cin >> asd;
        asd--;
        gr2[i] = asd;
        gr[i].push_back(asd);
        gr[asd].push_back(i);
    }
    vector < vector < int16_t > > res(k);
    vector < pair < int16_t, int16_t > > ku(k);
    for (int16_t i = 0; i < k; i++)
    {
        cin >> ku[i].f >> ku[i].s;
        ku[i].f--;
        ku[i].s--;
    }
    for (int16_t i = 0; i < k; i++)
    {
        vector < int16_t > path;
        path.reserve(n);
        vector < int16_t > visited(n, 1);
        dfs(ku[i].f, ku[i].s, visited, gr2, gr, res, path, i);
    }
    int16_t max_ = 0, maxi1 = 0, maxi2 = 1;
    for (int16_t i = 0; i < k; i++)
    {
        sort(res[i].begin(), res[i].end());
    }
    for (int16_t i = 0; i < k; i++)
    {
        for (int16_t j = 0; j < k; j++)
        {
            if (i != j)
            {
                int16_t curr = 0, left = 0, right = 0;
                while (left < res[i].size() && right < res[j].size())
                {
                    if (res[i][left] == res[j][right])
                    {
                        curr++, left++, right++;
                    }
                    else if (res[i][left] > res[j][right])
                    {
                        right++;
                    }
                    else
                    {
                        left++;
                    }
                }
                if (curr > max_)
                {
                    max_ = curr, maxi1 = i, maxi2 = j;
                }
            }
        }
    }
    cout << max_ << endl;
    cout << maxi1 + 1 << ' ' << maxi2 + 1 << endl;
    /*for (int16_t i = 0; i < k; i++)
    {
        cout << res[i].size() << endl;
        for (auto& x : res[i])
        {
            cout << x << ' ';
        }
        cout << endl;
    }*/
    return 0;
}
