{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}
import           Control.Monad               (forM, forM_, when)
import           Control.Monad.ST            (ST, runST)
import qualified Data.ByteString.Char8       as BC
import           Data.Maybe                  (fromJust)
import qualified Data.Vector.Unboxed.Mutable as VM

main :: IO ()
main = do
  [n, m] <- readInts <$> BC.getLine
  ps <- readInts <$> BC.getLine
  xys <- readIntss <$> BC.getContents
  print $ solve n m ps xys

solve :: Int -> Int -> [Int] -> [[Int]] -> Int
solve n m ps xys = runST $ do
  uf <- consUF n
  forM_ xys $ \([x, y]) ->
    unite (x - 1) (y - 1) uf
  fs <- forM (zip ps [1 .. n]) $ \(p, q) ->
    find (p - 1) (q - 1) uf
  return . length $ filter id fs

consUF :: Int -> ST s (VM.STVector s Int)
consUF n = VM.replicate n (-1)

unite :: Int -> Int -> VM.STVector s Int -> ST s Bool
unite i j uf = do
  ri <- root i uf
  rj <- root j uf
  when (ri /= rj) (updateUF ri rj uf)
  return $ ri /= rj

updateUF :: Int -> Int -> VM.STVector s Int -> ST s ()
updateUF i j uf = do
  x <- VM.read uf i
  y <- VM.read uf j
  let (i', j') = if x <= y then (i, j) else (j, i)
  VM.write uf i' $ x + y
  VM.write uf j' i'

find :: Int -> Int -> VM.STVector s Int -> ST s Bool
find i j uf = (==) <$> root i uf <*> root j uf

root :: Int -> VM.STVector s Int -> ST s Int
root i uf = do
  x <- VM.read uf i
  if x < 0
    then return i
    else do
      y <- root x uf
      VM.write uf i y
      return y

readInt :: BC.ByteString -> Int
readInt = fst . fromJust . BC.readInt

readInts :: BC.ByteString -> [Int]
readInts = map readInt . BC.words

readIntss :: BC.ByteString -> [[Int]]
readIntss = map readInts . BC.lines
