import Control.Monad.State
import Control.Monad.Reader
import Control.Arrow
import Data.Functor
import Data.List

main = do
  (_:x:_) <- map read . words <$> getLine :: IO[Int]
  aaa <-  (repeat >>> take x >>> sequence $ getLine) :: IO [String]
  print $ hoge aaa

toIntTuples = words >>> map read >>> (\(a:b:_) -> (a,b)) :: String -> (Int,Int)

hoge = map toIntTuples >>> sort >>> groupBy (\a b -> fst a == (fst b)) >>> map head >>> sortBy (\(a,b) (x,y) -> compare (b-a) (y-x)) >>> \x -> evalState dropBridges (0, x)

dropBridges :: State (Int, [(Int, Int)]) Int
dropBridges = do
  (cnt, bridges) <- get
  if null bridges then return cnt
  else do
    let dropping = head bridges
    put (cnt+1, filter (outside dropping) bridges)
    dropBridges
    where outside (a, b) (x, y) = (x < a && y <= a) || (b <= x && b < y)

