import Control.Applicative
import Control.Monad
import qualified Data.ByteString.Char8 as BS
import Data.Maybe
import Data.List
import Data.Ord

listToTuple2 [a,b] = (a,b)

main = do
  (n,m) <- listToTuple2 . map read . words <$> getLine :: IO (Int,Int)
  query <- sortBy (comparing snd) . map (listToTuple2 . map (fst . fromJust . BS.readInt) . BS.words) . BS.lines <$> BS.getContents :: IO [(Int,Int)]

  print $ length $ solve query Nothing

  where
    solve [] _ = []
    solve ((a,b):qs) Nothing = (b-1) : (solve qs (Just $ b-1))
    solve ((a,b):qs) (Just x)
      | a <= x = solve qs (Just x)
      | a > x = (b-1) : (solve qs (Just $ b-1))
