import sys

sys.setrecursionlimit(10**8)

N = int(eval(input()))

A = [int(eval(input()))-1 for i in range(N-1)]

wins = [[] for _ in range(N)]



for i,a in enumerate(A):

    wins[a].append(i+1)



depth = [-1] * N

def dep(v):

    if len(wins[v])==0: return 0

    if depth[v] >= 0:

        return depth[v]

    ds = []

    for w in wins[v]:

        ds.append(dep(w))

    ds.sort(reverse=True)

    ret = 0

    for i,d in enumerate(ds):

        ret = max(ret, i+d+1)

    depth[v] = ret

    return ret

print((dep(0)))
import sys

input = sys.stdin.readline

sys.setrecursionlimit(10**8)



N = int(eval(input()))

A = [int(eval(input()))-1 for i in range(N-1)]

wins = [[] for _ in range(N)]

for i,a in enumerate(A):

    wins[a].append(i+1)



def depth(v):

    if len(wins[v])==0: return 0

    res = []

    for u in wins[v]:

        res.append(depth(u))

    res.sort()

    ret = 0

    for r in res:

        ret = max(r+1,ret+1)

    return ret



print((depth(0)))
import sys

sys.setrecursionlimit(10**8)
N = int(eval(input()))
A = [int(eval(input())) - 1 for i in range(N - 1)]
wins = [[] for _ in range(N)]
for i, a in enumerate(A):
    wins[a].append(i + 1)
depth = [-1] * N


def dep(v):
    if len(wins[v]) == 0:
        return 0
    if depth[v] >= 0:
        return depth[v]
    ds = []
    for w in wins[v]:
        ds.append(dep(w))
    ds.sort(reverse=True)
    ret = 0
    for i, d in enumerate(ds):
        ret = max(ret, i + d + 1)
    depth[v] = ret
    return ret


print((dep(0)))

import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**8)
N = int(eval(input()))
A = [int(eval(input())) - 1 for i in range(N - 1)]
wins = [[] for _ in range(N)]
for i, a in enumerate(A):
    wins[a].append(i + 1)


def depth(v):
    if len(wins[v]) == 0:
        return 0
    res = []
    for u in wins[v]:
        res.append(depth(u))
    res.sort()
    ret = 0
    for r in res:
        ret = max(r + 1, ret + 1)
    return ret


print((depth(0)))

+input = sys.stdin.readline -depth = [-1] * N -def dep(v): +def depth(v): -    if depth[v] >= 0: -        return depth[v] -    ds = [] -    for w in wins[v]: -        ds.append(dep(w)) -    ds.sort(reverse=True) +    res = [] +    for u in wins[v]: +        res.append(depth(u)) +    res.sort() -    for i, d in enumerate(ds): -        ret = max(ret, i + d + 1) -    depth[v] = ret +    for r in res: +        ret = max(r + 1, ret + 1) -print((dep(0))) +print((depth(0)))