fun main(args:Array<String>):Unit{
    val (n, m) = readLine()!!.split(' ').map(String::toInt)
    f(n, m)
}
tailrec fun f(n:Int, m:Int):Unit{
    if (n != 0 && m != 0){
        val difficulty = Array(m){0}
        for (void in 0 until n){
            val (d, v) = readLine()!!.split(' ').map(String::toInt)
            if (difficulty[d - 1] < v){
                difficulty[d - 1] = v
            }
        }
        println(difficulty.sum())
        val (nn, nm) = readLine()!!.split(' ').map(String::toInt)
        f(nn, nm)
    }
}
