#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>

using namespace std;

const int maxn = 1000;

int n, k;
int p[maxn];
int dep[maxn];
int numb[maxn];
bool used[maxn][maxn];

void clear() {
	for (int i = 0; i < maxn; i++) {
		numb[i] = -1;
	}
}

pair <int, int> kur[maxn];
vector <int> lens[maxn][maxn];

int go(int i, int j) {
	clear();

	int a1 = kur[i].first;
	int b1 = kur[i].second;

	for (int k = 0; k < (int)lens[a1][b1].size(); k++) {
		numb[lens[a1][b1][k]] = k;
	}

	int a2 = kur[j].first;
	int b2 = kur[j].second;

	int num1 = -1, num2 = -1;

	for (int k = 0; k < (int)lens[a2][b2].size(); k++) {
		if (numb[lens[a2][b2][k]] != -1) {
			num1 = numb[lens[a2][b2][k]];
			num2 = k;
		}
	}

	int now = 0;
	int n1 = num1, n2 = num2;
	n1++;
	n2++;

	while (true) {
		if (n1 >= lens[a1][b1].size() || n2 >= lens[a2][b2].size()) {
			break;
		}

		if (lens[a1][b1][n1] == lens[a2][b2][n2]) {
			now++;
			n1++;
			n2++;
		}
		else {
			break;
		}
	}

	n1 = num1, n2 = num2;
	n1--;
	n2--;

	while (true) {
		if (n1 < 0 || n2 < 0) {
			break;
		}

		if (lens[a1][b1][n1] == lens[a2][b2][n2]) {
			now++;
			n1--;
			n2--;
		} else {
			break;
		}
	}

	return now;
}

int main() {
	freopen("twopaths.in", "r", stdin);
	freopen("twopaths.out", "w", stdout);

	scanf("%d %d", &n, &k);

	p[1] = 1;
	dep[1] = 1;

	for (int i = 2; i <= n; i++) {
		int pr;
		scanf("%d", &pr);

		p[i] = pr;
		dep[i] = dep[pr] + 1;
	}

	for (int i = 0; i < k; i++) {
		int a, b;
		scanf("%d %d", &a, &b);

		kur[i] = make_pair(a, b);
	}

	for (int i = 0; i < k; i++) {
		int a = kur[i].first;
		int b = kur[i].second;

		int na = a;
		int nb = b;

		if (!used[a][b]) {
			while (na != nb) {
				if (dep[na] >= dep[nb]) {
					na = p[na];
				}

				if (dep[na] < dep[nb]) {
					nb = p[nb];
				}
			}

			int c = na;

			na = a;
			nb = b;

			while (na != c) {
				lens[a][b].push_back(na);
				na = p[na];
			}

			lens[a][b].push_back(c);

			vector <int> help;

			while (nb != c) {
				help.push_back(nb);
				nb = p[nb];
			}

			reverse(help.begin(), help.end());

			for (int i = 0; i < (int)help.size(); i++) {
				lens[a][b].push_back(help[i]);
			}
			used[a][b] = true;
		}
	}

	pair <int, int> result = make_pair(1, 2);
	int maxx = 0;

	for (int i = 0; i < k; i++) {
		for (int j = i + 1; j < k; j++) {
			
			int now = go(i, j);

			if (now > maxx) {
				maxx = now;
				result = make_pair(i + 1, j + 1);
			}

			int a = kur[i].first;
			int b = kur[i].second;

			reverse(lens[a][b].begin(), lens[a][b].end());

			now = go(i, j);

			if (now > maxx) {
				maxx = now;
				result = make_pair(i + 1, j + 1);
			}
		}
	}

	cout << maxx << endl;

	cout << result.first << ' ' << result.second << endl;

	return 0;
}
/*
4 2
1 2 2
1 3 
1 4
*/

/*
7 3
1 2 2 4 5 5
1 3
3 7
6 1
*/