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, IOUArray)
import Data.Array.ST      (STArray, STUArray)
import Data.Array.Unboxed (UArray)

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)

type STUArrayM s i e = ST s (STUArray s i e)
type STUArrayIx s e  = STUArray s Int e

solve :: Int -> [Pair Int] -> Int
solve n xs = runST $ do
    num <- M.newArray (1, n) 1     :: STUArrayM s Int Int
    col <- M.newArray (1, n) False :: STUArrayM s Int Bool
    M.writeArray col 1 True
    forM_ xs $ \(x, y) -> do
        k <- M.readArray num x
        c <- M.readArray col x
        let (stay, red) = case (k, c) of
                (_, False) -> (False, False)
                (1, True)  -> (False, True)
                (_, True)  -> (True,  True)
        M.writeArray num x $ pred k
        M.writeArray num y . succ =<< M.readArray num y
        M.writeArray col x stay
        M.writeArray col y red
    L.sum . L.map fromEnum <$> M.getElems col
