import Control.Monad
import Control.Monad.ST
import Data.Maybe
import qualified Data.ByteString.Char8 as BS
import Data.List
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as VM
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM

readInt = fst . fromJust . BS.readInt
readIntList = map readInt . BS.words
getInt = readInt <$> BS.getLine
getIntList = readIntList <$> BS.getLine

main = do
    [n,m] <- getIntList
    xys <- replicateM m $ do
        [x,y] <- getIntList
        return (x-1,y-1)
    let edges = V.create $ do
            vec <- VM.replicate n []
            forM_ xys $ \(x,y) -> VM.modify vec (y:) x
            return vec
    let ans :: Int
        ans = runST $ do
            vec <- VUM.replicate n (-1)
            let len x = do
                l <- VUM.read vec x
                if l >= 0 then
                    return l
                else do
                    ls <- mapM len (edges V.! x)
                    let a = if null ls then 0
                            else maximum ls + 1
                    VUM.write vec x a
                    return a
            maximum <$> mapM len [0..n-1]
    print ans