#include <stdio.h>
#include <stdlib.h>

#define Max     2048

 int A[Max][Max];
 int n, ans;
 int ff[Max];
 int q[Max], qend;
 int koren[Max];
 int used[Max];
 int p1;

 void readf(void){
      int i, k1, k2;
 //     freopen("test.in", "r", stdin);
      scanf("%d", &n);
      for(i = 0; i < n; i++){
            scanf("%d %d", &k1, &k2);
            A[k1][k2] = 1;
            koren[k2] = 1;
            ff[k1] = 1;
            ff[k2] = 1;
      }
 }

 void fail(void){
      printf("0 0\n");
      exit(0);
 }

 void DFS(int pos){
      int i, j;
      for(i = 0; i < qend; i++){
            if( A[pos][q[i]] ){
                if( used[q[i]] )
                    fail();
                used[q[i]] = 1;
                DFS(q[i]);
            }
      }
 }

 void solve(void){
      int i;
      p1 = -1;
      readf();
      for(i = 0; i < Max; i++){
            if( ff[i] )
                q[qend++] = i;
      }
      for(i = 0; i < qend; i++){
            if( !koren[q[i]] ){
                if( q[i] != p1 && p1 != -1 )
                    fail();
                p1 = q[i];
            }
      }
      used[p1] = 1;
      DFS(p1);
      printf("%d %d\n", n+1, p1);
 }

 int main(void){
     solve();
     return 0;
 }
