#include <iostream>
#include <vector>
#include <algorithm>
#include <set>

using namespace std;

int main() {
	freopen("twopaths.in", "r", stdin);
	freopen("twopaths.out", "w", stdout);
	int n, k;
	cin >> n >> k;
	vector<int> p(n);
	p[0] = 0;
	for (int i = 1; i < n; i++) {
		cin >> p[i];
		p[i]--;
	}
	vector< pair<int, int> > arr(k);
	for (int i = 0; i < k; i++) {
		cin >> arr[i].first >> arr[i].second;
		arr[i].first--;
		arr[i].second--;
	}
	vector< vector< pair<int, int> > > g(k);
	for (int i = 0; i < k; i++) {
		set<int> s;
		int x = arr[i].first;
		while (x != 0) { s.insert(x); x = p[x]; }
		s.insert(0);
		x = arr[i].second;
		while (s.count(x) == 0) { g[i].push_back({ x, p[x] }); x = p[x]; }
		int y = arr[i].first;
		while (y != x) { g[i].push_back({ y, p[y] }); y = p[y]; }
		for (int j = 0; j < g[i].size(); j++) {
			if (g[i][j].first > g[i][j].second) { swap(g[i][j].first, g[i][j].second); }
		}
		sort(g[i].begin(), g[i].end());
	}
	int mx = 0;
	int a = 1, b = 2;
	for (int i = 0; i < k; i++) {
		for (int j = i + 1; j < k; j++) {
			int h = 0;
			int i1 = 0, i2 = 0;
			while (i1 < g[i].size() && i2 < g[j].size()) {
				if (g[i][i1] == g[j][i2]) { h++; i1++; i2++; }
				else {
					if (g[i][i1] > g[j][i2]) { i2++; }
					else { i1++; }
				}
			}
			if (h > mx) { mx = h; a = i + 1; b = j + 1; }
		}
	}
	cout << mx << '\n';
	cout << a << ' ' << b;
	return 0;
}