import Control.Monad (replicateM)
import Control.Monad.Trans.State
import Data.Maybe (fromMaybe)

makeSet :: [a] -> [[a]]
makeSet = map pure

pick :: (a -> Bool) -> [a] -> (Maybe a, [a])
pick _ [] = (Nothing, [])
pick p (x:xs) = if p x then (Just x, xs) else fmap (x:) (pick p xs)

find :: Eq a => a -> State [[a]] (Maybe [a])
find x = state $ pick (x `elem`)

merge :: Eq a => (a, a) -> State [[a]] ()
merge (x, y) = do
  x_root <- find x
  y_root <- find y
  modify ((fromMaybe [] x_root ++ fromMaybe [] y_root) :)

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

countIf :: (a -> Bool) -> [a] -> Int
countIf p = foldr (\x y -> if p x then 1 + y else y) 0

solve :: [(Int, Int)] -> [Int] -> Int
solve operations permutation =
  let merged = execState (mapM_ merge operations) (makeSet permutation)
  in countIf (\(i, x) -> i `elem` fromMaybe [] (evalState (find x) merged)) (zip [1..] permutation)

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