program tree;
var n,i,j,x,y,koren,count:word;
    flag:shortint;
    A,br:array[1..2000] of byte;
begin
     for i:=1 to 2000 do A[i]:=0;
     readln(n);
     flag:=0;
     for i:=1 to n do begin
         readln(x,y);
         if A[y]=1
            then flag:=-1
            else A[y]:=1;
         br[x]:=1; br[y]:=1;
     end;

if flag<>-1
   then begin
     flag:=0;
     for i:=1 to 2000 do begin
         if (br[i]=1) and (A[i]=0)
            then if flag=0
                 then begin
                      flag:=1;
                      koren:=i;
                 end
                 else flag:=-1;
         if br[i]=1
            then inc(count);
     end;
   end;

     if flag=1
        then writeln(count,' ',koren)
        else writeln('0 0');
end.