Program TREE;
 Var A, B: Array[1..2000] of Integer;
     Bl, Bl1: Array[1..2000] of Boolean;
     N, I, K, Br, Max: Integer;

 Function F(X: Integer): Boolean;
  Var J: Integer;
  Begin
   F := True;
   For J := 1 to N do If (X = B[J]) or (X = 0) then Begin F := False; Break End;
  End;

 Begin
  ReadLn(N);
  For I := 1 to N do Begin
                      ReadLn(A[I], B[I]);
                      If A[I] > Max then Max := A[I];
                      Bl1[A[I]] := True;
                     End;

  {Proverka za nesashtestvuvashto darvo}
  For I := 1 to N do Begin
                      If Bl[B[I]] = False then Bl[B[I]] := True
                                          else Begin WriteLn('0 0'); ReadLn; Halt End;
                     End;
  {Kray na proverkata}

  Bl := Bl1;
  For I := 1 to Max do Begin
                        If Bl[I] and F(A[I]) then Begin
                                                   Inc(Br);
                                                   K := A[I];
                                                  End;
                       End;
  If Br <> 1 then Begin WriteLn('0 0'); ReadLn; Halt End;
  WriteLn(N + 1, ' ',K);
  ReadLn
 End.