# coding:utf-8

import sys

Input = []
while True:
    line = sys.stdin.readline()
    if not line:
        break
    Input.append(line.split('\n'))
num = Input.pop(0)

node = []
for i in xrange(len(Input)):
    node.append(Input[i][0].split(' '))
print node

# 建立树节点字典
shu_dict = {}
for i in xrange(len(node)):
    shu_dict[int(node[i][1])] = int(node[i][0])

all_high = []
if num == 1:
    print 1
else:
    for key in shu_dict.keys():
        high = 2
        while shu_dict[key] != 0:
            high += 1
            key = shu_dict[key]
        all_high.append(high)
    print max(all_high)