import sys

sys.setrecursionlimit(202020)



N = int(eval(input()))

won = [[] for i in range(N)]

for i in range(1,N):

    a = int(eval(input())) - 1

    won[a].append(i)



def height(v):

    if not won[v]: return 0

    hs = [height(op) for op in won[v]]

    ret = 0

    for i,h in enumerate(sorted(hs)):

        ret = max(ret, h + len(hs) - i)

    return ret



print((height(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

sys.setrecursionlimit(202020)
N = int(eval(input()))
won = [[] for i in range(N)]
for i in range(1, N):
    a = int(eval(input())) - 1
    won[a].append(i)


def height(v):
    if not won[v]:
        return 0
    hs = [height(op) for op in won[v]]
    ret = 0
    for i, h in enumerate(sorted(hs)):
        ret = max(ret, h + len(hs) - i)
    return ret


print((height(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)))

-sys.setrecursionlimit(202020) +sys.setrecursionlimit(10**8) -won = [[] for i in range(N)] -for i in range(1, N): -    a = int(eval(input())) - 1 -    won[a].append(i) +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 height(v): -    if not won[v]: +def dep(v): +    if len(wins[v]) == 0: -    hs = [height(op) for op in won[v]] +    if depth[v] >= 0: +        return depth[v] +    ds = [] +    for w in wins[v]: +        ds.append(dep(w)) +    ds.sort(reverse=True) -    for i, h in enumerate(sorted(hs)): -        ret = max(ret, h + len(hs) - i) +    for i, d in enumerate(ds): +        ret = max(ret, i + d + 1) +    depth[v] = ret -print((height(0))) +print((dep(0)))