import Data.Either (either, isLeft, isRight, lefts, partitionEithers, rights)
import Data.Maybe
    ( catMaybes
    , fromJust
    , fromMaybe
    , isJust
    , isNothing
    , listToMaybe
    , mapMaybe
    , maybe
    , maybeToList
    )

import           Data.Array.IArray (IArray, (!))
import qualified Data.Array.IArray as I (Array, array, assocs, elems, listArray)
import           Data.Array.MArray (MArray)
import qualified Data.Array.MArray as M
    ( freeze
    , getAssocs
    , getElems
    , newArray
    , newListArray
    , readArray
    , writeArray
    )

import Data.Array.IO (IOArray)
import Data.Array.ST (STArray)

import Data.Bifunctor (bimap, first, second)
import Data.Foldable  (and, or, product, sum)
import Data.Function
import Data.Monoid    ((<>))

import qualified Data.List as L

import Control.Applicative
import Control.Monad
import Control.Monad.ST    (ST, runST)

main :: IO ()
main = do
    (n, m) <- getPair
    xs     <- replicateM m getPair
    print $ solve n xs

type Pair a = (a, a)
getPair = (\(x:y:_) -> (x, y)) . map read . words <$> getLine :: Read a => IO (Pair a)

solve :: Int -> [Pair Int] -> Int
solve n xs = runST $ do
    a <- M.newArray (1, n) (1, False) :: ST s (STArray s Int (Int, Bool))
    M.writeArray a 1 (1, True)
    forM_ xs $ \(x, y) -> do
        box <- M.readArray a x
        let addRed = bimap succ $ const True  :: (Int, Bool) -> (Int, Bool)
            remRed = bimap pred $ const False :: (Int, Bool) -> (Int, Bool)
            f, g :: (Int, Bool) -> (Int, Bool)
            (f, g) = case box of
                (1, True)  -> (addRed,     remRed)
                (_, True)  -> (addRed,     first pred)
                (_, False) -> (first succ, first pred)
        M.writeArray a y . f =<< M.readArray a y
        M.writeArray a x $ g box
    L.length . L.filter snd <$> M.getElems a
