output_description: Print the minimum possible depth of the tournament.
  
  * * *
input_description: The input is given from Standard Input in the following format:
  
      
      
      N
      a_2
      :
      a_N
attempt: import sys
  import math
  from collections import defaultdict
  
  sys.setrecursionlimit(10**7)
  
  
  def input():
      return sys.stdin.readline()[:-1]
  
  
  mod = 10**9 + 7
  
  
  def I():
      return int(input())
  
  
  def II():
      return map(int, input().split())
  
  
  def III():
      return list(map(int, input().split()))
  
  
  def Line(N, num):
      if N <= 0:
          return [[]] * num
      elif num == 1:
          return [I() for _ in range(N)]
      else:
          read_all = [tuple(II()) for _ in range(N)]
          return map(list, zip(*read_all))
  
  
  #################
  
  
  def bfs(pt, v):
      n = len(pt)
      d = [-1] * n
      d[v] = 0
      q = [v]
      c = 1
      while q:
          q1 = []
          for i in q:
              for j in pt[i]:
                  if d[j] == -1:
                      d[j] = c
                      q1.append(j)
          q = q1
          c += 1
      return d
  
  
  def edges_to_pt(s, t, n, directed=False):
      pt = [[] for _ in range(n)]
      for i in range(len(s)):
          pt[s[i] - 1].append(t[i] - 1)
          if directed == False:
              pt[t[i] - 1].append(s[i] - 1)
      return pt
  
  
  N = I()
  a = Line(N - 1, 1)
  
  s = [0] * (N - 1)
  t = [0] * (N - 1)
  for i in range(N - 1):
      s[i] = a[i]
      t[i] = i + 2
  
  pt = edges_to_pt(s, t, N, True)
  d = bfs(pt, 0)
  
  for i in range(N):
      d[i] += len(pt[i])
  
  print(max(d))
  
problem_description: Statement
  
  N contestants participated in a competition. The total of N-1 matches were
  played in a knockout tournament. For some reasons, the tournament may not be
  "fair" for all the contestants. That is, the number of the matches that must
  be played in order to win the championship may be different for each
  contestant. The structure of the tournament is formally described at the end
  of this statement.
  
  After each match, there were always one winner and one loser. The last
  contestant standing was declared the champion.
  
  ![](https://atcoder.jp/img/agc009/783f7be9c88350e31963edba8a958879.png)
  
  Figure: an example of a tournament
  
  For convenience, the contestants were numbered 1 through N. The contestant
  numbered 1 was the champion, and the contestant numbered i(2 ≦ i ≦ N) was
  defeated in a match against the contestant numbered a_i.
  
  We will define the _depth_ of the tournament as the maximum number of the
  matches that must be played in order to win the championship over all the
  contestants.
  
  Find the minimum possible depth of the tournament.
  
  The formal description of the structure of the tournament is as follows. In
  the i-th match, one of the following played against each other:
  
    * Two predetermined contestants
    * One predetermined contestant and the winner of the j-th match, where j(j<i) was predetermined
    * The winner of the j-th match and the winner of the k-th match, where j and k (j,k<i, j ≠ k) were predetermined
  
  Such structure is valid structure of the tournament, if and only if no
  contestant who has already been defeated in a match will never play in a
  match, regardless of the outcome of the matches.
samples: [{"input": "5\n    1\n    1\n    2\n    4", "output": "3\n    \n\nThe following tournament and the result of the matches are consistent with the\ngiven information:\n\n  * In the first match, contestants 4 and 5 played against each other, and contestant 4 won.\n  * In the second match, contestants 2 and 4 played against each other, and contestant 2 won.\n  * In the third match, contestants 1 and 3 played against each other, and contestant 1 won.\n  * In the fourth match, contestants 1 and 2 played against each other, and contestant 1 won.\n\n![783f7be9c88350e31963edba8a958879.png](https://atcoder.jp/img/agc009/783f7be9c88350e31963edba8a958879.png)\n\nThe depth of this tournament is 3, since contestant 5 must play three matches\nin order to win the championship. There is no tournament with depth 2 or less\nthat is consistent with the given information, thus the output should be 3.\n\n* * *"}, {"input": "7\n    1\n    2\n    1\n    3\n    1\n    4", "output": "3\n    \n\n* * *"}, {"input": "4\n    4\n    4\n    1", "output": "3"}]