#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <utility>
#include <vector>
#include <cstdio>
#include <iostream>
#include <string>
#include <set>
#include <time.h>
#include <map>
#define forn(i, n) for (int i = 0; i < (n); i++)
#define fornn(i, q, n) for (int i = (q); i < (n); i++)
#define times clock() * 1.0 / CLOCKS_PER_SEC
using namespace std;

typedef long long ll;

int solve();

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

	return 0;
}

const int MAXN = 2e3 + 7;

int P[MAXN];
vector<vector<int> > G;
int H[MAXN];

void dfs(int v, int pr = -1) {
	forn(i, G[v].size()) {
		if (G[v][i] != pr) {
			H[G[v][i]] = H[v] + 1;
			dfs(G[v][i], v);
		}
	}
}

int solve() {
	int n, k;
	cin >> n >> k;
	G.resize(n);
	forn(i, n - 1) {
		cin >> P[i + 1];
		P[i + 1]--;
		G[i + 1].push_back(P[i + 1]);
		G[P[i + 1]].push_back(i + 1);
	}
	dfs(0);
	vector<set<pair<int, int> > > T;
	forn(i, k) {
		int q, w;
		cin >> q >> w;
		set<pair<int, int> > ed;
		int cur1 = q - 1;
		int cur2 = w - 1;
		while (cur1 != cur2) {
			if (H[cur1] > H[cur2]) {
				ed.insert(make_pair(min(cur1, P[cur1]), max(cur1, P[cur1])));
				cur1 = P[cur1];
			}
			else {
				ed.insert(make_pair(min(cur2, P[cur2]), max(cur2, P[cur2])));
				cur2 = P[cur2];
			}
		}
		T.push_back(ed);
	}
	int ans = 0;
	pair<int, int> W = { 1, 2 };
	forn(i, k) {
		fornn(j, i + 1, k) {
			int cur = 0;
			for (auto it = T[i].begin(); it != T[i].end(); it++) {
				if (T[j].count(*it) != 0) {
					cur++;
				}
			}
			if (ans < cur) {
				W = { i + 1, j + 1 };
			}
			ans = max(ans, cur);
		}
	}
	cout << ans << endl;
	cout << W.first << " " << W.second;
	return 0;
}