import sys

sys.setrecursionlimit(100000)

N, M = list(map(int, input().split()))

edge = [[] for _ in range(N)]

for i in range(M):

    a,b = list(map(int, input().split()))

    edge[a-1].append(b-1)

dpv = [-1]*N



def dp(v):

    if dpv[v] != -1:

        return dpv[v]

    if len(edge[v])==0:

        dpv[v]=0

        return 0

    res = 0

    for w in edge[v]:

        res = max(dp(w),res)

    res += 1

    dpv[v] = res

    return res



def solve():

    for i in range(N):

        dp(i)

    ans = max(dpv)

    # print(dpv)

    return ans

print((solve()))
import sys

sys.setrecursionlimit(10**8)



N, M = list(map(int, input().split()))

edge = [[] for _ in range(N)]

for i in range(M):

  a,b = list(map(int, input().split()))

  edge[a-1].append(b-1)



def dfs(v):

  if len(edge[v])==0:

    return 0

  ans = 0

  for u in edge[v]:

    if longest[u]==-1:

      longest[u]=dfs(u)

    ans = max(ans,longest[u]+1)

  return ans



longest = [-1]*N

for i in range(N):

  if longest[i]==-1:

    longest[i] = dfs(i)

ans = max(longest)

print(ans)
import sys

sys.setrecursionlimit(100000)
N, M = list(map(int, input().split()))
edge = [[] for _ in range(N)]
for i in range(M):
    a, b = list(map(int, input().split()))
    edge[a - 1].append(b - 1)
dpv = [-1] * N


def dp(v):
    if dpv[v] != -1:
        return dpv[v]
    if len(edge[v]) == 0:
        dpv[v] = 0
        return 0
    res = 0
    for w in edge[v]:
        res = max(dp(w), res)
    res += 1
    dpv[v] = res
    return res


def solve():
    for i in range(N):
        dp(i)
    ans = max(dpv)
    # print(dpv)
    return ans


print((solve()))

import sys

sys.setrecursionlimit(10**8)
N, M = list(map(int, input().split()))
edge = [[] for _ in range(N)]
for i in range(M):
    a, b = list(map(int, input().split()))
    edge[a - 1].append(b - 1)


def dfs(v):
    if len(edge[v]) == 0:
        return 0
    ans = 0
    for u in edge[v]:
        if longest[u] == -1:
            longest[u] = dfs(u)
        ans = max(ans, longest[u] + 1)
    return ans


longest = [-1] * N
for i in range(N):
    if longest[i] == -1:
        longest[i] = dfs(i)
ans = max(longest)
print(ans)

-sys.setrecursionlimit(100000) +sys.setrecursionlimit(10**8) -dpv = [-1] * N -def dp(v): -    if dpv[v] != -1: -        return dpv[v] +def dfs(v): -        dpv[v] = 0 -    res = 0 -    for w in edge[v]: -        res = max(dp(w), res) -    res += 1 -    dpv[v] = res -    return res - - -def solve(): -    for i in range(N): -        dp(i) -    ans = max(dpv) -    # print(dpv) +    ans = 0 +    for u in edge[v]: +        if longest[u] == -1: +            longest[u] = dfs(u) +        ans = max(ans, longest[u] + 1) -print((solve())) +longest = [-1] * N +for i in range(N): +    if longest[i] == -1: +        longest[i] = dfs(i) +ans = max(longest) +print(ans)