{-# LANGUAGE PartialTypeSignatures #-}
module Main (main) where

import Control.Monad
import Control.Monad.Trans.State
import qualified Data.ByteString.Unsafe as B
import qualified Data.ByteString.Char8 as C8
import Data.Array
import qualified Data.Char as C
import qualified Data.List as L

main :: IO ()
main = do
  [n,m] <- readInts
  hs <- readInts
  replicateM m readPair >>= output . solve n hs

solve :: Int -> [Int] -> _ -> _
solve n hs = length . filter (/=0) . elems . (arr//) . flip zip [0,0..] . go
  where
    arr = listArray (1,n) hs

    go [] = []
    go ((a,b):xs)
      | arr!a < arr!b = a : go xs
      | arr!a > arr!b = b : go xs
      | otherwise = a : b : go xs

output :: _ -> IO ()
output = print

-- utils
readInts :: IO [Int]
readInts = L.unfoldr (C8.readInt . C8.dropWhile C.isSpace) <$> C8.getLine

readPair :: IO (Int, Int)
readPair = head . L.unfoldr readT <$> C8.getLine
  where
    readT = runStateT $
      (,) <$> StateT (C8.readInt . C8.dropWhile C.isSpace)
          <*> StateT (C8.readInt . B.unsafeTail)
