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

using namespace std;

const int SIZE = 1000;

int a[SIZE], b[SIZE][SIZE];
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 cnt[SIZE][SIZE];

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

	cin >> n >> k;

	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][_i] = 1;
		while (i != t) {
			i = a[i];
			b[i][_i] = 1;
		}

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

	for (int i = 0; i < n; i++)
		for (int p = 0; p < k; p++)
			for (int q = p + 1; q < k; q++)
				cnt[p][q] += b[i][p] * b[i][q];

	int max_i = 0, max_j = 1;

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

	cout << max(cnt[max_i][max_j] - 1, 0) << endl << max_i + 1 << ' ' << max_j + 1 << endl;
}