#include <algorithm>
#include <iostream>
#include <iomanip>
#include <vector>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <iterator>
using namespace std;


int G[4001][4001];
vector <int> gr[1000000];
int tin[1000000];
int tout[1000000];
int path[4001][4001];
bool fnd;
int dfstime = 0;

void dfs(int st, int prev, int sr) {
	if (st == sr) {
		fnd = true;
		return;
	}
	for (int i = 0; i < 200; i++) {
		if (G[st][i] == 1 and i != prev) {
			path[st][i] += 1;	
			path[i][st] += 1;
			dfs(i, st, sr);
			if (!fnd) {
				path[st][i] -= 1;	
				path[i][st] -= 1;	
			} else {
				break;
			}
		}
	}
}

void dfs(int st, int prev) {
	tin[st] = dfstime;
	for (auto k : gr[st]) {
		if (k != prev) {
			dfstime++;
			dfs(k, st);
		}
	}
}

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

	int n, m;
	cin >> n >> m;
	if (n <= 100) {
		for (int i = 0; i < 200; i++) {
			for (int j = 0; j < 200; j++) {
				G[i][j] = 0;
				path[i][j] = 0;
			}
		}
		for (int i = 1; i < n; i++) {
			int dfstimep;
			cin >> dfstimep;
			G[dfstimep-1][i] = 1;
			G[i][dfstimep-1] = 1;
		}
		vector <pair<int, int> > carry;
		for (int i = 0; i < m; i++) {
			int f, t;
			cin >> f >> t;
			carry.push_back(make_pair(f-1, t-1));
		}

		pair<int, int> answer = make_pair(-1, -1);
		int anss = -1;
		for (int i = 0; i < m; i++) {
			for (int j = i + 1; j < m; j++) {
				// cout  << i << " " << j << "&&&&&&&&" << endl;
				fnd = false;
				dfs(carry[i].first, -1, carry[i].second);
				fnd = false;
				dfs(carry[j].first, -1, carry[j].second);
				int sm = 0;
				for (int x = 0; x < 200; x++) {
					for (int y = 0; y < 200; y++) {
						sm += (path[x][y] == 2);
						// if (path[x][y] == 2) {
							// cout << x << " " << y << endl;
						// }
						path[x][y] = 0;
					}
				}
				if (sm > anss) {
					anss = sm;
					answer = make_pair(i, j);
				}
			}
		}
		cout << anss / 2<< endl << answer.first + 1 << " " << answer.second + 1 << endl;
	} else {
		for (int i = 1; i < n; i++) {
			int dfstimep;
			cin >> dfstimep;
			gr[dfstimep-1].push_back(i);
			gr[i].push_back(dfstimep-1);
		}
		dfs(0, -1);
		vector <pair<int, int> > carry;
		for (int i = 0; i < m; i++) {
			int f, t;
			cin >> f >> t;
			carry.push_back(make_pair(f-1, t-1));
		}
		pair<int, int> answer = make_pair(-1, -1);
		int anss = -1;
		for (int i = 0; i < m; i++) {
			for (int j = i + 1; j < m; j++) {
				int a = min(tin[carry[i].first], tin[carry[i].second]);
				int b = max(tin[carry[i].first], tin[carry[i].second]);
				int c = min(tin[carry[j].first], tin[carry[j].second]);
				int d = max(tin[carry[j].first], tin[carry[j].second]);
				int answ;
				if (b < c) {
					answ = 0;
				} else {
					answ = min(b, d) - max(a, c);
				}
				if (anss < answ) {
					anss = answ;
					answer = make_pair(i, j);
				}
			}
		}
		cout << anss << endl << answer.first + 1 << " " << answer.second + 1 << endl;
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}