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

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 n $ gs
  --print . bridges $ gs

solve :: Int -> [[Int]] -> Int
solve n gs = breakBridge n 0 . bridges $ gs
--  where
bridges [] = []
bridges (g:gs) = let (a:b:_) = g in [a..(b - 1)]:bridges gs

breakBridge :: Int -> Int -> [[Int]] -> Int
breakBridge _ i [] = i
breakBridge n i bs = breakBridge n (i + 1) newb
  where
    counts = toCountMap . join $ bs :: Map.Map Int Int
    --m = maximum . Map.elems $ counts
    --opt = lookup m . Map.fromList $ [(v, k) | (k, v) <- Map.toList counts]
    --opt = maximum . Map.fromList $ [(v, k) | (k, v) <- Map.toList counts]
    ret Nothing = []
    ret (Just x) = [b | b <- bs, notElem x b]
    --newb = ret opt
    opt = maximum [(v, k) | (k, v) <- Map.toList counts]
    (_, bridgeToBreak) = opt
    newb = [b | b <- bs, notElem bridgeToBreak b]

toCountMap :: (Ord k) => [k] -> Map.Map k Int
toCountMap xs = Map.fromListWith (+) [(x, 1) | x <- xs]
