#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

vector<vector<int> > g;
int d[1000];
int pr[1000];
vector<vector<int> > in;

void dfs(int v, int cd = 0)
{
	d[v] = cd;	
	for (int i : g[v])
	{
		dfs(i, cd + 1);
	}
}

int main()
{
	freopen("twopaths.in", "r", stdin);
	freopen("twopaths.out", "w", stdout);
	int n, m;
	cin >> n >> m;
	g.resize(n);
	for (int i = 0; i < n - 1; i++)
	{
		int p;
		cin >> p;
		p--;
		g[p].push_back(i + 1);
		pr[i + 1] = p;
	}
	dfs(0);
	for (int i = 0; i < m; i++)
	{
		int l, r;
		cin >> l >> r;
		l--, r--;
		if (d[l] < d[r])
			swap(l, r);
		vector<int> cur;
		while (d[l] > d[r])
		{
			cur.push_back(l);
			l = pr[l];
		}
		while (l != r)
		{
			cur.push_back(l);
			cur.push_back(r);
			l = pr[l];
			r = pr[r];
		}
		cur.push_back(r);
		in.push_back(cur);
	}
	int max = -1;
	int la = -1, ra = -1;
	for (int i = 0; i < (int)in.size(); i++)
		for (int j = i + 1; j < (int)in.size(); j++)
		{
			int cnt = 0;
			for (int k = 0; k < (int)in[i].size(); k++)
				for (int s = 0; s < (int)in[j].size(); s++)
				{
					if (in[i][k] == in[j][s])
						cnt++;
				}
			if (cnt > max)
			{
				max = cnt;
				la = i;
				ra = j;
			}
		}
	cout << max << endl;
	cout << la + 1 << " " << ra + 1 << endl;
}


