import sys



sys.setrecursionlimit(10 ** 7)



input = sys.stdin.readline



inf = float('inf')





def rec(v):

    if used[v]:

        return dp[v]

    used[v] = True

    res = 0

    for u in es[v]:

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

    dp[v] = res

    return res





n, m = list(map(int, input().split()))



es = tuple(set() for _ in range(n))

for _ in range(m):

    x, y = list(map(int, input().split()))

    x -= 1

    y -= 1

    es[x].add(y)



used = [False] * n

dp = [inf] * n



ans = 0

for i in range(n):

    ans = max(ans, rec(i))

print(ans)

import sys



sys.setrecursionlimit(10 ** 7)





def dfs(v):

    if d[v] != -1:

        return d[v]

    res = -1

    for u in e[v]:

        res = max(res, dfs(u))

    res += 1

    d[v] = res

    return res





n, m = list(map(int, input().split()))

e = tuple(set() for _ in range(n))

for _ in range(m):

    x, y = list(map(int, input().split()))

    x -= 1

    y -= 1

    e[x].add(y)



d = [-1] * n

res = -1

for v in range(n):

    res = max(res, dfs(v))

print(res)

import sys

sys.setrecursionlimit(10**7)
input = sys.stdin.readline
inf = float("inf")


def rec(v):
    if used[v]:
        return dp[v]
    used[v] = True
    res = 0
    for u in es[v]:
        res = max(res, rec(u) + 1)
    dp[v] = res
    return res


n, m = list(map(int, input().split()))
es = tuple(set() for _ in range(n))
for _ in range(m):
    x, y = list(map(int, input().split()))
    x -= 1
    y -= 1
    es[x].add(y)
used = [False] * n
dp = [inf] * n
ans = 0
for i in range(n):
    ans = max(ans, rec(i))
print(ans)

import sys

sys.setrecursionlimit(10**7)


def dfs(v):
    if d[v] != -1:
        return d[v]
    res = -1
    for u in e[v]:
        res = max(res, dfs(u))
    res += 1
    d[v] = res
    return res


n, m = list(map(int, input().split()))
e = tuple(set() for _ in range(n))
for _ in range(m):
    x, y = list(map(int, input().split()))
    x -= 1
    y -= 1
    e[x].add(y)
d = [-1] * n
res = -1
for v in range(n):
    res = max(res, dfs(v))
print(res)

-input = sys.stdin.readline -inf = float("inf") -def rec(v): -    if used[v]: -        return dp[v] -    used[v] = True -    res = 0 -    for u in es[v]: -        res = max(res, rec(u) + 1) -    dp[v] = res +def dfs(v): +    if d[v] != -1: +        return d[v] +    res = -1 +    for u in e[v]: +        res = max(res, dfs(u)) +    res += 1 +    d[v] = res -es = tuple(set() for _ in range(n)) +e = tuple(set() for _ in range(n)) -    es[x].add(y) -used = [False] * n -dp = [inf] * n -ans = 0 -for i in range(n): -    ans = max(ans, rec(i)) -print(ans) +    e[x].add(y) +d = [-1] * n +res = -1 +for v in range(n): +    res = max(res, dfs(v)) +print(res)