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: # DAGの最長経路...トポロジカルソートもどきか？
  # 普通にDAGに沿ったdpをすればいいのでは?
  # 再帰だと実装楽そうだけどどうしよう
  # 問題は終了と最初がわからんこと
  # いや最初は全部長さ0をセットすればいいだろ
  import sys
  
  sys.setrecursionlimit(1 << 25)
  readline = sys.stdin.buffer.readline
  read = sys.stdin.readline  # 文字列読み込む時はこっち
  ra = range
  enu = enumerate
  
  
  def exit(*argv, **kwarg):
      print(*argv, **kwarg)
      sys.exit()
  
  
  def mina(*argv, sub=1):
      return list(map(lambda x: x - sub, argv))
  
  
  # 受け渡されたすべての要素からsubだけ引く.リストを*をつけて展開しておくこと
  
  
  def ints():
      return list(map(int, readline().split()))
  
  
  def read_col(H):
      """H is number of rows
      A列、B列が与えられるようなとき
      ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合"""
      ret = []
      for _ in range(H):
          ret.append(list(map(int, readline().split())))
      return tuple(map(list, zip(*ret)))
  
  
  MOD = 10**9 + 7
  INF = 2**31  # 2147483648 > 10**9
  # default import
  from collections import defaultdict, Counter, deque
  from operator import itemgetter, xor, add
  from itertools import product, permutations, combinations
  from bisect import bisect_left, bisect_right  # , insort_left, insort_right
  from functools import reduce
  
  N, M = ints()
  dag = defaultdict(lambda: [])
  indeg = [0] * N
  for _ in ra(M):
      x, y = mina(*ints())
      indeg[y] += 1
      dag[x].append(y)
  
  q = deque()
  for u, inn in enu(indeg):  # 多点DFS初期化
      if inn == 0:
          q.append((u, 0))
  
  dp = [0] * N
  while q:  # トポロジカル順序でdpテーブルを埋めていく
      u, l = q.popleft()
      # dp[u] = max(dp[u], l)
      dp[u] = l
      for nx in dag[u]:
          indeg[nx] -= 1
          if indeg[nx] == 0:
              q.append((nx, l + 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)"}]