import sys

input = sys.stdin.readline

sys.setrecursionlimit(10**6)



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

ad = [[] for i in range(N)]

for _ in range(M):

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

    x -= 1; y -= 1

    ad[x].append(y)



dp = [-1]*N



def rec(nodes):

    if dp[nodes] != -1:

        return dp[nodes]

    res = 0

    for ne in ad[nodes]:

        option = rec(ne)+1

        if res < option:

            res = option

    dp[nodes] = res

    return dp[nodes]



ans = 0

for i in range(N):

    res = rec(i)

    if ans < res:

        ans = res

print(ans)
def main():

    import sys

    input = sys.stdin.readline

    sys.setrecursionlimit(10**6)



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

    ad = [[] for i in range(N)]

    for _ in range(M):

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

        x -= 1; y -= 1

        ad[x].append(y)



    dp = [-1]*N



    def rec(nodes):

        if dp[nodes] != -1:

            return dp[nodes]

        res = 0

        for ne in ad[nodes]:

            option = rec(ne)+1

            if res < option:

                res = option

        dp[nodes] = res

        return dp[nodes]



    ans = 0

    for i in range(N):

        res = rec(i)

        if ans < res:

            ans = res

    print(ans)

    

main()
import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**6)
N, M = list(map(int, input().split()))
ad = [[] for i in range(N)]
for _ in range(M):
    x, y = list(map(int, input().split()))
    x -= 1
    y -= 1
    ad[x].append(y)
dp = [-1] * N


def rec(nodes):
    if dp[nodes] != -1:
        return dp[nodes]
    res = 0
    for ne in ad[nodes]:
        option = rec(ne) + 1
        if res < option:
            res = option
    dp[nodes] = res
    return dp[nodes]


ans = 0
for i in range(N):
    res = rec(i)
    if ans < res:
        ans = res
print(ans)

def main():
    import sys

    input = sys.stdin.readline
    sys.setrecursionlimit(10**6)
    N, M = list(map(int, input().split()))
    ad = [[] for i in range(N)]
    for _ in range(M):
        x, y = list(map(int, input().split()))
        x -= 1
        y -= 1
        ad[x].append(y)
    dp = [-1] * N

    def rec(nodes):
        if dp[nodes] != -1:
            return dp[nodes]
        res = 0
        for ne in ad[nodes]:
            option = rec(ne) + 1
            if res < option:
                res = option
        dp[nodes] = res
        return dp[nodes]

    ans = 0
    for i in range(N):
        res = rec(i)
        if ans < res:
            ans = res
    print(ans)


main()

-import sys +def main(): +    import sys -input = sys.stdin.readline -sys.setrecursionlimit(10**6) -N, M = list(map(int, input().split())) -ad = [[] for i in range(N)] -for _ in range(M): -    x, y = list(map(int, input().split())) -    x -= 1 -    y -= 1 -    ad[x].append(y) -dp = [-1] * N +    input = sys.stdin.readline +    sys.setrecursionlimit(10**6) +    N, M = list(map(int, input().split())) +    ad = [[] for i in range(N)] +    for _ in range(M): +        x, y = list(map(int, input().split())) +        x -= 1 +        y -= 1 +        ad[x].append(y) +    dp = [-1] * N + +    def rec(nodes): +        if dp[nodes] != -1: +            return dp[nodes] +        res = 0 +        for ne in ad[nodes]: +            option = rec(ne) + 1 +            if res < option: +                res = option +        dp[nodes] = res +        return dp[nodes] + +    ans = 0 +    for i in range(N): +        res = rec(i) +        if ans < res: +            ans = res +    print(ans) -def rec(nodes): -    if dp[nodes] != -1: -        return dp[nodes] -    res = 0 -    for ne in ad[nodes]: -        option = rec(ne) + 1 -        if res < option: -            res = option -    dp[nodes] = res -    return dp[nodes] - - -ans = 0 -for i in range(N): -    res = rec(i) -    if ans < res: -        ans = res -print(ans) +main()