// A.cpp : Defines the entry point for the console application.
//

#include <fstream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

struct vertex
{
	vector<int> n, e;
};

vector<vertex> g;
vector<vector<int>> w;
vector<int> d;
int t;

int dfs(int i, int f, int pr)
{
	int j;
	if (i == f)
		return 1;
	for (j = 0; j < g[i].n.size(); j++)
	{
		if (g[i].n[j] != pr && dfs(g[i].n[j], f, i))
		{
			w[t].push_back(g[i].e[j]);
			return 1;
		}
	}
	return 0;
}

int main()
{
	int n, i, j, k, ans = 0, ans1 = 1, ans2 = 2, s, f, h, r, sum;
	vertex v;
	ifstream cin("twopaths.in");
	ofstream cout("twopaths.out");
	cin >> n >> k;
	for (i = 0; i < n; i++)
		g.push_back(v);
	for (i = 1; i < n; i++)
	{
		cin >> j;
		g[j - 1].n.push_back(i);
		g[j - 1].e.push_back(i - 1);
		g[i].n.push_back(j - 1);
		g[i].e.push_back(i - 1);
	}

	for (t = 0; t < k; t++)
	{
		cin >> s >> f;
		w.push_back({});
		dfs(s - 1, f - 1, -1);
		sort(w[t].begin(), w[t].end());
	}

	for (i = 0; i < k; i++)
	{
		for (j = 0; j < i; j++)
		{
			t = 0;
			h = 0;
			sum = 0;
			for (r = 0; r < n - 1; r++)
			{
				for (t; t < w[i].size() && w[i][t] < r; t++) {}
				for (h; h < w[j].size() && w[j][h] < r; h++) {}
				if (t == w[i].size() || h == w[j].size())
					break;
				if (w[i][t] == r && w[j][h] == r)
					sum++;
			}
			if (sum > ans)
			{
				ans = sum;
				ans1 = i + 1;
				ans2 = j + 1;
			}
		}
	}

	cout << ans << endl << ans1 << ' ' << ans2 << endl;
	cout.close();
}

