import java.io.*;
import java.util.*;
// The "S5" class.
public class S5
{
    static boolean[] [] map;
    static int firstNode;
    static PrintWriter output;

    public static void main (String[] args) throws Exception
    {
	BufferedReader input = new BufferedReader (new FileReader ("DATA5.txt"));
	output = new PrintWriter (new FileWriter ("OUT5.txt"));

	for (int run = 0 ; run < 5 ; run++)
	{
	    map = new boolean [100] [100];

	    int numInput = Integer.parseInt (input.readLine ());

	    String[] line;
	    int from, to;

	    boolean read = false;
	    for (int i = 0 ; i < numInput ; i++)
	    {
		line = input.readLine ().split (" ");
		from = Integer.parseInt (line [0]);
		if (!read)
		{
		    firstNode = from;
		    read = true;
		}
		to = Integer.parseInt (line [1]);
		map [from] [to] = true;
	    }

	    recur (firstNode, 0);
	}
	input.close ();
	output.close ();
    } // main method


    static void recur (int from, int counter)
    {
	for (int i = 0 ; i < 100 ; i++)
	{
	    if (map [from] [i])
	    {
		if (i == firstNode)
		{
		    output.println (counter + 1);
		    return;
		}
		recur (i, counter + 1);
	    }
	}
    }
} // S5 class

