
{-# LANGUAGE StandaloneDeriving #-}

module Main where

import Debug.Trace
import qualified Data.Map.Strict as Map


main :: IO ()
main = do
  getLine
  s <- map read . words <$> getLine
  j <- map (map read . words) . lines <$> getContents
  --s `traceShow` j `traceShow` print $ solve s j
  print $ solve s j


solve :: [Int] -> [[Int]] -> Int
--solve s j = uf' `traceShow` cnt
solve s j =  cnt
  where
    z = zip [1..] s
    tbl = foldr ins Empty $ j
    --ins [i, a] uf = uf `traceShow` union i a uf
    ins [i, a] uf = union i a uf
    (cnt, uf') = check 0 z tbl

check :: Int -> [(Int, Int)] -> UnionFind -> (Int, UnionFind)
check cnt [] uf = (cnt, uf)
check cnt ((i, a):ss) uf = check (if b then cnt + 1 else cnt) ss uf'
   where
     (b, uf') = same i a uf

data UnionFind = Empty | Tree (Map.Map Int Int)
deriving instance Show (UnionFind) 

root :: Int -> UnionFind -> (Int, UnionFind)
root a Empty = (a, set a a Empty)
root a uf@(Tree t) = (s, set a s uf')
  where
    (s, uf') = case (Map.lookup a t) of Just k -> if a == k then (k, uf) else root k uf
                                        Nothing -> (a, uf)

union :: Int -> Int -> UnionFind -> UnionFind
union a b uf = Tree t'
  where
    (ar, uf') = root a uf
    (br, uf'') = root b uf'
    t' = case uf'' of Tree t -> if ar == br then t else Map.insert ar br t
                      Empty -> Map.empty

same :: Int -> Int -> UnionFind -> (Bool, UnionFind)
same a b uf = (ar == br, uf'')
  where
    (ar, uf') = root a uf
    (br, uf'') = root b uf'

set :: Int -> Int -> UnionFind -> UnionFind
set a b Empty = Tree (Map.insert a b Map.empty)
set a b (Tree t) = Tree (Map.insert a b t)
