{-# LANGUAGE BangPatterns #-}
import Control.Arrow
import Control.Monad.Fix
import Data.Int
import Data.Foldable
import Data.List
import qualified Data.Vector as V
import qualified Data.Vector.Unboxed as VU
import qualified Data.ByteString.Char8 as B

solve :: Int -> Int -> VU.Vector (Int, Int) -> Int
solve !n !m !abn = (\(x, _, _) -> x) $ foldl'
  (\(!acc, !mx, !tgts) (!pos) ->
    let (acc', mx') = if mx == pos then (acc + 1, n) else (acc, mx)
    in  case tgts of
          ((s, t) : tgts') | s == pos -> (acc', mx' `min` t, tgts')
          _                           -> (acc', mx', tgts)
  )
  (0, n, targets)
  [0 .. n - 1]
 where
  targets =
    VU.toList
      $ VU.filter (\(x, y) -> y < n)
      $ VU.zip (VU.fromList [0 .. n - 1])
      $ VU.accumulate min (VU.replicate n n) abn

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
  abn <-
    VU.replicateM m
    $   (\vec -> (vec VU.! 0 - 1, vec VU.! 1 - 1))
    .   VU.unfoldrN 2 readInt
    <$> B.getLine
  print $ solve n m abn
