N, *A = [int(_) for _ in open(0).read().split()]

win_lose = {i: set() for i in range(1, N + 1)}

for win, lose in zip(A, list(range(2, N + 1))):

    win_lose[win].add(lose)

s1 = [1]

s2 = []

while s1:

    a = s1.pop()

    s2 += [a]

    for b in win_lose[a]:

        s1 += [b]

dp = [0] * (N + 1)

for x in s2[::-1]:

    if win_lose[x] == set():

        dp[x] = 0

    else:

        dp[x] = max(a + b for a, b in zip(

            sorted(dp[y] for y in win_lose[x])[::-1], list(range(1, N + 1))))

print((dp[1]))

N, *A = [int(_) for _ in open(0).read().split()]

win_lose = {i: set() for i in range(1, N + 1)}

for win, lose in zip(A, list(range(2, N + 1))):

    win_lose[win].add(lose)

s1 = [1]

s2 = []

while s1:

    a = s1.pop()

    s2 += [a]

    for b in win_lose[a]:

        s1 += [b]

dp = [0] * (N + 1)

for x in s2[::-1]:

    if win_lose[x] == set():

        dp[x] = 0

    else:

        dp[x] = max(

            i + v + 1

            for i, v in enumerate(sorted(dp[y] for y in win_lose[x])[::-1]))

print((dp[1]))

N, *A = [int(_) for _ in open(0).read().split()]
win_lose = {i: set() for i in range(1, N + 1)}
for win, lose in zip(A, list(range(2, N + 1))):
    win_lose[win].add(lose)
s1 = [1]
s2 = []
while s1:
    a = s1.pop()
    s2 += [a]
    for b in win_lose[a]:
        s1 += [b]
dp = [0] * (N + 1)
for x in s2[::-1]:
    if win_lose[x] == set():
        dp[x] = 0
    else:
        dp[x] = max(
            a + b
            for a, b in zip(
                sorted(dp[y] for y in win_lose[x])[::-1], list(range(1, N + 1))
            )
        )
print((dp[1]))

N, *A = [int(_) for _ in open(0).read().split()]
win_lose = {i: set() for i in range(1, N + 1)}
for win, lose in zip(A, list(range(2, N + 1))):
    win_lose[win].add(lose)
s1 = [1]
s2 = []
while s1:
    a = s1.pop()
    s2 += [a]
    for b in win_lose[a]:
        s1 += [b]
dp = [0] * (N + 1)
for x in s2[::-1]:
    if win_lose[x] == set():
        dp[x] = 0
    else:
        dp[x] = max(
            i + v + 1 for i, v in enumerate(sorted(dp[y] for y in win_lose[x])[::-1])
        )
print((dp[1]))

-            a + b -            for a, b in zip( -                sorted(dp[y] for y in win_lose[x])[::-1], list(range(1, N + 1)) -            ) +            i + v + 1 for i, v in enumerate(sorted(dp[y] for y in win_lose[x])[::-1])