Problem:
Score : 400 points

Problem Statement

We have a permutation of the integers from 1 through N, p_1, p_2, .., p_N.
We also have M pairs of two integers between 1 and N (inclusive), represented as (x_1,y_1), (x_2,y_2), .., (x_M,y_M).
AtCoDeer the deer is going to perform the following operation on p as many times as desired so that the number of i (1 ≤ i ≤ N) such that p_i = i is maximized:

Choose j such that 1 ≤ j ≤ M, and swap p_{x_j} and p_{y_j}.

Find the maximum possible number of i such that p_i = i after operations.

Constraints

2 ≤ N ≤ 10^5

1 ≤ M ≤ 10^5

p is a permutation of integers from 1 through N.

1 ≤ x_j,y_j ≤ N

x_j ≠ y_j

If i ≠ j, \{x_i,y_i\} ≠ \{x_j,y_j\}.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
p_1 p_2 .. p_N
x_1 y_1
x_2 y_2
:
x_M y_M

Output

Print the maximum possible number of i such that p_i = i after operations.

Sample Input 1

5 2
5 3 1 4 2
1 3
5 4

Sample Output 1

2

If we perform the operation by choosing j=1, p becomes 1 3 5 4 2, which is optimal, so the answer is 2.

Sample Input 2

3 2
3 2 1
1 2
2 3

Sample Output 2

3

If we perform the operation by, for example, choosing j=1, j=2, j=1 in this order, p becomes 1 2 3, which is obviously optimal.
Note that we may choose the same j any number of times.

Sample Input 3

10 8
5 3 6 8 7 10 9 1 2 4
3 1
4 1
5 9
2 5
6 5
3 5
8 9
7 9

Sample Output 3

8

Sample Input 4

5 1
1 2 3 4 5
1 5

Sample Output 4

5

We do not have to perform the operation.

Solution in Haskell (GHC 7.10.3) (judge verdict: Accepted)

{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE ScopedTypeVariables #-}
import qualified Data.ByteString.Lazy.Char8 as B
import Data.Maybe
import qualified Data.IntMap as IM
import qualified Data.IntSet as IS
import Debug.Trace

solve :: Int -> Int -> [Int] -> [(Int,Int)] -> Int
solve n m ps xys = answer where

  grp0 = foldr (uncurry uftMerge) IM.empty xys

  im1 :: IM.IntMap (IS.IntSet, IS.IntSet)
--  grp0xxx = (trace ("grp0" ++ show grp0) grp0)
  grp0xxx = grp0
  (im1, _) = foldr op (IM.empty, grp0xxx) (zip [1..] ps) where
    op :: (Int, Int) -> (IM.IntMap (IS.IntSet, IS.IntSet), IM.IntMap Int)
       -> (IM.IntMap (IS.IntSet, IS.IntSet), IM.IntMap Int)
    op (n, p) (jm, grp) = (jm', grp') where
      (r, grp') = getRoot n grp
      jm' = IM.alter f r jm where
        f Nothing         = Just (IS.singleton n, IS.singleton p)
        f (Just (cn, cp)) = Just (IS.insert n cn, IS.insert p cp)

  answer = foldr op 0 im1 where
    op (cn, cp) sum = sum + IS.size (IS.intersection cn cp)

uftMerge :: Int -> Int -> IM.IntMap Int -> IM.IntMap Int
uftMerge x y im | rx == ry = im
                | otherwise = IM.insert rx ry im2
  where (rx, im1) = getRoot x im
        (ry, im2) = getRoot y im1

getRoot :: Int -> IM.IntMap Int -> (Int, IM.IntMap Int)
getRoot x im = case IM.lookup x im of
                 Nothing -> (x, im)
                 Just y -> let (root, im1) = getRoot y im
                           in (root, IM.adjust (const root) x im1)

readBInt :: B.ByteString -> Int
readBInt = fst . fromJust . B.readInt

tmain :: B.ByteString -> Int
tmain cont =
  let remLines0 = map B.words (B.lines cont)
      [bs_n,bs_m]:remLines1 = remLines0
      n = readBInt bs_n
      m = readBInt bs_m
      line2:remLines2 = remLines1
      ps = map readBInt line2
      xys = map (\[x1,x2] -> (readBInt x1,readBInt x2)) remLines2
  in solve n m ps xys

outAnswer :: Int -> IO ()
outAnswer = putStrLn . show

main :: IO ()
main = outAnswer . tmain =<< B.getContents