import Control.Applicative
import Control.Monad

type Shuffle = (Int, Int)

myRead' :: String -> Shuffle
myRead' xs = (a, b)
  where (a:b:_) = map (read :: String -> Int) $ words xs

myRead :: [String] -> [Shuffle]
myRead = map myRead'

operation :: [Int] -> Shuffle -> [Int]
operation cards (x, y) =
  let a = take x cards
      b = take (y - x) $ drop x cards
      c = drop y cards
  in
      c ++ b ++ a

solver :: [Int] -> [Shuffle] -> Int -> Int -> Int -> Int
solver cards op p q r =
  let result  = foldl operation cards op
      ansList = drop (p - 1) $ take q result
  in
      length $ filter (<= r) ansList

main :: IO()
main = do
  n <- (read :: String -> Int) <$> getLine -- number of cards
  unless (n == 0) $ do
    m <- (read :: String -> Int) <$> getLine -- number of shuffles
    (p:q:r:_) <- map (read :: String -> Int) . words <$> getLine
    shuffles <- myRead <$> replicateM m getLine
    print $ solver [1..n] shuffles p q r
    main