def main():

    import sys

    sys.setrecursionlimit(10**7)



    N,M=list(map(int,sys.stdin.readline().split()))

    dic=[[] for _ in range(N)]

    for i in range(M):

        x,y=list(map(int,sys.stdin.readline().split()))

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





    dp=[0]*(N)

    def dfs(frm):

        if dp[frm]:

            return dp[frm]



        mx=0

        if not dic[frm]:

            return 0



        for val in dic[frm]:

            mx=max(mx,dfs(val)+1)



        dp[frm]=mx

        return mx



    mx=0

    for key in range(N):

        mx=max(mx,dfs(key))



    return mx



print((main()))

def main():

    import sys

    sys.setrecursionlimit(10**7)



    N,M=list(map(int,sys.stdin.readline().split()))

    dic=[[] for _ in range(N)]

    for i in range(M):

        x,y=list(map(int,sys.stdin.readline().split()))

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





    dp=[0]*(N)

    def dfs(frm):

        if dp[frm]:

            return dp[frm]



        mx=0

        if not dic[frm]:

            return 0



        dp[frm]=max(dfs(val)+1 for val in dic[frm])



        return dp[frm]



    mx=0

    return max(dfs(x) for x in range(N))





print((main()))

def main():
    import sys

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

    def dfs(frm):
        if dp[frm]:
            return dp[frm]
        mx = 0
        if not dic[frm]:
            return 0
        for val in dic[frm]:
            mx = max(mx, dfs(val) + 1)
        dp[frm] = mx
        return mx

    mx = 0
    for key in range(N):
        mx = max(mx, dfs(key))
    return mx


print((main()))

def main():
    import sys

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

    def dfs(frm):
        if dp[frm]:
            return dp[frm]
        mx = 0
        if not dic[frm]:
            return 0
        dp[frm] = max(dfs(val) + 1 for val in dic[frm])
        return dp[frm]

    mx = 0
    return max(dfs(x) for x in range(N))


print((main()))

-        for val in dic[frm]: -            mx = max(mx, dfs(val) + 1) -        dp[frm] = mx -        return mx +        dp[frm] = max(dfs(val) + 1 for val in dic[frm]) +        return dp[frm] -    for key in range(N): -        mx = max(mx, dfs(key)) -    return mx +    return max(dfs(x) for x in range(N))