{-# LANGUAGE BangPatterns #-}
import Control.Arrow
import Control.Applicative
import Control.Monad.Fix
import Control.Monad.ST
import Data.Int
import Data.Foldable
import Data.List
import Data.Graph
import qualified Data.Vector as V
import qualified Data.Vector.Unboxed as VU
import qualified Data.ByteString.Char8 as B
import Debug.Trace

solve :: Int -> Int -> VU.Vector Int -> VU.Vector (Int, Int) -> Int
solve !n !m !pn !xys = sum $ map
  (\idxs -> countPairs (sort idxs) (sort $ map (pn VU.!) idxs))
  componentGroups
 where
  graph           = buildG (0, n - 1) (VU.toList xys)

  componentGroups = map group $ components graph
    where group (Node !n !fs) = n : concat (map group fs)

  countPairs xs ys = go xs ys 0
   where
    go _  [] !acc = acc
    go [] _  !acc = acc
    go (x : xs) (y : ys) !acc | x == y = go xs ys (acc + 1)
                              | x > y  = go (x : xs) ys acc
                              | x < y  = go xs (y : ys) acc

main = do
  let readInt :: Integral a => B.ByteString -> Maybe (a, B.ByteString)
      readInt = fmap (first fromIntegral . second B.tail) . B.readInt

  (n, m) <-
    (\vec -> (vec VU.! 0, vec VU.! 1)) . VU.unfoldrN 2 readInt <$> B.getLine
  pn  <- VU.map (\n -> n - 1) . VU.unfoldrN n readInt <$> B.getLine
  xys <-
    VU.replicateM m
    $   (\vec -> (vec VU.! 0 - 1, vec VU.! 1 - 1))
    .   VU.unfoldrN 2 readInt
    <$> B.getLine
  print $ solve n m pn xys
