import Control.Monad (replicateM)
import Data.Set hiding (foldl, map, filter)
import qualified Data.Set as Set
import Data.Maybe

addPair :: [Set Int] -> (Int, Int) -> [Set Int]
addPair ls (x, y) = case (searchLinkedSets x y ls) of
    (Nothing, Nothing, r) -> (fromList [x, y]):r
    (Just(xs), Nothing, r) -> (insert y xs):r
    (Nothing, Just(ys), r) -> (insert x ys):r
    (Just(xs), Just(ys), r) -> (union xs ys):r

searchLinkedSets :: Int -> Int -> [Set Int] -> (Maybe(Set Int), Maybe(Set Int), [Set Int])
searchLinkedSets x y ls = foldl (search' x y) (Nothing, Nothing, []) ls
    where
        search' x y (Nothing, Nothing, ls) s | (member x s) && (member y s) = (Just s, Just s, ls)
        search' x y (Nothing, ys, ls) s | member x s = (Just s, ys, ls)
        search' x y (xs, Nothing, ls) s | member y s = (xs, Just s, ls)
        search' _ _ (xs, ys, ls) s             = (xs, ys, s:ls)

linkedSets :: [(Int, Int)] -> [Set Int]
linkedSets = foldl addPair []

equals :: [Int] -> [(Int, Int)] -> Int
equals l pairs = noNeedToMove + (sum $ map countIn linkedSets')
    where
        linkedSets' = linkedSets pairs
        countIn s = length $ filter ismember pickuped
            where
                ismember x = member x s
                at i = l !! (i - 1)
                pickuped = map at $ toList s
        linkedElements = unions linkedSets'
        notLinked x = notMember x linkedElements
        noNeedToMove = length $ filter notLinked $ map (\(_, x)->x) $ filter (\(i, x) -> i == x) $ zip [1..] l

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
