{-# OPTIONS_GHC -O #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ViewPatterns #-}

import Control.Monad
import Data.List
import qualified Data.ByteString.Char8 as B
import qualified Data.Vector.Storable as V
import qualified Data.Vector.Storable.Mutable as MV

findUF :: MV.IOVector Int -> Int -> IO Int
findUF uf x = do
    p <- MV.unsafeRead uf x
    if p == -1
        then return x
        else do
                f <- findUF uf p
                MV.unsafeWrite uf x f
                return f

main = do
    [n,m] <- map read.words <$> getLine

    -- 順列読込 / ByteString -> Vector.Storable
    ps <- V.unfoldr (B.readInt.B.dropWhile (==' ')) <$> B.getLine

    -- (xi, yi)をUnionFind -> Vector.Storable.Mutable
    uf <- MV.replicate (n+1) (-1)
    replicateM_ m $ do
        [x,y] <- unfoldr (B.readInt.B.dropWhile (==' ')) <$> B.getLine
        px <- findUF uf x
        py <- findUF uf y
        when (px /= py) $ MV.unsafeWrite uf py px

    -- (i == pi)の数
    (print.length.filter id) =<< mapM (chk uf ps) [1..n]

chk uf ps i = do
    ri <- findUF uf i
    pi <- findUF uf $ ps V.! (i-1)
    return $ ri == pi