#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/STACK:256000000")
#include<iostream>
#include<fstream>
#include<cmath>
#include<ctime>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>

using namespace std;

typedef long long ll;
#define forn(i, n) for (int i = 0; i < n; ++i)
#define forq(i, q, n) for (int i = q; i < n; ++i)
#define mk make_pair
#define psh push_back
#define vi vector<int>
#define vll vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vii vector<pii>
#define vpll vector<pll>
#define x first
#define y second

const int MOD = 1e9 + 7;
const int MAXN = 5e4 + 7;
const ll INf = 1e15 + 7;

vector<pii> kur[MAXN];

int main()
{
	/*freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);*/
	freopen("twopaths.in", "r", stdin);
	freopen("twopaths.out", "w", stdout);
	int n, k;
	cin >> n >> k;
	unordered_map<int, int> m;
	m[0] = -1;
	forn(i, n - 1)
	{
		int x;
		scanf("%d", &x);
		//cin >> x;
		x--;
		m[i + 1] = x;
	}
	forn(i, k)
	{
		int f, t;
		scanf("%d%d", &f, &t);
		//cin >> f >> t;
		f--;
		t--;
		int f1 = f, t1 = t;
		set<pii> s1;
		while (m[f] != -1)
		{
			s1.insert(mk(f, m[f]));
			f = m[f];
		}
		vector<pii> v;
		while (m[t] != -1)
		{
			pii p = mk(t, m[t]);
			if (s1.find(p) != s1.end())
				s1.erase(p);
			else
				v.psh(mk(m[t], t));
			t = m[t];
		}
		for (auto it = s1.begin(); it != s1.end(); ++it)
		{
			pii p = *it;
			if (p.x > p.y)
				p = mk(p.y, p.x);
			if (p != mk(-1, 0))
				v.psh(p);
		}
		/*cout << f1  + 1 << " " << t1 + 1 << "\n";
		forn(j, v.size())
			cout << v[j].x << " " << v[j].y << "\n";
		cout << "\n";*/
		kur[i] = v;
	}
	int ans = 0;
	int kur1 = -1, kur2 = -1;
	forn(i, k)
		forq(j, i + 1, k)
		{
			set<pii> s;
			auto v1 = kur[i];
			auto v2 = kur[j];
			forn(k, v1.size())
				s.insert(v1[k]);
			int tekans = 0;
			forn(k, v2.size())
				if (s.find(v2[k]) != s.end())
					tekans++;
			if (tekans > ans)
			{
				ans = tekans;
				kur1 = i + 1;
				kur2 = j + 1;
			}
		}
	cout << ans << "\n" << kur1 << " " << kur2;

	return 0;
}