infile = open("twopaths.in", "r")
outfile = open("twopaths.out", "w")
n, k = list(map(int, infile.readline().split()))
prev = list(map(int, infile.readline().split()))
if(n <= 100):
    sets = [set() for i in range(k)]
    for i in range(k):
        a, b = list(map(int, infile.readline().split()))
        a, b = max(a, b), min(a, b)
        while a != 1:
            sets[i].add(a)
            a = prev[a - 2]
        sets[i].add(1)
        t = True
        while b != 1:
            if b in sets[i] and t:
                t = False
            elif b in sets[i]:
                sets[i].remove(b)
            else:
                sets[i].add(b)
            b = prev[b - 2]
        if not t:
            sets[i].remove(1)
    mmin = -1
    a1 = 0
    b1 = 0
    for i in range(k):
        for j in range(i):
            if len(sets[i] & sets[j]) - 1 >= mmin:
                mmin = len(sets[i] & sets[j]) - 1
                a1 = i + 1
                b1 = j + 1
else:
    sets = [set() for i in range(k)]
    for i in range(k):
        a, b = list(map(int, infile.readline().split()))
        a, b = max(a, b), min(a, b)
        while a != b:
            sets[i].add(a)
            a = prev[a - 2]
        sets[i].add(b)
        t = True
    mmin = -1
    a1 = 0
    b1 = 0
    for i in range(k):
        for j in range(i):
            if len(sets[i] & sets[j]) - 1 >= mmin:
                mmin = len(sets[i] & sets[j]) - 1
                a1 = i + 1
                b1 = j + 1
outfile.write(str(max(0, mmin)) + "\n" + str(a1) + " " + str(b1))

