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

makeSets :: Int -> IO (IOUArray Int Int)
makeSets n = newListArray (1, n) (repeat 0)

find :: IOUArray Int Int -> Int -> IO Int
find arr x = do
  px <- readArray arr x
  if px == 0
    then return x
    else do
      ppx <- find arr px
      writeArray arr x ppx
      return ppx

union :: IOUArray Int Int -> Int -> Int -> IO ()
union arr x y = do
  xr <- find arr x
  yr <- find arr y
  writeArray arr xr yr

readOperation :: String -> (Int, Int)
readOperation s = let [x, y] = map read . words $ s in (x, y)

solve :: [(Int, Int)] -> [Int] -> IO Int
solve operations permutation = do
  sets <- makeSets (length permutation)
  mapM_ (uncurry (union sets)) operations
  xs <- mapM (\(i,x) -> do { ri <- find sets i; rx <- find sets x; return $ if ri == rx then 1 else 0 }) (zip [1..] permutation)
  return $ sum xs

main :: IO ()
main = do
  [_n, m] <- map read . words <$> getLine
  permutation <- map read . words <$> getLine
  operations <- replicateM m (readOperation <$> getLine)
  print =<< solve operations permutation
