import Control.Monad (replicateM)
import qualified Data.Set as Set
import qualified Data.Map as Map
import Data.Maybe

insertSet :: Ord k => Set.Set k -> a -> Map.Map k a -> Map.Map k a
insertSet keySet value m = Set.foldl ins m keySet
    where
        ins m' key = Map.insert key value m'

addPair :: (Map.Map Int (Set.Set Int)) -> (Int, Int) -> (Map.Map Int (Set.Set Int))
addPair m (x, y) = insertSet s s m
    where
        s = case (Map.lookup x m, Map.lookup y m) of
            (Just s', Just s'') | s' == s'' -> Set.empty
            (Just s', Just s'') -> Set.union s' s''
            (Just s', Nothing) -> Set.insert y s'
            (Nothing, Just s') -> Set.insert x s'
            (Nothing, Nothing) -> Set.fromList [x, y]
            
uniq :: Eq a => [a] -> [a]
uniq [] = []
uniq (x:xs) | x `elem` xs = uniq xs
uniq (x:xs)               = x:(uniq xs)

equals :: [Int] -> [(Int, Int)] -> Int
equals l pairs = (length noNeedToMove) + (sum $ map countIn sets)
    where
        m = foldl addPair Map.empty pairs
        noNeedToMove = filter (\(i,x)->i == x && Map.notMember x m) $ zip [1..] l
        sets = uniq $ Map.elems m
        countIn s = length $ filter isMember $ map at $ Set.toList s
            where
                at i = l !! (i - 1)
                isMember x = Set.member x s

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