# python template for atcoder1

import sys

sys.setrecursionlimit(10**9)

input = sys.stdin.readline



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

edges = [set() for _ in range(N)]

ins = [0]*N

for _ in range(M):

    a, b = [int(x)-1 for x in input().split()]

    ins[b] += 1

    edges[a].add(b)



dp = [-1]*N



start = []

for i in range(N):

    if ins[i] == 0:

        start.append(i)





def dfs(s):

    if dp[s] != -1:

        return dp[s]

    mx = 0

    if edges[s]:

        mx = max([dfs(dest)+1 for dest in edges[s]])

    dp[s] = mx

    return mx





ans = -1

for i in range(N):

    if ins[i] == 0:

        ans = max(ans, dfs(i))



print(ans)

# python template for atcoder1

import sys

sys.setrecursionlimit(10**9)

input = sys.stdin.readline



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

edges = [set() for _ in range(N)]

ins = [0]*N

for _ in range(M):

    a, b = [int(x)-1 for x in input().split()]

    ins[b] += 1

    edges[a].add(b)



dp = [-1]*N





def dfs(s):

    if dp[s] != -1:

        return dp[s]

    mx = 0

    if edges[s]:

        mx = max([dfs(dest)+1 for dest in edges[s]])

    dp[s] = mx

    return mx





ans = -1

for i in range(N):

    ans = max(ans, dfs(i))



print(ans)

# python template for atcoder1
import sys

sys.setrecursionlimit(10**9)
input = sys.stdin.readline
N, M = list(map(int, input().split()))
edges = [set() for _ in range(N)]
ins = [0] * N
for _ in range(M):
    a, b = [int(x) - 1 for x in input().split()]
    ins[b] += 1
    edges[a].add(b)
dp = [-1] * N
start = []
for i in range(N):
    if ins[i] == 0:
        start.append(i)


def dfs(s):
    if dp[s] != -1:
        return dp[s]
    mx = 0
    if edges[s]:
        mx = max([dfs(dest) + 1 for dest in edges[s]])
    dp[s] = mx
    return mx


ans = -1
for i in range(N):
    if ins[i] == 0:
        ans = max(ans, dfs(i))
print(ans)

# python template for atcoder1
import sys

sys.setrecursionlimit(10**9)
input = sys.stdin.readline
N, M = list(map(int, input().split()))
edges = [set() for _ in range(N)]
ins = [0] * N
for _ in range(M):
    a, b = [int(x) - 1 for x in input().split()]
    ins[b] += 1
    edges[a].add(b)
dp = [-1] * N


def dfs(s):
    if dp[s] != -1:
        return dp[s]
    mx = 0
    if edges[s]:
        mx = max([dfs(dest) + 1 for dest in edges[s]])
    dp[s] = mx
    return mx


ans = -1
for i in range(N):
    ans = max(ans, dfs(i))
print(ans)

-start = [] -for i in range(N): -    if ins[i] == 0: -        start.append(i) -    if ins[i] == 0: -        ans = max(ans, dfs(i)) +    ans = max(ans, dfs(i))