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

using namespace std;

long long swap1(set<long long> a, set<long long> b){
    long long res = 0;
    for(auto i in a){
        if(b.find(a)) res += 1;
    }
    return res;
}



int main(){
    ifstream in("twopaths.in");
    ofstream out("twopath.out");
    long long n, k;
    in >> n >> k;
    vector<long long> prev(n-1);
    for(long long i =0; i < n -1; i++){
        in >> prev[i];
    }
    vector<vector<long long> > kur(k, vector<long long> (2));
    for(long long i = 0; i < k; k++){
        in >> kur[i][0] >> kur[i][1];
    }
    vector<set<long long> > sets(k, set<long long> (1, 1));
    for(long long i = 0; i < k; i++){
        while(kur[i][0] != 1){
            sets[i].insert(kur[i][0]);
            kur[i][0] = prev[kur[i][0] - 2];
        }
        bool res = true;
        while(kur[i][1] != 1){
            if(sets[i].find(kur[i][1]) and not res) sets[i].erase(kur[i][1]);
            else{
            if(sets[i].find(kur[i][1]) and res) res = false;
            else sets[i].insert(kur[i][1]);
        }
        }
        if(not res){
            sets[i].remove(1);
        }
    }
        long long mmin = -1;
        long long a1, b1;
        for(long long i = 0; i < k; i++){
            for(long long j = 0; j < i; j++){
                long long u = swap1(sets[i], sets[j]) - 1;
                if(u > mmin){
                    mmin = u;
                    a1 = i + 1;
                    b1 = j + 1;
                }
            }
        }
        out << mmin << endl << a1 << " " << b1 << endl;
    return 0;
}
