import Control.Monad
import Data.Maybe
import qualified Data.ByteString.Char8 as BS
import Data.List
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as VM
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM

readInt = fst . fromJust . BS.readInt
readIntList = map readInt . BS.words
getInt = readInt <$> BS.getLine
getIntList = readIntList <$> BS.getLine


----------
data Heap a = Empty | Heap Int a (Heap a) (Heap a) deriving Show

empty = Empty

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@(Heap _ x l1 r1) h2@(Heap _ y l2 r2)
    | x > y     = makeHeap x l1 (merge r1 h2)
    | otherwise = makeHeap y l2 (merge r2 h1)

rank :: Heap a -> Int
rank Empty = 0
rank (Heap r _ _ _) = r

makeHeap :: a -> Heap a -> Heap a -> Heap a
makeHeap x a b
    | ra >= rb  = Heap (rb + 1) x a b
    | otherwise = Heap (ra + 1) x b a
    where ra = rank a
          rb = rank b

--挿入
heapInsert :: Ord a => Heap a -> a -> Heap a
heapInsert h x = merge (singleton x) h

--リストから
heapFromList :: Ord a => [a] -> Heap a
heapFromList = foldl heapInsert Empty

--リストへ
heapToList :: Ord a => Heap a -> [a]
heapToList h | isEmpty h = []
             | otherwise = let (x, h') = deleteMax h in x : heapToList h'

--最大値の取り出し（削除）
deleteMax :: Ord a => Heap a -> (a, Heap a)
deleteMax Empty = error "Empty Heap"
deleteMax (Heap _ x a b) = (x, merge a b)

--最大値を見る（削除しない）
findMax :: Heap a -> a
findMax Empty = error "Empty Heap"
findMax (Heap _ x _ _) = x

--空か
isEmpty :: Heap a -> Bool
isEmpty Empty = True
isEmpty _     = False

----------

main = do
    [n,m] <- getIntList
    ab <- replicateM n $ do
        [a,b] <- getIntList
        return (a,b)
    
    let works = V.create $ do
            vec <- VM.replicate (10^5 + 1) empty
            forM_ ab $ \(a,b) -> do
                VM.modify vec (flip heapInsert b) a
            return vec
    
    let result = fst $ foldl' f (0,empty) [1..m]
         where f (s,h) i = (s+b, newh)
                  where h' = merge h $ works V.! i
                        (b, newh) | isEmpty h' = (0,empty)
                                  | otherwise = deleteMax $ h'

    print result