#define fname "twopaths"

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

#include <vector>
#include <map>
#include <set>
#include <string>
#include <unordered_map>
#include <time.h>


#define forn(i, n) for (ll i = 0; i < n; i ++)
#define fornn(i, m, n) for (ll i = m; i < n; i ++)

#define inb push_back
#define mk make_pair
#define X first
#define Y second

#define times clock() / 1000.0

using namespace std;

typedef long long ll;

const ll dd = 4e5;
const ll inf = 1e16;
const ll mod = 1e18 + 9;

vector<vector<int> > ways;
vector<pair<int, int> > qwe;

vector<int> used;
vector<int> used1;

set<pair<int, int> > qq, qq1;

int timer = 1;

bool go(int v, int f) {
	used[v] = timer;
	if (v == f)
		return 1;
	forn(i, ways[v].size()) {
		if (used[ways[v][i]] < timer) {
			qq.insert(mk(min(v, ways[v][i]), max(v, ways[v][i])));
			if (go(ways[v][i], f))
				return 1;
			qq.erase(mk(min(v, ways[v][i]), max(v, ways[v][i])));
		}
	}
	return 0;
}



bool go1(int v, int f) {
	used1[v] = timer;
	if (v == f)
		return 1;
	forn(i, ways[v].size()) {
		if (used1[ways[v][i]] < timer) {
			qq1.insert(mk(min(v, ways[v][i]), max(v, ways[v][i])));
			if (go1(ways[v][i], f))
				return 1;
			qq1.erase(mk(min(v, ways[v][i]), max(v, ways[v][i])));
		}
	}
	return 0;
}




int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#else
	freopen(fname".in", "r", stdin);
	freopen(fname".out", "w", stdout);
#endif
	int n, m;
	scanf("%d%d", &n, &m);
	ways.resize(n);
	used.resize(n);
	used1.resize(n);
	fornn(i, 1, n) {
		int p;
		scanf("%d", &p);
		p--;
		ways[p].inb(i);
		ways[i].inb(p);
	}

	forn(i, m) {
		int a, b;
		scanf("%d%d", &a, &b);
		a--, b--;
		qwe.inb(mk(a, b));
	}

	int maxx = 0;
	pair<int, int> ans = mk(0, 1);

	vector<int> nums;
	forn(i, m)
		nums.inb(i);

	random_shuffle(nums.begin(), nums.end());

	forn(ii, m) {
		int i = nums[ii];
		forn(jj, ii) {
			int j = nums[jj];
			timer++;
			qq.clear();
			qq1.clear();
			go(qwe[i].X, qwe[i].Y);
			go1(qwe[j].X, qwe[j].Y);
			int cnt = 0;
			if (qq.size() > qq1.size())
				qq.swap(qq1);

			for (auto ind = qq.begin(); ind != qq.end(); ind++)
				if (qq1.count(*ind))
					cnt++;
			if (maxx < cnt)
				maxx = cnt, ans = mk(i, j);
		}
		if (times > 1.8)
			break;
	}
	printf("%d\n", maxx);
	printf("%d %d", ans.X + 1, ans.Y + 1);
	return 0;
}