import Data.List

dataSet :: [[Int]] -> Int -> Int -> Int -> [Int] -> (Int,[Int])
dataSet [] _ m wcd misp = ((m - 1) - wcd,misp)
dataSet ([win,mis]:xs) n m wcd misp
  | win >= n = dataSet xs n m (wcd+1) misp
  | otherwise = dataSet xs n m wcd ((mis-n):misp)

calc :: (Int,[Int]) -> Int
calc (0,_) = 0
calc (nes,misp) = sum $ take nes (sort misp)

main = do
  n_m <- getLine
  contents <- getContents
  let 
    [n,m] = map read $ words n_m :: [Int]
    lst = map  (map read) $ map words $ lines contents :: [[Int]]
    cost = calc $ dataSet lst n m 0 []
  putStrLn $ show cost
