import Control.Monad
import qualified Data.Map.Strict as Map
import Data.List

main :: IO ()
main = do
  (n:m:_) <- fmap (map read . words) getLine :: IO [Int]
  gs <- replicateM m $ fmap (map read . words) getLine :: IO [[Int]]
  print . solve' 0 . sortBridge $ gs

sortBridge = sortBy (\x y -> (x !! 1) `compare` (y !! 1))
isDivided i ds = (ds !! 0) <= i && (ds !! 1) > i

solve' :: Int -> [[Int]] -> Int
solve' i [] = i
solve' i gs = solve' (i + 1) $ filter (not . isDivided bridge) gs
  where
    bridge = (head gs) !! 1 - 1
