#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<vector<int>> g;
vector<int> p, used, tin, tout;
int time;
int min(int a, int b) {
	return a < b ? a : b;
}
int max(int a, int b) {
	return a > b ? a : b;
}
void dfs(int v) {
	used[v] = 1;
	tin[v] = time++;
	for (int i = 0; i < g[v].size(); i++)
		if (!used[g[v][i]])
			dfs(g[v][i]);
	tout[v] = time;
}
int main() {
	freopen("twopaths.in", "r", stdin);
	freopen("twopaths.out", "w", stdout);
	int n, k;
	cin >> n >> k;
	p.resize(n, -1);
	used.resize(n);
	tin.resize(n);
	tout.resize(n);
	g.resize(n);
	vector<vector<pair<int, int> > > cour(k);
	for (int i = 1; i < n; i++) {
		scanf("%d", &p[i]);
		p[i]--;
		g[p[i]].push_back(i);
	}
	dfs(0);
		for (int i = 0; i < k; i++) {
			int a, b;
			scanf("%d%d", &a, &b);
			--a, --b;
			int f = a, s = b;
			while (!(tout[a] >= tout[b] && tin[a] < tin[b]) && a!=b) {
				cour[i].push_back({ min(p[a], a), max(p[a], a) });
				a = p[a];
			}
			while (b != a)
			{
				cour[i].push_back({ min(p[b], b), max(p[b], b) });
				b = p[b];
			}
			sort(cour[i].begin(), cour[i].end());
		}
	long long ans = 0;
	int f = 1, s = 2;
	for (int i = 0; i < k; i++) {
		for (int j = i + 1; j < k; j++){
			int cur = 0;
			if (cour[j].size() <= ans) continue;
			int fl = 0;
			for (int l = 0; l < cour[j].size(); l++) {
				int ql = 0, qr=cour[i].size();
				while (qr - ql > 1) {
					int m = (ql + qr) / 2;
					if (cour[j][l] > cour[i][m])
						ql = m;
					if (cour[j][l] < cour[i][m])
						qr = m;
					if (cour[j][l] == cour[i][m] || cour[j][l]==cour[i][ql]  || cour[j][l]== cour[i][min(qr, cour[i].size()-1)]) {
						cur++;
						fl = 1;
						break;
					}
				}
				if(!fl)
					if (cour[j][l] == cour[i][ql] || cour[j][l] == cour[i][min(qr, cour[i].size() - 1)]) {
						cur++;
						if (cour[j].size() - l - 1 + cur <= ans) break;
					}
			}
			if (cur > ans) {
				ans = cur;
				f = i + 1, s = j + 1;
			}
		}
	}
	cout << ans << endl << f << ' ' << s;
}