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

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

main = do
    [n,m] <- getIntList
    ba <- fmap sort $ replicateM m $ do
        [a,b] <- getIntList
        return (b, a)
    print . fst $ foldl' f (0,0) ba

f (l,p) (b,a) | a <= p = (l,p)
              | otherwise = (l+1,b-1)