import Control.Monad
import Control.Monad.Trans.State.Strict
import Data.Char (isSpace)
import Data.Maybe (fromJust)
import qualified Data.ByteString.Lazy.Char8 as B
import Data.List
--
type Parser a = StateT B.ByteString Maybe a

parseInt :: Parser Int
parseInt = StateT (B.readInt . B.dropWhile isSpace)

parseInt2 :: Parser (Int,Int)
parseInt2 = liftM2 (,) parseInt parseInt

parse :: Parser [(Int,Int)]
parse = do
    (_,m)<-parseInt2
    replicateM m parseInt2

main=B.getContents>>=print.f 0 0.sortOn snd.fromJust.evalStateT parse

f n _ [] = n
f n x ((a,b):abs)
    | a<x  = f n x abs
    | True = f (n+1) b abs
