import sys

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines



N,*P = list(map(int,read().split()))



graph = [[] for _ in range(N+1)]

for i,x in enumerate(P,2):

    graph[i].append(x)

    graph[x].append(i)



root = 1

parent = [0] * (N+1)

order = []

stack = [root]

while stack:

    x = stack.pop()

    order.append(x)

    for y in graph[x]:

        if y == parent[x]:

            continue

        parent[y] = x

        stack.append(y)



dp = [0] * (N+1)

for v in order[::-1]:

    p = parent[v]

    A = [dp[c] for c in graph[v] if c != p]

    if not A:

        continue

    A.sort(reverse = True)

    A = [x + i for i,x in enumerate(A,1)]

    dp[v] = max(A)



answer = dp[1]

print(answer)
import sys



read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines



def dfs_order(G, root=1):

    parent = [0] * (N + 1)

    order = []

    stack = [root]

    while stack:

        x = stack.pop()

        order.append(x)

        for y in G[x]:

            if y == parent[x]:

                continue

            parent[y] = x

            stack.append(y)

    return parent, order



N, *A = list(map(int, read().split()))

G = [[] for _ in range(N + 1)]

for i, x in enumerate(A, 2):

    G[x].append(i)

    G[i].append(x)



parent, order = dfs_order(G)



dp = [0] * (N + 1)

for v in order[::-1]:

    p = parent[v]

    nums = [dp[w] for w in G[v] if w != p]

    if not nums:

        continue

    nums.sort()

    n = len(nums)

    for i in range(n):

        nums[i] += n - i

    dp[v] = max(nums)



print((dp[1]))
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N, *P = list(map(int, read().split()))
graph = [[] for _ in range(N + 1)]
for i, x in enumerate(P, 2):
    graph[i].append(x)
    graph[x].append(i)
root = 1
parent = [0] * (N + 1)
order = []
stack = [root]
while stack:
    x = stack.pop()
    order.append(x)
    for y in graph[x]:
        if y == parent[x]:
            continue
        parent[y] = x
        stack.append(y)
dp = [0] * (N + 1)
for v in order[::-1]:
    p = parent[v]
    A = [dp[c] for c in graph[v] if c != p]
    if not A:
        continue
    A.sort(reverse=True)
    A = [x + i for i, x in enumerate(A, 1)]
    dp[v] = max(A)
answer = dp[1]
print(answer)

import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


def dfs_order(G, root=1):
    parent = [0] * (N + 1)
    order = []
    stack = [root]
    while stack:
        x = stack.pop()
        order.append(x)
        for y in G[x]:
            if y == parent[x]:
                continue
            parent[y] = x
            stack.append(y)
    return parent, order


N, *A = list(map(int, read().split()))
G = [[] for _ in range(N + 1)]
for i, x in enumerate(A, 2):
    G[x].append(i)
    G[i].append(x)
parent, order = dfs_order(G)
dp = [0] * (N + 1)
for v in order[::-1]:
    p = parent[v]
    nums = [dp[w] for w in G[v] if w != p]
    if not nums:
        continue
    nums.sort()
    n = len(nums)
    for i in range(n):
        nums[i] += n - i
    dp[v] = max(nums)
print((dp[1]))

-N, *P = list(map(int, read().split())) -graph = [[] for _ in range(N + 1)] -for i, x in enumerate(P, 2): -    graph[i].append(x) -    graph[x].append(i) -root = 1 -parent = [0] * (N + 1) -order = [] -stack = [root] -while stack: -    x = stack.pop() -    order.append(x) -    for y in graph[x]: -        if y == parent[x]: -            continue -        parent[y] = x -        stack.append(y) + + +def dfs_order(G, root=1): +    parent = [0] * (N + 1) +    order = [] +    stack = [root] +    while stack: +        x = stack.pop() +        order.append(x) +        for y in G[x]: +            if y == parent[x]: +                continue +            parent[y] = x +            stack.append(y) +    return parent, order + + +N, *A = list(map(int, read().split())) +G = [[] for _ in range(N + 1)] +for i, x in enumerate(A, 2): +    G[x].append(i) +    G[i].append(x) +parent, order = dfs_order(G) -    A = [dp[c] for c in graph[v] if c != p] -    if not A: +    nums = [dp[w] for w in G[v] if w != p] +    if not nums: -    A.sort(reverse=True) -    A = [x + i for i, x in enumerate(A, 1)] -    dp[v] = max(A) -answer = dp[1] -print(answer) +    nums.sort() +    n = len(nums) +    for i in range(n): +        nums[i] += n - i +    dp[v] = max(nums) +print((dp[1]))