import sys

input = sys.stdin.readline

from collections import deque



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

dp = [-1]*N

ad = [deque() for _ in range(N)]

num = [0]*N

for _ in range(M):

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

    x -= 1; y -= 1

    ad[x].append(y)

    num[y] += 1



L = []

S = deque()



for v in range(N):

    if num[v] == 0:

        S.append(v)

        dp[v] = 0



while S:

    v = S.popleft()

    L.append(v)

    for _ in range(len(ad[v])):

        nv = ad[v].popleft()

        num[nv] -= 1

        if num[nv] == 0:

            S.append(nv)

            if dp[nv] < dp[v] + 1:

                dp[nv] = dp[v] + 1



ans = max(dp)

print(ans)
def main():

    import sys

    input = sys.stdin.readline

    from collections import deque



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

    dp = [-1]*N

    ad = [deque() for _ in range(N)]

    num = [0]*N

    for _ in range(M):

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

        x -= 1; y -= 1

        ad[x].append(y)

        num[y] += 1



    L = []

    S = deque()



    for v in range(N):

        if num[v] == 0:

            S.append(v)

            dp[v] = 0



    while S:

        v = S.popleft()

        L.append(v)

        for _ in range(len(ad[v])):

            nv = ad[v].popleft()

            num[nv] -= 1

            if num[nv] == 0:

                S.append(nv)

                if dp[nv] < dp[v] + 1:

                    dp[nv] = dp[v] + 1



    ans = max(dp)

    print(ans)



main()
import sys

input = sys.stdin.readline
from collections import deque

N, M = list(map(int, input().split()))
dp = [-1] * N
ad = [deque() for _ in range(N)]
num = [0] * N
for _ in range(M):
    x, y = list(map(int, input().split()))
    x -= 1
    y -= 1
    ad[x].append(y)
    num[y] += 1
L = []
S = deque()
for v in range(N):
    if num[v] == 0:
        S.append(v)
        dp[v] = 0
while S:
    v = S.popleft()
    L.append(v)
    for _ in range(len(ad[v])):
        nv = ad[v].popleft()
        num[nv] -= 1
        if num[nv] == 0:
            S.append(nv)
            if dp[nv] < dp[v] + 1:
                dp[nv] = dp[v] + 1
ans = max(dp)
print(ans)

def main():
    import sys

    input = sys.stdin.readline
    from collections import deque

    N, M = list(map(int, input().split()))
    dp = [-1] * N
    ad = [deque() for _ in range(N)]
    num = [0] * N
    for _ in range(M):
        x, y = list(map(int, input().split()))
        x -= 1
        y -= 1
        ad[x].append(y)
        num[y] += 1
    L = []
    S = deque()
    for v in range(N):
        if num[v] == 0:
            S.append(v)
            dp[v] = 0
    while S:
        v = S.popleft()
        L.append(v)
        for _ in range(len(ad[v])):
            nv = ad[v].popleft()
            num[nv] -= 1
            if num[nv] == 0:
                S.append(nv)
                if dp[nv] < dp[v] + 1:
                    dp[nv] = dp[v] + 1
    ans = max(dp)
    print(ans)


main()

-import sys +def main(): +    import sys -input = sys.stdin.readline -from collections import deque +    input = sys.stdin.readline +    from collections import deque -N, M = list(map(int, input().split())) -dp = [-1] * N -ad = [deque() for _ in range(N)] -num = [0] * N -for _ in range(M): -    x, y = list(map(int, input().split())) -    x -= 1 -    y -= 1 -    ad[x].append(y) -    num[y] += 1 -L = [] -S = deque() -for v in range(N): -    if num[v] == 0: -        S.append(v) -        dp[v] = 0 -while S: -    v = S.popleft() -    L.append(v) -    for _ in range(len(ad[v])): -        nv = ad[v].popleft() -        num[nv] -= 1 -        if num[nv] == 0: -            S.append(nv) -            if dp[nv] < dp[v] + 1: -                dp[nv] = dp[v] + 1 -ans = max(dp) -print(ans) +    N, M = list(map(int, input().split())) +    dp = [-1] * N +    ad = [deque() for _ in range(N)] +    num = [0] * N +    for _ in range(M): +        x, y = list(map(int, input().split())) +        x -= 1 +        y -= 1 +        ad[x].append(y) +        num[y] += 1 +    L = [] +    S = deque() +    for v in range(N): +        if num[v] == 0: +            S.append(v) +            dp[v] = 0 +    while S: +        v = S.popleft() +        L.append(v) +        for _ in range(len(ad[v])): +            nv = ad[v].popleft() +            num[nv] -= 1 +            if num[nv] == 0: +                S.append(nv) +                if dp[nv] < dp[v] + 1: +                    dp[nv] = dp[v] + 1 +    ans = max(dp) +    print(ans) + + +main()