
class BinaryIndexedMaxTree(val size:Int){
    private val array = Array<Long>(size, {0})
    private fun up(idx:Int):Int {//左端の0を1に変える
        return idx + (idx.inv() and (idx + 1))
    }
    private fun left(idx:Int):Int {//左端の
        return idx - (idx.inv() and (idx + 1))
    }
    operator fun get(index:Int):Long{
        var max:Long = 0
        var idx = index
        while (idx >= 0){
            if (array[idx] > max) max = array[idx]
            idx = left(idx)
        }
        return max
    }
    fun increase(index:Int, value:Long):Unit{
        var idx = index
        while(idx < size){
            if (array[idx] < value) array[idx] = value
            idx = up(idx)
        }
    }
}
data class SushiDish(val distance:Long, val caloric:Long)
fun main(args:Array<String>):Unit{
    val (ns, cs) = readLine()!!.split(' ')
    val (n, c) = Pair(ns.toInt(), cs.toLong())
    val sushis = Array(n, {val (d, c) = readLine()!!.trim().split(' ').map(String::toLong); SushiDish(d, c)})
    val rBit = BinaryIndexedMaxTree(n)//rBit[idx] => 時計回りにidx + 1番目の寿司を食べた場合の最大摂取カロリー
    val lBit = BinaryIndexedMaxTree(n)//lBit[idx] => 反時計回りにn - idx番目の寿司を食べた場合の最大摂取カロリー
    val rrBit = BinaryIndexedMaxTree(n + 1)//rrBit[idx] => 時計回りにidx番目の寿司まで食べてスタート地点に戻った場合の最大摂取カロリー
    val rlBit = BinaryIndexedMaxTree(n + 1)//rlBit[idx] => 反時計回りにn - idx + 1番目の寿司まで食べてスタート地点に戻った場合の最大摂取カロリー
    var sumCaloric:Long = 0
    for ((idx, sushi) in sushis.withIndex()){
        sumCaloric += sushi.caloric
        if (sumCaloric > sushi.distance) rBit.increase(idx, sumCaloric - sushi.distance)
        if (sumCaloric > sushi.distance * 2) rrBit.increase(idx + 1, sumCaloric - sushi.distance * 2)
    }
    sumCaloric = 0
    for ((idx, sushi) in sushis.reversed().withIndex()){
        sumCaloric += sushi.caloric
        if (sumCaloric > c - sushi.distance) lBit.increase(idx, sumCaloric - c + sushi.distance)
        if (sumCaloric > (c - sushi.distance) * 2) rlBit.increase(idx + 1, sumCaloric - (c - sushi.distance) * 2)
    }
    println((0 until n).map{idx -> listOf(rBit[idx] + rlBit[n - idx - 1], lBit[idx] + rrBit[n - idx - 1]).max()}.filterNotNull().max())
}