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: # https://atcoder.jp/contests/dp/tasks/dp_g
  import sys
  
  input = sys.stdin.readline
  
  N, M = [int(x) for x in input().split()]
  
  # メモ化再帰版
  # E = [[] for _ in range(N)]
  # for i in range(M):
  #     e = tuple([int(x) for x in input().split()])
  #     E[e[0] - 1].append(e[1] - 1)
  #
  # dp = [-1] * N
  # def rec(v):
  #     if dp[v] != -1:
  #         return dp[v]
  #
  #     res = 0
  #     for n in E[v]:
  #         res = max(res, rec(n) + 1)
  #     dp[v] = res
  #     return res
  #
  # res = 0
  # for n in range(N):
  #     res = max(res, rec(n))
  #
  # print(res)
  
  # BFS版
  from collections import deque
  
  
  deg = [0] * N  # 各頂点の入力数
  dp = [0] * N  # 各頂点のスタートからの距離
  
  E = [[] for _ in range(N)]
  for i in range(M):
      e = tuple([int(x) - 1 for x in input().split()])
      E[e[0]].append(e[1])
      deg[e[1]] += 1
  
  que = deque([i for i, v in enumerate(deg) if v == 0])
  
  while len(que) > 0:
      v = que.popleft()
      for to in E[v]:
          deg[to] -= 1
          if deg[to] == 0:
              que.append(to)
              dp[to] = max(dp[to], dp[v] + 1)
  
  print(max(dp))
  
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)"}]