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

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

makeSets :: Int -> ST s (STUArray s Int Int)
makeSets n = newListArray (1, n) [1..n]

find :: STUArray s Int Int -> Int -> ST s Int
find arr x = do
  parent <- readArray arr x
  if parent == x
    then return x
    else do
      root <- find arr parent
      writeArray arr x root
      return root

union :: STUArray s Int Int -> Int -> Int -> ST s ()
union sets x y = do
  x_root <- find sets x
  y_root <- find sets y
  writeArray sets x_root y_root

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

solve :: [(Int, Int)] -> [Int] -> Int
solve operations permutation = runST $ do
    sets <- makeSets (length permutation)
    mapM_ (uncurry (union sets)) operations
    sum <$> mapM (beTogether sets) (zip [1..] permutation)
  where
    beTogether sets (i, x) = do
      i_root <- find sets i
      x_root <- find sets x
      if i_root == x_root
        then return 1
        else return 0

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