import collections

n, m = [int(i) for i in input().split()]

todc = collections.defaultdict(lambda: [])

lendc = collections.defaultdict(lambda: -1)





for k in range(m):

    i, j = [int(n) for n in input().split()]

    todc[j] += [i]





def maxlen(n):

    if len(todc[n]) == 0:

        lendc[n] = 0

        return 0

    else:

        m = 0

        for i in todc[n]:

            if lendc[i] == -1:

                lendc[i] = maxlen(i)

            m = max(m, lendc[i]+1)

        return m

       



def maxlen_stack(org_n):

    stack = [org_n]

    while stack:

        n = stack.pop()

        if len(todc[n]) == 0:

            lendc[n] = 0

        else:

            m = 0

            recursive = []

            for i in todc[n]:

                if lendc[i] == -1:

                    recursive.append(i)

                else:

                    m = max(m, lendc[i]+1)

            if recursive:

                stack += [n] + recursive

            else:

                lendc[n] = m         

    return lendc[org_n]



k = 0

for i in range(1, n+1):

    if lendc[i] == -1:

        #lendc[i] = maxlen(i)

        lendc[i] = maxlen_stack(i)

    k = max(k, lendc[i])

print(k)

import collections

n, m = [int(i) for i in input().split()]

todc = collections.defaultdict(lambda: [])

lendc = collections.defaultdict(lambda: -1)





for k in range(m):

    i, j = [int(n) for n in input().split()]

    todc[j] += [i]





def maxlen(n):

    if len(todc[n]) == 0:

        lendc[n] = 0

        return 0

    else:

        m = 0

        for i in todc[n]:

            if lendc[i] == -1:

                lendc[i] = maxlen(i)

            m = max(m, lendc[i]+1)

        return m

       



def maxlen_stack(org_n):

    stack = [org_n]

    while stack:

        n = stack.pop()

        if lendc[n] != -1:

            continue        

        if len(todc[n]) == 0:

            lendc[n] = 0

        else:

            m = 0

            recursive = []

            for i in todc[n]:

                if lendc[i] == -1:

                    recursive.append(i)

                else:

                    m = max(m, lendc[i]+1)

            if recursive:

                stack += [n] + recursive

            else:

                lendc[n] = m         

    return lendc[org_n]



k = 0

for i in range(1, n+1):

    if lendc[i] == -1:

        #lendc[i] = maxlen(i)

        lendc[i] = maxlen_stack(i)

    k = max(k, lendc[i])

print(k)

import collections

n, m = [int(i) for i in input().split()]
todc = collections.defaultdict(lambda: [])
lendc = collections.defaultdict(lambda: -1)
for k in range(m):
    i, j = [int(n) for n in input().split()]
    todc[j] += [i]


def maxlen(n):
    if len(todc[n]) == 0:
        lendc[n] = 0
        return 0
    else:
        m = 0
        for i in todc[n]:
            if lendc[i] == -1:
                lendc[i] = maxlen(i)
            m = max(m, lendc[i] + 1)
        return m


def maxlen_stack(org_n):
    stack = [org_n]
    while stack:
        n = stack.pop()
        if len(todc[n]) == 0:
            lendc[n] = 0
        else:
            m = 0
            recursive = []
            for i in todc[n]:
                if lendc[i] == -1:
                    recursive.append(i)
                else:
                    m = max(m, lendc[i] + 1)
            if recursive:
                stack += [n] + recursive
            else:
                lendc[n] = m
    return lendc[org_n]


k = 0
for i in range(1, n + 1):
    if lendc[i] == -1:
        # lendc[i] = maxlen(i)
        lendc[i] = maxlen_stack(i)
    k = max(k, lendc[i])
print(k)

import collections

n, m = [int(i) for i in input().split()]
todc = collections.defaultdict(lambda: [])
lendc = collections.defaultdict(lambda: -1)
for k in range(m):
    i, j = [int(n) for n in input().split()]
    todc[j] += [i]


def maxlen(n):
    if len(todc[n]) == 0:
        lendc[n] = 0
        return 0
    else:
        m = 0
        for i in todc[n]:
            if lendc[i] == -1:
                lendc[i] = maxlen(i)
            m = max(m, lendc[i] + 1)
        return m


def maxlen_stack(org_n):
    stack = [org_n]
    while stack:
        n = stack.pop()
        if lendc[n] != -1:
            continue
        if len(todc[n]) == 0:
            lendc[n] = 0
        else:
            m = 0
            recursive = []
            for i in todc[n]:
                if lendc[i] == -1:
                    recursive.append(i)
                else:
                    m = max(m, lendc[i] + 1)
            if recursive:
                stack += [n] + recursive
            else:
                lendc[n] = m
    return lendc[org_n]


k = 0
for i in range(1, n + 1):
    if lendc[i] == -1:
        # lendc[i] = maxlen(i)
        lendc[i] = maxlen_stack(i)
    k = max(k, lendc[i])
print(k)

+        if lendc[n] != -1: +            continue