#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;
const int lim = 5000;
const int klim = 5000;
int child[lim];
int neigh[lim];
int lvl[lim];

int min(int a, int b) {
	return (a < b ? a : b);
}

int max(int a, int b) {
	return (a > b ? a : b);
}

int euler[lim * 2];
int es = 0;
int first[lim];

void dfs(int v) {
	first[v] = es;
	euler[es++] = v;
	for (int i = child[v]; i != -1; i = neigh[i]) {
		lvl[i] = lvl[v] + 1;
		dfs(i);
		euler[es++] = v;
	}
}

int highest(int a, int b) {
	return (lvl[a] < lvl[b] ? a : b);
}

int lowest(int a, int b) {
	return (lvl[a] > lvl[b] ? a : b);
}

struct Tree {
	int n;
	int tree[lim * 4];
	Tree() {}
	Tree(int size) {
		n = size;
		int i;
		for (i = n; i < n * 2; ++i)
			tree[i] = euler[i - n];
		for (i = n - 1; i; --i)
			tree[i] = highest(tree[i * 2], tree[i * 2 + 1]);
	}
	int get(int l, int r) {
		r += n;
		int res = tree[r];
		for (l += n; l < r; l >>= 1, r >>= 1) {
			if (l & 1)
				res = highest(res, tree[l++]);
			if (r & 1)
				res = highest(res, tree[--r]);
		}
		return res;
	}
};

Tree tree;

int lca(int a, int b) {
	if (first[a] > first[b])
		swap(a, b);
	return tree.get(first[a], first[b]);
}

pair<int, int> query[klim];

int main() {
	freopen("twopaths.in", "r", stdin);
	freopen("twopaths.out", "w", stdout);
	int n, k, i, j;
	scanf("%d %d", &n, &k);
	memset(child, -1, sizeof(child));
	for (i = 1; i < n; ++i) {
		scanf("%d", &j);
		--j;
		neigh[i] = child[j];
		child[j] = i;
	}
	dfs(0);
	tree = Tree(es);
	for (i = 0; i < k; ++i) {
		scanf("%d %d", &query[i].first, &query[i].second);
		--query[i].first;
		--query[i].second;
	}
	int ans = 0, ans1, ans2, upd;
	int lca11, lca12, lca21, lca22, lca1;
	int min1, max1, min2, max2;
	for (i = 0; i < k; ++i)
		for (j = i + 1; j < k; ++j) {
			lca11 = lca(query[i].first, query[j].first);
			lca12 = lca(query[i].first, query[j].second);
			lca21 = lca(query[i].second, query[j].first);
			lca22 = lca(query[i].second, query[j].second);
			lca1 = lca(query[i].first, query[i].second);
			min1 = lowest(lca11, lca12);
			max1 = lowest(lca1, highest(lca11, lca12));
			min2 = lowest(lca21, lca22);
			max2 = lowest(lca1, highest(lca21, lca22));
			upd = lvl[min1] - lvl[max1] + lvl[min2] - lvl[max2];
			if (ans <= upd) {
				ans = upd;
				ans1 = i;
				ans2 = j;
			}
		}
	printf("%d\n%d %d", ans, ans1 + 1, ans2 + 1);
	return 0;
}