   import java.io.*;
   import java.util.*;

    class DWITE5
   {
   
   
   
   
      static PrintWriter out;
      static boolean found;
      
       public static void dfs (boolean [][]grid, int startNode, int now, int distance)
      {
         if (startNode == now && distance != 0)
         {
            out.println (distance);
            found = true;
         }
         else if (!found)
         {
            for (int i = 1; i < grid [now].length; i++)
               if (grid [now][i])
                  dfs (grid, startNode, i, distance + 1);
               
         }
      }
      
      
   
   
   
       public static void main (String [] args) throws IOException
      {
         Scanner test = new Scanner (System.in);
         Scanner in = new Scanner (new File ("DATA5.txt"));
         out = new PrintWriter (new FileWriter ("OUT5.txt"));
      
      
         for (int x = 0; x < 5; x++)
         {
            found = false;
            boolean grid [][] = new boolean [101][101];
            for (int i = 0; i < 101; i++)
               for (int j = 0; j < 101; j++)
                  grid [i][j] = false;
         
            int numPairs = in.nextInt();   
            for (int i = 0; i < numPairs; i++)
               grid [in.nextInt()][in.nextInt()] = true;
               
            dfs (grid, 1, 1, 0);
            
            
            
            
            
            
         }
         out.close();
      
      }
      
      
   }


