{$S-}
var
a:array[1..2000] of record
                    x,y:integer;
                    end;
b:array[1..2000,1..2000] of byte;
koren,poseten1,poseten:array[1..2000] of boolean;
vyzli:array[1..2000] of integer;
korInd,kor,n,i,p,x,y,max,br:integer;
procedure rec(ind:integer);
var i:integer;
begin
if poseten1[ind] then begin
                      writeln('0 0');
                      halt(0);
                      end;
poseten1[ind]:=true;
for i:=1 to br do if b[ind,vyzli[i]]=1 then rec(vyzli[i]);
end;

begin
readln(n);
fillchar(poseten,sizeof(poseten),false);
fillchar(poseten1,sizeof(poseten1),false);
fillchar(koren,sizeof(koren),true);
for i:=1 to n do begin
                 readln(a[i].x,a[i].y);
                 poseten[a[i].x]:=true;
                 poseten[a[i].y]:=true;
                 koren[a[i].y]:=false;
                 b[a[i].x,a[i].y]:=1;
                 end;
for i:=1 to 2000 do
 if poseten[i] then begin
                    inc(br);
                    vyzli[br]:=i;
                    if koren[i] then begin
                                     inc(kor);
                                     korInd:=i;
                                     end;
                    if kor>1 then begin
                                  writeln('0 0');
                                  halt(0);
                                  end;
                    end;
rec(korInd);
writeln(br,' ',korInd);
end.