CodeEff/ECCO huggingface dataset:
# Original solution
import sys
sys.setrecursionlimit(10 ** 9)

input = sys.stdin.readline

N = int(eval(input()))

G =[[] for _ in range(N + 1)] #iさんが勝った相手のリストを要素とするリスト
for i in range(2, N + 1):
    a = int(eval(input()))
    G[a].append(i)

# print (G)

set_lst = [set() for _ in range(N + 1)] #使った数字を管理する
tmp_lst = [0] * (N + 1) #次に使える最小を管理する

# print (set_lst)
# print (tmp_lst)

def calc(x): #xからの移動先を検討
    tmp = 0 #何試合目に行うことができるか
    for next_ in G[x]:
        y = max(calc(next_), tmp_lst[x])
        while y in set_lst[x]: #既に使われていたとき、一つずつ更新する
            y += 1
        set_lst[x].add(y) #新しく使ったことを記録する
        if y == tmp_lst[x]:
            tmp_lst[x] = y + 1
        tmp = max(y, tmp)
    return tmp + 1

ans = calc(1) - 1

# print (set_lst)
# print (tmp_lst)
print (ans)

# Optimized solution
import sys
sys.setrecursionlimit(10 ** 9)

input = sys.stdin.readline

N = int(eval(input()))

G =[[] for _ in range(N + 1)] #iさんが勝った相手のリストを要素とするリスト
for i in range(2, N + 1):
    a = int(eval(input()))
    G[a].append(i)

# print (G)

def calc(x): #xからの移動先を検討
    tmp = 0
    lst = []
    for next_ in G[x]:
        y = calc(next_)
        lst.append(y)
    lst.sort()
    for i in lst:
        if tmp >= i:
            tmp += 1
        else: #tmp < i
            tmp = i
    return tmp + 1

ans = calc(1) - 1

print (ans)
# Original solution
def main():

    import sys
    sys.setrecursionlimit(10 ** 9)

    input = sys.stdin.readline

    N = int(eval(input()))

    G =[[] for _ in range(N + 1)] #iさんが勝った相手のリストを要素とするリスト
    for i in range(2, N + 1):
        a = int(eval(input()))
        G[a].append(i)

    # print (G)

    set_lst = [set() for _ in range(N + 1)] #使った数字を管理する
    tmp_lst = [0] * (N + 1) #次に使える最小を管理する

    # print (set_lst)
    # print (tmp_lst)

    def calc(x): #xからの移動先を検討
        tmp = 0 #何試合目に行うことができるか
        for next_ in G[x]:
            y = max(calc(next_), tmp_lst[x])
            while y in set_lst[x]: #既に使われていたとき、一つずつ更新する
                y += 1
            set_lst[x].add(y) #新しく使ったことを記録する
            if y == tmp_lst[x]:
                tmp_lst[x] = y + 1
            tmp = max(y, tmp)
        return tmp + 1

    ans = calc(1) - 1

    # print (set_lst)
    # print (tmp_lst)
    print (ans)

if __name__ == '__main__':
    main()

# Optimized solution
def main():
    import sys
    sys.setrecursionlimit(10 ** 9)

    input = sys.stdin.readline

    N = int(eval(input()))

    G =[[] for _ in range(N + 1)] #iさんが勝った相手のリストを要素とするリスト
    for i in range(2, N + 1):
        a = int(eval(input()))
        G[a].append(i)

    # print (G)

    def calc(x): #xからの移動先を検討
        tmp = 0
        lst = []
        for next_ in G[x]:
            y = calc(next_)
            lst.append(y)
        lst.sort()
        for i in lst:
            if tmp >= i:
                tmp += 1
            else: #tmp < i
                tmp = i
        return tmp + 1

    ans = calc(1) - 1

    print (ans)

if __name__ == '__main__':
    main()
# Original solution
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

n = int(eval(input()))
rev_g = [[] for _ in range(n+1)]
for i in range(n-1):
    a = int(eval(input()))
    rev_g[a].append(i+2)

def f(v):
    m = len(rev_g[v])
    if m == 0:
        return 0
    q = []
    for nv in rev_g[v]:
        q.append(f(nv))
    q.sort()
    dp = [0] * (m+1)
    for i in range(m):
        dp[i+1] = max(q[i]+1, dp[i]+1)
    return dp[m]


print((f(1)))


# Optimized solution
import sys
sys.setrecursionlimit(10**7)

n = int(eval(input()))
rev_g = [[] for _ in range(n+1)]
for i in range(n-1):
    a = int(eval(input()))
    rev_g[a].append(i+2)

def f(v):
    m = len(rev_g[v])
    if m == 0:
        return 0
    q = []
    for nv in rev_g[v]:
        q.append(f(nv))
    q.sort()
    dp = [0] * (m+1)
    for i in range(m):
        dp[i+1] = max(q[i]+1, dp[i]+1)
    return dp[m]


print((f(1)))
# Original solution
import sys

limit = 10 ** 5 * 2
sys.setrecursionlimit(limit)

def calc_depth(n):
    if children[n] == set():
        return 0

    depth_l = [calc_depth(child) for child in children[n]]
    depth_l.sort(reverse=True)

    return max(depth + (i+1) for (i, depth) in enumerate(depth_l))

N = int(eval(input()))

children = [set() for i in range(N + 1)]

for i in range(N - 1):
    a = int(eval(input()))
    children[a].add(i + 2)

ans = calc_depth(1)

# print(children)

print(ans)

# Optimized solution
from sys import stdin, setrecursionlimit

setrecursionlimit(10**7)

def solve():
    N = int(stdin.readline())
    Adj = [[] for i in range(N)]

    for i in range(N - 1):
        a = int(stdin.readline()) - 1
        Adj[a].append(i + 1)

    ans = dfs(N, Adj, 0, -1)

    print(ans)

def dfs(N, Adj, v, p):
    dl = []

    for u in Adj[v]:
        if u == p:
            continue

        dl.append(dfs(N, Adj, u, v))

    dl.sort(reverse=True)

    if not dl:
        return 0

    res = max(dl[i] + i + 1 for i in range(len(dl)))

    return res

if __name__ == '__main__':
    solve()
# Original solution
import sys
sys.setrecursionlimit(10**5)
from collections import defaultdict

n = int(eval(input()))
e = [[] for i in range(n)]
for i in range(1,n):
    x = int(eval(input()))
    e[x-1].append(i)
num = {}
def dfs(x):
    if x in num:
        return num[x]
    count = len(e[x])
    if count == 0:
        return 0
    d = defaultdict(int)
    ma = 0
    for nex in e[x]:
        v = dfs(nex)
        d[v] += 1
        ma = max(ma,v)
    now = count
    bef = 0
    for v in range(ma,-1,-1):
        if d[v] > 0:
            bef += d[v]
            now = max(now,bef+v)
    num[x] = now
    return now

print((dfs(0)))

# Optimized solution
import sys
sys.setrecursionlimit(10**5)
from collections import defaultdict

n = int(eval(input()))
e = [[] for i in range(n)]
for i in range(1,n):
    x = int(eval(input()))
    e[x-1].append(i)

def dfs(x):

    count = len(e[x])
    if count == 0:
        return 0
    d = defaultdict(int)
    ma = 0
    s = set()
    for nex in e[x]:
        v = dfs(nex)
        d[v] += 1
        s.add(v)
        ma = max(ma,v)
    now = count
    bef = 0
    for v in sorted(list(s),reverse=True):
        bef += d[v]
        now = max(now,bef+v)
    return now

print((dfs(0)))
# Original solution
import sys
sys.setrecursionlimit(10**5)
from collections import defaultdict

n = int(eval(input()))
e = [[] for i in range(n)]
for i in range(1,n):
    x = int(eval(input()))
    e[x-1].append(i)

def dfs(x):

    count = len(e[x])
    if count == 0:
        return 0
    d = defaultdict(int)
    ma = 0
    s = set()
    for nex in e[x]:
        v = dfs(nex)
        d[v] += 1
        s.add(v)
        ma = max(ma,v)
    now = count
    bef = 0
    for v in sorted(list(s),reverse=True):
        bef += d[v]
        now = max(now,bef+v)
    return now

print((dfs(0)))

# Optimized solution
import sys
sys.setrecursionlimit(10**5)

n = int(eval(input()))
e = [[] for i in range(n)]
for i in range(1,n):
    x = int(eval(input()))
    e[x-1].append(i)

def dfs(x):
    count = len(e[x])
    if count == 0:
        return 0
    l = []
    for nex in e[x]:
        l.append(dfs(nex))
    l.sort(reverse=True)
    now = count
    for i in range(count):
        now = max(now,l[i]+i+1)
    return now
print((dfs(0)))
# Original solution
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import *
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list([int(x)-1 for x in input().split()])
def II(): return int(eval(input()))
def IF(): return float(eval(input()))
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    def dfs(x):
        if dp[x] is not None: return dp[x]
        dx = d[x]
        for e in edg[x]:
            dx.append(dfs(e))
        if dx == []:
            dp[x] = 0
            return 0
        dp[x] = max(1 + i + v for i, v in enumerate(sorted(dx, reverse=1)))
        return dp[x]
    
    n = II()
    edg = [[] for _ in range(n)]
    dp = [None] * n
    d = [[] for _ in range(n)]
    for i in range(n - 1):
        a = II() - 1
        edg[a].append(i + 1)
    print((dfs(0)))
    return


#main
if __name__ == '__main__':
    solve()


# Optimized solution
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import *
from bisect import bisect_left, bisect_right
import sys
sys.setrecursionlimit(2147483647)
input = sys.stdin.readline
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list([int(x)-1 for x in input().split()])
def II(): return int(eval(input()))
def IF(): return float(eval(input()))
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10
input=lambda:sys.stdin.readline().rstrip()
#solve
def solve():

    n = int(eval(input()))
    edg = [[] for _ in range(n)]
    dp = [None] * n
    for i in range(1,n):
        u = int(eval(input())) - 1
        edg[u].append(i)
        edg[i].append(u)

    def dfs(v,p=-1)->int:
        if (dp[v] is not None): return dp[v]
        if (edg[v] == [p]):
            dp[v] = 0
            return 0
        tmp = []
        for nv in edg[v]:
            if (nv == p): continue
            tmp.append(dfs(nv,v))
        dp[v] = max(1 + i + x for i, x in enumerate(sorted(tmp, reverse=1)))
        return dp[v]
    
    print((dfs(0)))
    return


#main
if __name__ == '__main__':
    solve()
# Original solution
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n = int(eval(input()))
a = [None]*(n-1)
from collections import defaultdict
c = defaultdict(list)
for i in range(n-1):
    num = int(eval(input()))
    c[num].append(i+2)

def sub(winner):
    l = len(c[winner])
    vs = []
    for p in c[winner]:
        vs.append(sub(p))
    vs.sort()
    ans = max([l] + [(l-i+v) for i,v in enumerate(vs)])
    return ans
ans = sub(1)
print(ans)

# Optimized solution
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")

n = int(eval(input()))
a = [None]*(n-1)
from collections import defaultdict
c = defaultdict(list)
for i in range(n-1):
    num = int(eval(input()))
    c[num].append(i+2)

### 行きがけ順DFS
def dfs(start):
    q = [start] # now, prev
    us = []
    seen = set([start])
    while q:
        u = q.pop()
        us.append(u)
        for v in c[u]:
            if v in seen:
                continue
            q.append(v)
            seen.add(v)
    return us

us = dfs(1)
val = {}
for winner in us[::-1]:
    l = len(c[winner])
    vs = []
    for p in c[winner]:
        vs.append(val[p])
    vs.sort()
    ans = max([l] + [(l-i+v) for i,v in enumerate(vs)])
    val[winner] = ans
ans = val[1]
print(ans)
# Original solution
import sys
sys.setrecursionlimit(10 ** 6)

N, *A = list(map(int, open(0)))

E = [[] for _ in [0] + A]
for i, v in enumerate(A, 1):
    E[v - 1] += i,

def dfs(v):
    D = sorted(map(dfs, E[v]))
    l = len(D)
    return max([0] + [l - i + d for i, d in enumerate(D)])

print((dfs(0)))


# Optimized solution
import sys
sys.setrecursionlimit(10 ** 6)

N, *A = list(map(int, open(0)))

E = [[] for _ in range(N + 1)]
for i, v in enumerate(A, 1):
    E[v - 1].append(i)

def dfs(v):
    D = sorted(map(dfs, E[v]))
    return max([0] + [len(D) - i + d for i, d in enumerate(D)])

print((dfs(0)))
# Original solution
import sys
from functools import lru_cache
sys.setrecursionlimit(1000000)
input=sys.stdin.readline

N = int(eval(input()))
par = {i:[] for i in range(1, N+1)}
for i in range(2, N+1):
    a = int(eval(input()))
    par[a].append(i)

@lru_cache(maxsize=None)
def dfs(s):
    depth = 1
    res = sorted(dfs(t) for t in par[s])[::-1]
    for i, j in enumerate(res, start=1):
        depth = max(depth, i+j)
    return depth

print((dfs(1)-1))

# Optimized solution
import sys
from collections import defaultdict
input=sys.stdin.readline


def solve():
    N = int(eval(input()))
    par = {i:[] for i in range(1, N+1)}

    for i in range(2, N+1):
        a = int(eval(input()))
        par[a].append(i)

    q = [1]
    l = []
    while q:
        s = q.pop()
        l.append(s)
        for t in par[s]:
            q.append(t)

    dp = defaultdict(int)
    for s in l[::-1]:
        depth = 1
        res = []
        for t in par[s]:
            res.append(dp[t])
        res.sort(reverse=True)
        for i, j in enumerate(res, start=1):
            depth = max(depth, i+j)
        dp[s] = depth

    print((dp[1]-1))


if __name__ == "__main__":
    solve()
# Original solution
import sys
from collections import defaultdict
input=sys.stdin.readline


def solve():
    N = int(eval(input()))
    par = {i:[] for i in range(1, N+1)}

    for i in range(2, N+1):
        a = int(eval(input()))
        par[a].append(i)

    q = [1]
    l = []
    while q:
        s = q.pop()
        l.append(s)
        for t in par[s]:
            q.append(t)

    dp = defaultdict(int)
    for s in l[::-1]:
        depth = 1
        res = [dp[t] for t in par[s]]
        res.sort(reverse=True)
        for i, j in enumerate(res, start=1):
            depth = max(depth, i+j)
        dp[s] = depth

    print((dp[1]-1))


if __name__ == "__main__":
    solve()


# Optimized solution
import sys
input=sys.stdin.readline


def solve():
    N = int(eval(input()))
    par = {i:[] for i in range(1, N+1)}
    q = [1]
    l = []
    dp = [0] * (N+1)

    for i in range(2, N+1):
        a = int(eval(input()))
        par[a].append(i)

    while q:
        s = q.pop()
        l.append(s)
        for t in par[s]:
            q.append(t)

    for s in l[::-1]:
        depth = 1
        res = sorted([dp[t] for t in par[s]])[::-1]
        for i, j in enumerate(res):
            depth = max(depth, i+j+1)
        dp[s] = depth

    print((dp[1]-1))


if __name__ == "__main__":
    solve()
# Original solution
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    NI = lambda : int(sys.stdin.readline())

    N = NI()
    edge = [[] for _ in range(N+1)]
    for i in range(2,N+1):
        u = NI()
        edge[u].append(i)

    def depth(u):
        ret = 0
        d = []
        for v in edge[u]:
            d.append(depth(v))
        d.sort(reverse=True)
        for i,x in enumerate(d):
            ret = max(ret,i+x+1)
        return ret

    ans = depth(1)
    print(ans)

if __name__ == '__main__':
    main()

# Optimized solution
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    NI = lambda : int(sys.stdin.readline())

    N = NI()
    edge = [[] for _ in range(N+1)]
    cnt = [0] * (N+1)
    for i in range(2,N+1):
        u = NI()
        edge[i].append(u)
        cnt[u] += 1

    q = collections.deque()
    for i in range(2,N+1):
        if cnt[i] == 0:
            q.append((i,0))
            cnt[i] = 1


    out = [[] for _ in range(N+1)]
    ret = 0
    while q:
        u,d = q.popleft()
        out[u].append(d)
        if len(out[u]) == cnt[u]:
            out[u].sort(reverse=True)
            ret = 0
            for i,x in enumerate(out[u]):
                ret = max(ret,i+x+1)
            if u == 1: break
            for v in edge[u]:
                q.append((v,ret))


    print((ret-1))

if __name__ == '__main__':
    main()
# Original solution
n=int(eval(input()))
a=[int(eval(input())) for _ in range(n-1)]
ki=[[] for _ in range(n)]
for i,x in enumerate(a):
  ki[x-1].append(i+1)

ans=0
import sys
sys.setrecursionlimit(10**7)
from collections import Counter
def marge(arg):
  ret=0
  ca=dict(Counter(arg))
  m=max(ca.keys())
  for i in range(1,m+1):
    if i not in ca:
      ca[i]=0
  while True:
    for i in range(1,m+1):
      if i==1 and i in ca and ca[i]:
        ca[i]-=1
      elif i>1:
        if i-1 in ca:
          ca[i-1]+=ca[i]
        else:
          ca[i-1]=ca[i]
        ca[i]=0
        if i==m:m-=1
    ret+=1
    if i==1:
      return ret+ca[1]

def dfs(v):
  ret=[]
  if len(ki[v])==0:return 1
  for nv in ki[v]:
    ret.append(dfs(nv))
  return marge(ret)+1
print((dfs(0)-1))


# Optimized solution
n=int(eval(input()))
a=[int(eval(input())) for _ in range(n-1)]
ki=[[] for _ in range(n)]
for i,x in enumerate(a):
  ki[x-1].append(i+1)

ans=0
import sys
sys.setrecursionlimit(10**7)
from collections import Counter
def marge(arg):
  ret=0
  arg.sort(reverse=True)
  for i,x in enumerate(arg):
    ret=max(ret,i+1+x)
  return ret


def dfs(v):
  ret=[]
  if len(ki[v])==0:return 0
  for nv in ki[v]:
    ret.append(dfs(nv))
  return marge(ret)
print((dfs(0)))
# Original solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

import heapq

n = int(eval(input()))
T = [[] for _ in range(n)]
for i in range(1, n):
  a = int(eval(input()))
  a -= 1
  T[a].append(i)

def dfs(v):
  if not T[v]:
    return 0
  L = []
  for i in T[v]:
    heapq.heappush(L, dfs(i)+1)
  temp = heapq.heappop(L)
  for _ in range(len(T[v])-1):
    temp = max(temp+1, heapq.heappop(L))
  return temp

ans = dfs(0)
print(ans)

# Optimized solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

import heapq

def main():
  n = int(eval(input()))
  T = [[] for _ in range(n)]
  for i in range(1, n):
    a = int(eval(input()))
    a -= 1
    T[a].append(i)

  def dfs(v):
    if not T[v]:
      return 0
    L = []
    for i in T[v]:
      heapq.heappush(L, dfs(i)+1)
    temp = heapq.heappop(L)
    for _ in range(len(T[v])-1):
      temp = max(temp+1, heapq.heappop(L))
    return temp

  ans = dfs(0)
  print(ans)
  
if __name__ == "__main__":
  main()
# Original solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

import heapq

def main():
  n = int(eval(input()))
  T = [[] for _ in range(n)]
  for i in range(1, n):
    a = int(eval(input()))
    a -= 1
    T[a].append(i)

  def dfs(v):
    if not T[v]:
      return 0
    L = []
    for i in T[v]:
      heapq.heappush(L, dfs(i)+1)
    temp = heapq.heappop(L)
    for _ in range(len(T[v])-1):
      temp = max(temp+1, heapq.heappop(L))
    return temp

  ans = dfs(0)
  print(ans)
  
if __name__ == "__main__":
  main()

# Optimized solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

import heapq

def main():
  n = int(eval(input()))
  T = [[] for _ in range(n)]
  for i in range(1, n):
    a = int(eval(input()))
    a -= 1
    T[a].append(i)

  def dfs(v):
    if not T[v]:
      return 0
    L = []
    for i in T[v]:
      heapq.heappush(L, dfs(i)+1)
    temp = heapq.heappop(L)
    while L:
      temp = max(temp+1, heapq.heappop(L))
    return temp

  ans = dfs(0)
  print(ans)
  
if __name__ == "__main__":
  main()
# Original solution
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)))

# Optimized solution
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)))
# Original solution
from collections import Counter

n = int(eval(input()))
xs = [None] + [int(eval(input()))-1 for _ in range(n-1)]

count_win = Counter(xs)
q = [i for i in range(n) if not i in count_win]
children = [[] for i in range(n)]

while not q[0]==0:
    i = q.pop(0)
    children[i].sort(reverse=True)
    children[i] = max(v+w for v, w in  enumerate([0]+children[i]))
    j = xs[i]
    children[j].append(children[i])
    if count_win[j] <= 1:
        q.append(j)
    else:
        count_win[j] -= 1

children[0].sort(reverse=True)
children[0] = max(v+w for v, w in  enumerate([0]+children[0]))

print((children[0]))

# Optimized solution
from collections import Counter

n = int(eval(input()))
xs = [None] + [int(eval(input()))-1 for _ in range(n-1)]

count_win = Counter(xs)
q = [i for i in range(n) if not i in count_win]
children = [[] for i in range(n)]

qi = 0
while not q[qi]==0:
    i, qi = q[qi], qi+1
    children[i].sort(reverse=True)
    children[i] = max(v+w for v, w in  enumerate([0]+children[i]))
    j = xs[i]
    children[j].append(children[i])
    if count_win[j] <= 1:
        q.append(j)
    else:
        count_win[j] -= 1

children[0].sort(reverse=True)
children[0] = max(v+w for v, w in  enumerate([0]+children[0]))

print((children[0]))
# Original solution
import collections, sys
sys.setrecursionlimit(10**5 + 5)
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)

memo = [-1] * (N + 1)


def rec(x):
    if memo[x] == -1:
        if win_lose[x] == set():
            memo[x] = 0
        else:
            memo[x] = max(a + b for a, b in zip(
                sorted(rec(y) for y in win_lose[x])[::-1], list(range(1, N + 1))))
    return memo[x]


print((rec(1)))


# Optimized solution
import collections
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]
memo = [0] * (N + 1)
for x in s2[::-1]:
    if win_lose[x] == set():
        memo[x] = 0
    else:
        memo[x] = max(a + b for a, b in zip(
            sorted(memo[y] for y in win_lose[x])[::-1], list(range(1, N + 1))))
print((memo[1]))
# Original solution
import collections
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]
memo = [0] * (N + 1)
for x in s2[::-1]:
    if win_lose[x] == set():
        memo[x] = 0
    else:
        memo[x] = max(a + b for a, b in zip(
            sorted(memo[y] for y in win_lose[x])[::-1], list(range(1, N + 1))))
print((memo[1]))


# Optimized solution
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]))