import           Control.Monad
import qualified Data.ByteString.Char8 as B
import           Data.Set              (Set (..))
import qualified Data.Set              as S

data Interval = Interval Int Int

instance Eq (Interval) where
  (Interval x y) /= (Interval z w)
    | y <= z    = False
    | w <= x    = False
    | otherwise = True

instance Ord (Interval) where
  compare (Interval x y) (Interval z w)
    | y <= z    = LT
    | w <= x    = GT
    | otherwise = EQ

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

readInts :: IO [Int]
readInts = map (maybe err fst . B.readInt) . B.words <$> B.getLine
  where err = error "parse error"

readInterval :: IO Interval
readInterval = (\[x, y] -> Interval x y) <$> readInts

solve :: [Interval] -> Int
solve = S.size . foldr insert S.empty

merge :: Interval -> Interval -> Interval
merge (Interval x y) (Interval z w)
  | y <= z    = err
  | w <= x    = err
  | otherwise = Interval (x `max` z) (y `min` w)
  where err = error "merge error"

insert :: Interval -> Set Interval -> Set Interval
insert i6l set
  | i6l `S.member` set = S.insert (merge i6l i6l') set
  | otherwise          = S.insert i6l set
  where
    i6l'  = S.elemAt index set
    index = S.findIndex i6l set
