import Control.Monad
import qualified Data.IntSet as S
import Data.IntSet (IntSet)

main :: IO ()
main = do
  [n, m] <- readInts
  ass <- replicateM m readInts
  print $ solve [] ass

readInts :: IO [Int]
readInts = map read . words <$> getLine

solve :: [IntSet] -> [[Int]] -> Int
solve ss [] = solve' ss
solve ss ([a, b]:ass) = solve (add (S.fromList [a..b]) ss) ass

add :: IntSet -> [IntSet] -> [IntSet]
add s ss 
  | any (`S.isSubsetOf` s) ss
    = filter (not . S.isSubsetOf s) ss
  | otherwise
    = (s :) $ filter (not . S.isSubsetOf s) ss

solve' :: [IntSet] -> Int
solve' [] = 0
solve' (s:ss) = 1 + solve' $ filter (S.disjoint s) ss
