var a:Array[1..2000,1..2000] of Boolean;
    kraen:Array[1..2000] of Boolean;
    known:Array[1..2000] of Boolean;
    queue:Array[1..2222] of Longint;
    vis:Array[1..2000] of Boolean;
    n,min,max,i,j,k,qt,qh,br:Longint;
    goodfornow,found:Boolean;
Procedure bfs(Koren:Longint);
Begin
 For k:=1 to 2000 do Begin
                       queue[k]:=0;
                       vis[k]:=false;
                     End;
  queue[1]:=Koren;
  vis[koren]:=true;
  qt:=0;
  qh:=1;
 Repeat
   qt:=qt+1;
  For br:=min to max do
   if (a[queue[qt],br])and(not vis[br])and(known[br])then
    Begin
      qh:=qh+1;
      queue[qh]:=br;
      vis[br]:=true;
    End;
 Until qt=qh;
End;
Begin
  ReadLn(n);
  min:=3000;
  max:=-3000;
 For k:=1 to n do
 Begin
   ReadLn(i,j);
   a[i,j]:=true;
   known[i]:=True;
   known[j]:=true;
  if i<min then min:=i;
  if j<min then min:=j;
  if i>max then max:=i;
  if j>max then max:=j;
   kraen[j]:=True;
 End;
  goodfornow:=False;
 For i:=min to max do
  if (known[i])and(not kraen[i])then
   Begin
     goodfornow:=True;
     Break;
   End;
 if not goodfornow then
 Begin
   WriteLn('0 0');
   ReadLn;
   Halt(0);
 End;
 {ne zabravqi proverkata za dygi ot i kum vyzel}
 {nachalo}
 For i:=min to max do
 if known[i] then
 Begin
   br:=0;
  For j:=min to max do if known[j] then
   if (a[j,i]) then br:=br+1;
  If br>1 then
  Begin
    WriteLn('0 0');
    ReadLn;
    Halt(0);
  End;
 End;
 {krai}
 For i:=min to max do
 if (not kraen[i])and(known[i])then
 Begin
   goodfornow:=True;

     bfs(i);
    For j:=min to max do
     if(known[j])and(kraen[j])then
     Begin
       found:=false;
      For k:=1 to 2000 do if queue[k]=j then
       Begin
         found:=True;
         Break;
       End;
      If not found then
       Begin
         goodfornow:=False;
         Break;
       End;
     End;

  If goodfornow then Break;
 End;

  If goodfornow then
  Begin
    br:=0;
   For j:=min to max do if known[j] then br:=br+1;
    WriteLn(br,' ',i);
    ReadLn;
    Halt(0);
  End
  else
  Begin
    WriteLn('0 0');
    ReadLn;
    Halt(0);
  End;
End.