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

using namespace std;

const int MAXN = 100000;
const int MAXK = 50000;

int a[MAXN];
vector<vector<int>> b;
int n, k;

int f(int i, int j)
{
	vector<int> v(n);

	v[i] = 1;
	while (i > 0) {
		i = a[i];
		v[i] = 1;
	}

	while (v[j] == 0)
		j = a[j];

	return j;
}

int main()
{
#ifdef _CONSOLE
	freopen("input.txt", "r", stdin);
#else
	freopen("twopaths.in", "r", stdin);
	freopen("twopaths.out", "w", stdout);
#endif

	cin >> n >> k;

	b.resize(n);

	a[0] = 0;

	for (int i = 1; i < n; i++) {
		cin >> a[i];
		a[i]--;
	}

	for (int _i = 0; _i < k; _i++) {
		int i, j;

		cin >> i >> j;

		--i; --j;

		int t = f(i, j);

		b[i].push_back(_i);
		while (i != t) {
			i = a[i];
			b[i].push_back(_i);
		}

		while (j != t) {
			b[j].push_back(_i);
			j = a[j];
		}
	}

	int max = -1, max_i, max_j;
	int f;
	vector<int> cnt(k), tmp(k);

	for (int i = 0; i < k; i++) {
		cnt.assign(k, 0);

		for (int j = 0; j < n; j++) {
			tmp.assign(k, 0);
			f = 0;

			for (int p = 0; p < b[j].size(); p++) {
				tmp[b[j][p]] = 1;

				if (b[j][p] == i)
					f = 1;
			}

			if (f)
				for (int p = 0; p < k; p++)
					cnt[p] += tmp[p];
		}

		for (int j = i + 1; j < k; j++)
			if (cnt[j] > max) {
				max = cnt[j];
				max_i = i;
				max_j = j;
			}
	}

	cout << (max == 0 ? 0 : max - 1) << endl << max_i + 1 << ' ' << max_j + 1 << endl;
}