import Control.Applicative
import Control.Monad
import Data.List (foldl')

main :: IO ()
main = do
  n <- readLn
  unless (n == 0) $ do
    m <- readLn
    solve n <$> f <*> replicateM m f >>= print
    main
  where
    f = map read <$> words <$> getLine

solve :: Int -> [Int] -> [[Int]] -> Int
solve n [p, q, r] =  g . foldl' f il
  where
    il = if n == r then [(r, True)] else [(r, True),(n-r, False)]
    f ls [x, y] = let (as, bs) = split x ls
                      (cs, ds) = split (y-x) bs
                  in ds ++ cs ++ as
    g ls = let (_, as) = split (p-1) ls
               (bs, _) = split (q - p + 1) as
           in sum . map fst . filter snd $ bs
    
split :: Int -> [(Int,Bool)] -> ([(Int,Bool)], [(Int,Bool)])
split _ [] = ([], [])
split n ((x,b):xs)
  | n == 0 = ([], (x,b):xs)
  | n < x = ([(n, b)], (x-n, b):xs)
  | otherwise = let (as, bs) = split (n - x) xs
                in ((x,b):as, bs)

