{-# 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 qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as VM
import Control.Monad
import Control.Monad.ST
import Data.List
import Debug.Trace

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

  act = do
    grp <- uftInit (n+1)
    mapM_ (uncurry (uftMerge grp)) xys
    vec <- VM.replicate (n+1) ([], [])
    forM_ (zip [n,n-1..1] (reverse ps)) $ \(i,p) ->
      uftRoot grp i >>= VM.modify vec (\(is, ps) -> (i:is, p:ps))
    foldl' (op vec) (return 0) [1..n] where
      op vec msum j = do
        sum <- msum
        (is, ps) <- VM.read vec j
        return $ sum + IS.size (IS.intersection (IS.fromDistinctAscList is)
                                                (IS.fromList ps))

uftInit :: Int -> ST s (VUM.MVector s Int)
uftInit size = do
  vec <- VUM.new size
  forM_ [0..size-1] $ \i -> VUM.write vec i i
  return vec

uftMerge :: VUM.MVector s Int -> Int -> Int -> ST s ()
uftMerge vec x y = do
  rx <- uftRoot vec x
  ry <- uftRoot vec y
  if rx == ry then return ()
              else VUM.write vec rx ry

uftRoot :: VUM.MVector s Int -> Int -> ST s Int
uftRoot vec x = do
  y <- VUM.read vec x
  if x == y then return x
            else uftRoot vec y >>= vum_write_ret vec x

vum_write_ret :: VUM.Unbox a => VUM.MVector s a -> Int -> a -> ST s a
vum_write_ret vec i a = VUM.write vec i a >> return a

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
