output_description: Print the length of the longest directed path in G.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N M
      x_1 y_1
      x_2 y_2
      :
      x_M y_M
attempt: from collections import deque
  
  N, M = map(int, input().split())
  to_nodes = [[] for _ in range(N)]  # 終点ノード
  deg = [0] * N  # 入力辺
  
  for _ in range(M):
      x, y = map(int, input().split())
      to_nodes[x - 1].append(y - 1)
      deg[y - 1] += 1
  
  st = deque()
  push, pop = st.append, st.pop
  
  for n in range(N):
      if deg[n] == 0:
          push(n)  # 入力辺が無く、最長パスの始点になりうる点を取得
  
  dp = [0] * N
  # print(deg,dp)
  while st:
      n = pop()
      nodes = to_nodes[n]
      for ni in nodes:
          deg[ni] -= 1  # 使ったエッジをグラフから削除する
          if deg[ni] == 0:
              push(ni)
              dp[ni] = max(dp[ni], dp[n] + 1)
          # print('n',n,'ni',ni,deg,dp)
  
  print(max(dp))
  
  """''
  @input
  4 5
  1 2
  1 3
  3 2
  2 4
  3 4
  @output
  [0, 2, 1, 2] [0, 0, 0, 0]
  n 0 ni 1 [0, 1, 1, 2] [0, 0, 0, 0]
  n 0 ni 2 [0, 1, 0, 2] [0, 0, 1, 0]
  n 2 ni 1 [0, 0, 0, 2] [0, 2, 1, 0]
  n 2 ni 3 [0, 0, 0, 1] [0, 2, 1, 0]
  n 1 ni 3 [0, 0, 0, 0] [0, 2, 1, 3]
  3
  """ ""
  
problem_description: Statement
  
  There is a directed graph G with N vertices and M edges. The vertices are
  numbered 1, 2, \ldots, N, and for each i (1 \leq i \leq M), the i-th directed
  edge goes from Vertex x_i to y_i. G **does not contain directed cycles**.
  
  Find the length of the longest directed path in G. Here, the length of a
  directed path is the number of edges in it.
samples: [{"input": "4 5\n    1 2\n    1 3\n    3 2\n    2 4\n    3 4", "output": "3\n    \n\nThe red directed path in the following figure is the longest:\n\n![](https://img.atcoder.jp/dp/longest_0_muffet.png)\n\n* * *"}, {"input": "6 3\n    2 3\n    4 5\n    5 6", "output": "2\n    \n\nThe red directed path in the following figure is the longest:\n\n![](https://img.atcoder.jp/dp/longest_1_muffet.png)\n\n* * *"}, {"input": "5 8\n    5 3\n    2 3\n    2 4\n    5 2\n    5 1\n    1 4\n    4 3\n    1 3", "output": "3\n    \n\nThe red directed path in the following figure is one of the longest:\n\n![](https://img.atcoder.jp/dp/longest_2_muffet.png)"}]