program Z7;

{$APPTYPE CONSOLE}

uses
  SysUtils;
  var a,b,p:array [1..1000] of longint;
     q:array [1..1000,1..4000] of longint;
   i,n,k,j,f,s,mx,x,y,d:longint;
begin
  assign(input,'twopaths.in');
  reset(input);
  assign(output,'twopaths.out');
  rewrite(output);
  readln(n,k);
  for i:=1 to 1000 do
   for j:=1 to 1000 do
    q[i,j]:=0;
  for i:=1 to n-1 do
   read(p[i+1]);
    for i:=1 to k do
     readln(a[i],b[i]);
   close(input);
   for i:=1 to k do
    begin
      while a[i]<>1 do
       begin
         q[i,a[i]]:=q[i,a[i]]+1;
         a[i]:=p[a[i]];
       end;

       while b[i]<>1 do
       begin
           q[i,b[i]]:=q[i,b[i]]+1;
           
         b[i]:=p[b[i]];
       end;

    end;
    s:=0;
    mx:=0;
    x:=1;
    y:=2;
   for i:=1 to k-1 do
    for j:=i+1 to k do
     begin
       s:=0;
        for d:=2 to n do
         if (q[i,d]=1)and(q[j,d]=1) then s:=s+1;
       if mx<s then
        begin
         mx:=s;
         x:=i;
         y:=j;
        end;

     end;
  writeln(mx);
  writeln(x,' ',y);
  close(output);
end.
 