fun main(args: Array<String>) {
    val pw = java.io.PrintWriter(System.out)
    Problem.solve(java.util.Scanner(System.`in`), pw)
    pw.flush()
}

object Problem {
    fun <A : Comparable<A>> maxPriorityQueueOf(a: Collection<A>): java.util.Queue<A> {
        val q = java.util.PriorityQueue<A>(a.count()) { x, y ->
            return@PriorityQueue if (y > x) 1 else -1
        }
        q.addAll(a)
        return q
    }

    fun solve(sc: java.util.Scanner, pw: java.io.PrintWriter) {
        val N = sc.next().toInt()
        val M = sc.next().toInt()
        val AB = Array(N) { Pair(sc.next().toInt(), sc.next().toInt()) }
            .sortedBy { it.first }

        val ans = mutableListOf<Int>()
        val maxQ = maxPriorityQueueOf(listOf(0))
        var c = 0
        for (i in 0..N) {
            while (i == N || AB[i].first > c) {
                if (maxQ.isNotEmpty()) {
                    val max = maxQ.first()
                    maxQ.remove(max)
                    ans += max
                }
                c++
                if (c > M) break
            }
            if (c > M || i == N) break
            maxQ.add(AB[i].second)
        }
        pw.println(ans.sum())
    }
}
