public static void main(String args[]) {
        //Scanner in = new Scanner(Dwarfs.class.getResourceAsStream("/lvl2/dwarfs/in.txt"));
        Scanner in = new Scanner(System.in);

        Map<Integer, Set<Integer>> relations = new HashMap<Integer, Set<Integer>>();

        Set<Integer> personsWithNoParent = new HashSet<Integer>();

        int n = in.nextInt();
        for (int i = 0; i < n; i++) {
            int x = in.nextInt();
            int y = in.nextInt();
            if (!relations.containsKey(x)) {
                relations.put(x, new HashSet<Integer>());
            }
            if (!relations.containsKey(y)) {
                relations.put(y, new HashSet<Integer>());
            }
            relations.get(x).add(y);
            personsWithNoParent.add(x);
        }
        for (Set<Integer> relation : relations.values()) {
            personsWithNoParent.removeAll(relation);
        }

        int longestPropag = 0;
        for (int person : personsWithNoParent) {

            List<Integer> lastPersonsVisited = Arrays.asList(person);
            int propag = 0;

            // Algo de recherche en largeur.
            while (!lastPersonsVisited.isEmpty()) {

                List<Integer> nextPersons = new ArrayList<Integer>();

                for (Integer lastPersonVisited : lastPersonsVisited) {

                    for (Integer nextPerson : relations.get(lastPersonVisited)) {
                        nextPersons.add(nextPerson);
                    }
                }

                lastPersonsVisited = nextPersons;

                propag++;
            }

            if (propag > longestPropag) {
                longestPropag = propag;
            }
        }
        System.out.println(longestPropag);
    }