#include <iostream>
#include <fstream>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

int n, k, ans[1000][1000], maxy, l, r;
vector <int> v[1000], bb[1000][1000];
bool used[1000];

bool dfs(int w, int b, int a) {
	used[w] = true;
	if (b == w)
		return true;
	for (int i = 0; i < v[w].size(); i++)
		if (!used[v[w][i]])
			if (dfs(v[w][i], b, a))
				bb[w][v[w][i]].push_back(a);
}

int main() {
	//ifstream cin("in.txt");
	//ofstream cout("out.txt");
	ifstream cin("twopaths.in");
	ofstream cout("twopaths.out");
	cin >> n >> k;
	for (int i = 1; i <= n; i++) {
		int p;
		cin >> p;
		v[i + 1].push_back(p);
		v[p].push_back(i + 1);
	}
	for (int i = 1; i <= k; i++) {
		int a, b;
		for (int j = 1; j <= n; j++)
			used[j] = false;
		cin >> a >> b;
		dfs(a, b, i);
	}
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			for (int l = 0; l < bb[i][j].size(); l++)
				for (int r = 0; r < bb[i][j].size(); r++)
					ans[bb[i][j][l]][bb[i][j][r]]++;
	for (int i = 1; i <= k; i++)
		for (int j = 1; j <= k; j++)
			if (i != j && ans[i][j] > maxy) {
				maxy = ans[i][j];
				l = i;
				r = j;
			}
	cout << maxy << endl;
	cout << l << " " << r;
	return 0;
}