import sys

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines

sys.setrecursionlimit(10 ** 7)



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

outdeg = {i: [] for i in range(N)}

for _ in range(M):

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

    a -= 1

    b -= 1

    outdeg[a].append(b)



dp = [-1] * (N)





def rec(s):

    if dp[s] != -1:

        return dp[s]



    res = 0

    for t in outdeg[s]:

        res = max(res, rec(t)+1)

    dp[s] = res

    return res





ans = 0

for s in range(N):

    ans = max(ans, rec(s))

print(ans)

import sys

input = sys.stdin.buffer.readline

sys.setrecursionlimit(10 ** 7)





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

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

for _ in range(M):

    s, t = list(map(int, input().split()))

    s -= 1

    t -= 1

    edge[s].append(t)



dp = [-1]*N



def dfs(s):

    if dp[s] != -1:

        return dp[s]

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

        dp[s] = 0

        return 0

    res = 0

    for t in edge[s]:

        res = max(res, dfs(t)+1)

    dp[s] = res

    return res



for i in range(N):

    dfs(i)

print((max(dp)))

    
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**7)
N, M = list(map(int, readline().split()))
outdeg = {i: [] for i in range(N)}
for _ in range(M):
    a, b = list(map(int, readline().split()))
    a -= 1
    b -= 1
    outdeg[a].append(b)
dp = [-1] * (N)


def rec(s):
    if dp[s] != -1:
        return dp[s]
    res = 0
    for t in outdeg[s]:
        res = max(res, rec(t) + 1)
    dp[s] = res
    return res


ans = 0
for s in range(N):
    ans = max(ans, rec(s))
print(ans)

import sys

input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**7)
N, M = list(map(int, input().split()))
edge = [[] for _ in range(N)]
for _ in range(M):
    s, t = list(map(int, input().split()))
    s -= 1
    t -= 1
    edge[s].append(t)
dp = [-1] * N


def dfs(s):
    if dp[s] != -1:
        return dp[s]
    if len(edge[s]) == 0:
        dp[s] = 0
        return 0
    res = 0
    for t in edge[s]:
        res = max(res, dfs(t) + 1)
    dp[s] = res
    return res


for i in range(N):
    dfs(i)
print((max(dp)))

-read = sys.stdin.buffer.read -readline = sys.stdin.buffer.readline -readlines = sys.stdin.buffer.readlines +input = sys.stdin.buffer.readline -N, M = list(map(int, readline().split())) -outdeg = {i: [] for i in range(N)} +N, M = list(map(int, input().split())) +edge = [[] for _ in range(N)] -    a, b = list(map(int, readline().split())) -    a -= 1 -    b -= 1 -    outdeg[a].append(b) -dp = [-1] * (N) +    s, t = list(map(int, input().split())) +    s -= 1 +    t -= 1 +    edge[s].append(t) +dp = [-1] * N -def rec(s): +def dfs(s): +    if len(edge[s]) == 0: +        dp[s] = 0 +        return 0 -    for t in outdeg[s]: -        res = max(res, rec(t) + 1) +    for t in edge[s]: +        res = max(res, dfs(t) + 1) -ans = 0 -for s in range(N): -    ans = max(ans, rec(s)) -print(ans) +for i in range(N): +    dfs(i) +print((max(dp)))