import java.util.*

fun main(args: Array<String>) {
    val (N, C) = readLine()!!.split(" ").map{it.toLong()}

    val cal = TreeMap<Long, Long>()
    val rcal = TreeMap<Long, Long>()

    for (i in 1..N) {
        val (x, v) = readLine()!!.split(" ").map{it.toLong()}
        val e = cal.lowerEntry(x)
        cal[x] = v + (e?.value ?: 0L) - (x - (e?.key ?: 0L))
        rcal[C - x] = v
    }

    for (k in rcal.keys) {
        val e = rcal.lowerEntry(k)
        rcal[k] = rcal[k]!! + (e?.value ?: 0L) - (k - (e?.key ?: 0L))
    }

    fun func(cal: TreeMap<Long, Long>) {
        for (k in cal.keys) {
            cal[k] = Math.max(cal[k]!!, cal.lowerEntry(k)?.value ?: 0L)
        }
    }

    func(cal)
    func(rcal)

    fun calc(mp: TreeMap<Long, Long>, rmp: TreeMap<Long, Long>): Long {
        var r = 0L

        for (e in mp.entries) {
            r = Math.max(r, e.value)

            val t = rmp.lowerEntry(C - e.key)?.value ?: 0L
            r = Math.max(r, e.value + t - e.key)
        }

        return r
    }

    println(Math.max(calc(cal, rcal), calc(rcal, cal)))
}
