import qualified Data.ByteString.Char8 as BS
import Data.Maybe
import Data.List
import Data.Function
import qualified Data.Set as S
import Data.Array.IArray

solve m xs =
    let set = f 1 S.empty
        arr = listArray (0,length xs - 1) xs :: Array Int (Int,Int)
     in S.foldr (\i a -> a + snd (arr!i)) 0 set
  where
    f :: Int -> S.Set Int -> S.Set Int
    f i s
      | i > m = s
      | otherwise =
          let xs' = [ x | x <- zip [0..] xs,
                      (fst . snd) x <= i && S.notMember (fst x) s ]
              maxOf = maximumBy (compare `on` (snd . snd))
           in case xs' of
               [] -> f (i+1) s
               _  -> let (p,_) = maxOf xs'
                       in f (i+1) (S.insert p s)

main :: IO ()
main = do
    [n,m] <- map read . words <$> getLine :: IO [Int]
    xs <- map ((\[a,b] -> (a,b))
        . map (fst . fromJust . BS.readInt) . BS.words)
        . BS.lines <$> BS.getContents
    print $ solve m xs
