{-# LANGUAGE OverloadedStrings #-}
import Prelude hiding (null, elem)
import Control.Monad
import Data.Char
import Data.List hiding (insert, null, elem)
import Data.Maybe
import qualified Data.ByteString.Char8 as BS
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as MV
import qualified Data.Vector.Unboxed as UV

main :: IO ()
main = do
  [n, m] <- getIntList
  jobs <- MV.replicate m []
  replicateM_ n $ do
    [day, reward] <- getIntList
    if day <= m
      then MV.modify jobs (reward:) (day-1)
      else return ()
  print . solve =<< (V.freeze jobs)

solve :: V.Vector [Int] -> Int
solve jobs = go [0..m-1] empty
  where
    m = V.length jobs
    go [] _ = 0
    go (d:days) qs = p + go days qs''
      where
        rewards = jobs V.! d
        qs' = merge qs $ fromList rewards
        (p, qs'') = if null qs' then (0, qs') else deleteFindMax qs'

-- Leftist Heap
data Heap a =
  Empty |
  Heap {
    rank :: Int,
    elem :: a,
    left :: Heap a,
    right ::Heap a}

empty :: Heap a
empty = Empty

null :: Heap a -> Bool
null Empty = True
null (Heap _ _ _ _) = False 

singleton :: a -> Heap a
singleton x = Heap 1 x Empty Empty

merge :: Ord a => Heap a -> Heap a -> Heap a
merge h     Empty = h
merge Empty h     = h
merge h1 h2
  | elem h1 > elem h2 = makeHeap (elem h1) (left h1) (merge (right h1) h2)
  | otherwise = makeHeap (elem h2) (left h2) (merge (right h2) h1)

makeHeap :: a -> Heap a -> Heap a -> Heap a
makeHeap x a b =
  if ra >= rb then Heap (rb + 1) x a b else Heap (ra + 1) x b a
  where 
    rb = if null b then 0 else rank b
    ra = if null a then 0 else rank a

insert :: Ord a => a -> Heap a -> Heap a
insert x h = merge (singleton x) h

deleteFindMax :: Ord a => Heap a -> (a, Heap a)
deleteFindMax Empty = error "Empty Heap"
deleteFindMax (Heap _ x a b) = (x, merge a b)

fromList :: Ord a => [a] -> Heap a
fromList = foldl (flip insert) Empty

toList :: Ord a => Heap a -> [a]
toList h
  | null h = []
  | otherwise = let (x, h') = deleteFindMax h
                in x : toList h'
-- IO
readInt :: BS.ByteString -> Int
readInt = fst . fromJust . BS.readInt

getInt :: IO Int
getInt = readInt <$> BS.getLine

getIntList :: IO [Int]
getIntList = unfoldr (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine

getIntVec :: Int -> IO (UV.Vector Int)
getIntVec n = UV.unfoldrN n (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine