{-# LANGUAGE BangPatterns #-}
import Control.Arrow
import Control.Monad.Fix
import Data.Char
import Data.Int
import Data.Foldable
import Data.List
import qualified Data.Vector as V
import qualified Data.Vector.Unboxed as VU
import qualified Data.ByteString.Char8 as B

solve :: Int -> Int -> VU.Vector (Int, Int) -> Int
solve !n !m !xs =
  V.length $ V.filter (\k -> k == m) $ V.map (\(!x, y, z) -> x) $ V.scanl'
    ( \(!acc, !ls, !rs) (!i) ->
      let (l, ls') =
            if not (null ls) && i == head ls then (1, tail ls) else (0, ls)
          (r, rs') =
            if not (null rs) && i == head rs then (-1, tail rs) else (0, rs)
      in  (acc + l + r, ls', rs')
    )
    (0, lefts, rights)
    (V.convert $ VU.fromList [1 .. n])
 where
  lefts  = sort $ VU.toList $ VU.map fst xs
  rights = sort $ VU.toList $ VU.map ((+ 1) . snd) xs

main = do
  let readInt :: Integral a => B.ByteString -> Maybe (a, B.ByteString)
      readInt = fmap (first fromIntegral . second B.tail) . B.readInt

  (n, m) <-
    (\vec -> (vec VU.! 0, vec VU.! 1)) . VU.unfoldrN 2 readInt <$> B.getLine
  xs <-
    VU.replicateM m
    $   (\vec -> (vec VU.! 0, vec VU.! 1))
    .   VU.unfoldrN 2 readInt
    <$> B.getLine
  print $ solve n m xs
