import Control.Monad.ST
import Data.Array.ST

createDjs :: Int -> ST s (STArray s Int Int)
createDjs n = do newArray (1, n) (-1)

findDjs :: STArray s Int Int -> Int -> ST s Int
findDjs arr x = do
    x' <- readArray arr x
    if x' < 0
        then
            return x
        else
            do
                x'' <- findDjs arr x'
                writeArray arr x x''
                return x''

mergeDjs :: STArray s Int Int -> Int -> Int -> ST s ()
mergeDjs arr x y = do
    nx <- readArray arr x
    ny <- readArray arr y
    if nx > ny
        then do
            writeArray arr x y
            writeArray arr y (nx + ny)
        else do
            writeArray arr y x
            writeArray arr x (nx + ny)

addDjs :: STArray s Int Int -> [Int] -> ST s ()
addDjs arr [x, y] = do
    x' <- findDjs arr x
    y' <- findDjs arr y
    if x' == y'
        then
             return()
        else
            do
                mergeDjs arr x' y'

movable :: STArray s Int Int -> (Int, Int) -> ST s Int
movable arr (x, y) = do
    x' <- findDjs arr x
    y' <- findDjs arr y
    return $ if x' == y' then 1 else 0

equals :: Int -> [Int] -> [[Int]] -> Int
equals n p edges = runST $ do
    arr <- createDjs n
    mapM_ (addDjs arr) edges
    movable_list <- mapM (movable arr) $ zip [1..] p
    return $ sum movable_list


main :: IO ()
main = do
    [n, m] <- map read . words <$> getLine :: IO [Int]
    p <- map read . words <$> getLine :: IO [Int]
    edges <- mapM (\_->map read . words <$> getLine) [1..m] :: IO [[Int]]
    putStrLn $ show $  equals n p edges
