import sys

input = sys.stdin.readline



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

edges = [[] for _ in range(n)]

edges2 = [[] for _ in range(n)]



dis = [0] * n



for _ in range(m):

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

    edges[y-1].append(x-1)

    edges2[x-1].append(y-1)

    dis[x-1] = -1



q = []



for i in range(n):

    if dis[i] == 0:

        q.append(i)



while q:

    l = q.pop(0)

    for i in edges[l]:

        dis[i] = max(dis[i], dis[l] + 1)

        edges2[i].remove(l)

        if not edges2[i]:

            q.append(i)



print((max(dis)))

import sys

input = sys.stdin.readline



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

edges = [[] for _ in range(n)]

edges2 = [0] * n



dis = [0] * n



for _ in range(m):

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

    edges[y-1].append(x-1)

    edges2[x-1] += 1

    dis[x-1] = -1



q = []



for i in range(n):

    if dis[i] == 0:

        q.append(i)



while q:

    l = q.pop(0)

    for i in edges[l]:

        dis[i] = max(dis[i], dis[l] + 1)

        edges2[i] -= 1

        if edges2[i] == 0:

            q.append(i)



print((max(dis)))

import sys

input = sys.stdin.readline
n, m = list(map(int, input().split()))
edges = [[] for _ in range(n)]
edges2 = [[] for _ in range(n)]
dis = [0] * n
for _ in range(m):
    x, y = list(map(int, input().split()))
    edges[y - 1].append(x - 1)
    edges2[x - 1].append(y - 1)
    dis[x - 1] = -1
q = []
for i in range(n):
    if dis[i] == 0:
        q.append(i)
while q:
    l = q.pop(0)
    for i in edges[l]:
        dis[i] = max(dis[i], dis[l] + 1)
        edges2[i].remove(l)
        if not edges2[i]:
            q.append(i)
print((max(dis)))

import sys

input = sys.stdin.readline
n, m = list(map(int, input().split()))
edges = [[] for _ in range(n)]
edges2 = [0] * n
dis = [0] * n
for _ in range(m):
    x, y = list(map(int, input().split()))
    edges[y - 1].append(x - 1)
    edges2[x - 1] += 1
    dis[x - 1] = -1
q = []
for i in range(n):
    if dis[i] == 0:
        q.append(i)
while q:
    l = q.pop(0)
    for i in edges[l]:
        dis[i] = max(dis[i], dis[l] + 1)
        edges2[i] -= 1
        if edges2[i] == 0:
            q.append(i)
print((max(dis)))

-edges2 = [[] for _ in range(n)] +edges2 = [0] * n -    edges2[x - 1].append(y - 1) +    edges2[x - 1] += 1 -        edges2[i].remove(l) -        if not edges2[i]: +        edges2[i] -= 1 +        if edges2[i] == 0: