{-# LANGUAGE BangPatterns #-}
import Control.Arrow
import Control.Monad.Fix
import Data.Int
import Data.Foldable
import qualified Data.Vector as V
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import qualified Data.ByteString.Char8 as B
import Data.ByteString.Internal (accursedUnutterablePerformIO)
import Debug.Trace

unew :: VUM.Unbox a => Int -> VUM.IOVector a
unew !n = accursedUnutterablePerformIO $ VUM.unsafeNew n

uread :: VUM.Unbox a => Int -> VUM.IOVector a -> a
uread !i !v = accursedUnutterablePerformIO $ VUM.unsafeRead v i

uwrite :: VUM.Unbox a => Int -> a -> VUM.IOVector a -> VUM.IOVector a
uwrite !i !a !v =
  accursedUnutterablePerformIO $ VUM.unsafeWrite v i a >> return v

uthaw :: VUM.Unbox a => VU.Vector a -> VUM.IOVector a
uthaw !v = accursedUnutterablePerformIO $ VU.unsafeThaw v

ufreeze :: VUM.Unbox a => VUM.IOVector a -> VU.Vector a
ufreeze !v = accursedUnutterablePerformIO $ VU.unsafeFreeze v

uset :: VUM.Unbox a => a -> VUM.IOVector a -> VUM.IOVector a
uset !a !v = accursedUnutterablePerformIO $ VUM.set v a >> return v

solve :: Int -> Int -> VU.Vector (Int16, Int16) -> Int
solve n m edges = VU.maximum paths
 where
  graph :: V.Vector [Int16]
  graph = V.accumulate (\(!x) (!y) -> y : x)
                       (V.replicate n [])
                       (V.convert $ VU.map (first fromIntegral) edges)

  paths :: VU.Vector Int
  paths = ufreeze $ foldl'
    ( \(!acc) (!i) -> if uread i acc >= 0
      then acc
      else (\(!_, !_, !x) -> x) $ fix
        ( \(!f) (!ts, !i, !acc) -> if null ts
          then ([], i, uwrite i 0 acc)
          else
            let acc' = foldl'
                  ( \(!acc) !t -> (\(!_, !_, !x) -> x)
                    $ f (graph V.! fromIntegral t, fromIntegral t, acc)
                  )
                  acc
                  ts
            in  ( []
                , i
                , uwrite
                  i
                  (succ $ maximum $ map (\t -> uread (fromIntegral t) acc') ts)
                  acc'
                )
        )
        (graph V.! i, i, acc)
    )
    (uset (-1) $ unew n)
    [0 .. n - 1]

main = do
  let readInt :: Integral a => B.ByteString -> Maybe (a, B.ByteString)
      readInt = fmap (first fromIntegral . second B.tail) . B.readInt

  (n, m) <-
    (\vec -> (vec VU.! 0, vec VU.! 1)) . VU.unfoldrN 2 readInt <$> B.getLine
  edges <-
    VU.replicateM m
    $   (\vec -> (vec VU.! 0 - 1, vec VU.! 1 - 1))
    .   VU.unfoldrN 2 readInt
    <$> B.getLine
  print $ solve n m edges
