import Control.Monad (replicateM)
import Data.Array.Diff

data Field = Field { f_place :: DiffArray Int Int, f_middle :: DiffArray Int Int, f_m :: Int , f_s :: Int }

f_empty :: Int -> Field
f_empty n = Field {
    f_place = listArray (1, n) [0,0..],
    f_middle = listArray (1, n) [0,0..],
    f_m = 0,
    f_s = 0
}

f_new :: Field -> (Int, Int) -> Field
f_new f (x, y) = f {
        f_place = (f_place f) // [(x, new_m), (y, new_m)],
        f_middle = (f_middle f) // [(new_m, new_s)],
        f_m = new_m,
        f_s = new_s
    }
        where
            new_m = (f_m f) + 1
            new_s = (f_s f) + 1

f_add :: Field -> Int -> Int -> Field
f_add f x m = f { f_place = (f_place f) // [(x, m)] }

f_union :: Field -> Int -> Int -> Field
f_union f m m' = f { f_middle = (f_middle f) // [(m', (f_middle f) ! m)] }

addPair :: Field -> (Int, Int) -> Field
addPair f p@(x, y) = case ((f_place f) ! x, (f_place f) ! y) of
    (0, 0) -> f_new f p
    (m_x, 0) -> f_add f y m_x
    (0, m_y) -> f_add f x m_y
    (m_x, m_y) -> f_union f m_x m_y

f_movable :: Field -> (Int, Int) -> Bool
f_movable f (i, x) = case (f_place f) ! i of
    0 -> i == x
    m -> case (f_place f) ! x of
        0  -> False
        m' -> ((f_middle f) ! m) == ((f_middle f) ! m')

equals :: Int -> [Int] -> [(Int, Int)] -> Int
equals n l pairs = length $ filter (f_movable f) $ zip [1..] l
    where
        f = foldl addPair (f_empty n) pairs

main = do
    [n, m] <- (map read . words) <$> getLine :: IO [Int]
    l <- (map read . words) <$> getLine :: IO [Int]
    pairs <- map (map read . words) <$> replicateM m getLine :: IO [[Int]]
    print $ equals n l $ map (\[x,y]->(x,y)) pairs
