N,C = list(map(int,input().split()))

src = [tuple(map(int,input().split())) for i in range(N)]



cumsumr = [0]

cummaxr = [0]

cummaxr2 = [0]

px = 0

for x,v in src:

    cumsumr.append(cumsumr[-1] + v-(x-px))

    cummaxr.append(max(cummaxr[-1], cumsumr[-1]))

    cummaxr2.append(max(cummaxr2[-1], cumsumr[-1]-x))

    px = x



cumsuml = [0]

cummaxl = [0]

cummaxl2 = [0]

px = C

for x,v in src[::-1]:

    cumsuml.append(cumsuml[-1] + v-(px-x))

    cummaxl.append(max(cummaxl[-1], cumsuml[-1]))

    cummaxl2.append(max(cummaxl2[-1], cumsuml[-1]-(C-x)))

    px = x



ans = max(cummaxr[-1], cummaxl[-1])

for i in range(N):

    ans = max(ans, cummaxr2[i] + cummaxl[N-i])

    ans = max(ans, cummaxl2[i] + cummaxr[N-i])

print(ans)
import sys

input = sys.stdin.readline

N,C = list(map(int,input().split()))

XV = [tuple(map(int,input().split())) for i in range(N)]



rcum1 = [0]

rcum2 = [0]

rcummx = [0]

px = 0

for x,v in XV:

    d = x - px

    rcum1.append(rcum1[-1] + v - d)

    rcum2.append(rcum2[-1] + v - 2*d)

    rcummx.append(max(rcummx[-1], rcum1[-1]))

    px = x



lcum1 = [0]

lcum2 = [0]

lcummx = [0]

px = C

for x,v in reversed(XV):

    d = px - x

    lcum1.append(lcum1[-1] + v - d)

    lcum2.append(lcum2[-1] + v - 2*d)

    lcummx.append(max(lcummx[-1], lcum1[-1]))

    px = x

lcum1.reverse()

lcum2.reverse()

lcummx.reverse()



ans = max(rcummx[-1], lcummx[0])

for i in range(N+1):

    a = rcum2[i] + lcummx[i]

    b = lcum2[i] + rcummx[i]

    ans = max(ans, a, b)

print(ans)
N, C = list(map(int, input().split()))
src = [tuple(map(int, input().split())) for i in range(N)]
cumsumr = [0]
cummaxr = [0]
cummaxr2 = [0]
px = 0
for x, v in src:
    cumsumr.append(cumsumr[-1] + v - (x - px))
    cummaxr.append(max(cummaxr[-1], cumsumr[-1]))
    cummaxr2.append(max(cummaxr2[-1], cumsumr[-1] - x))
    px = x
cumsuml = [0]
cummaxl = [0]
cummaxl2 = [0]
px = C
for x, v in src[::-1]:
    cumsuml.append(cumsuml[-1] + v - (px - x))
    cummaxl.append(max(cummaxl[-1], cumsuml[-1]))
    cummaxl2.append(max(cummaxl2[-1], cumsuml[-1] - (C - x)))
    px = x
ans = max(cummaxr[-1], cummaxl[-1])
for i in range(N):
    ans = max(ans, cummaxr2[i] + cummaxl[N - i])
    ans = max(ans, cummaxl2[i] + cummaxr[N - i])
print(ans)

import sys

input = sys.stdin.readline
N, C = list(map(int, input().split()))
XV = [tuple(map(int, input().split())) for i in range(N)]
rcum1 = [0]
rcum2 = [0]
rcummx = [0]
px = 0
for x, v in XV:
    d = x - px
    rcum1.append(rcum1[-1] + v - d)
    rcum2.append(rcum2[-1] + v - 2 * d)
    rcummx.append(max(rcummx[-1], rcum1[-1]))
    px = x
lcum1 = [0]
lcum2 = [0]
lcummx = [0]
px = C
for x, v in reversed(XV):
    d = px - x
    lcum1.append(lcum1[-1] + v - d)
    lcum2.append(lcum2[-1] + v - 2 * d)
    lcummx.append(max(lcummx[-1], lcum1[-1]))
    px = x
lcum1.reverse()
lcum2.reverse()
lcummx.reverse()
ans = max(rcummx[-1], lcummx[0])
for i in range(N + 1):
    a = rcum2[i] + lcummx[i]
    b = lcum2[i] + rcummx[i]
    ans = max(ans, a, b)
print(ans)

+import sys + +input = sys.stdin.readline -src = [tuple(map(int, input().split())) for i in range(N)] -cumsumr = [0] -cummaxr = [0] -cummaxr2 = [0] +XV = [tuple(map(int, input().split())) for i in range(N)] +rcum1 = [0] +rcum2 = [0] +rcummx = [0] -for x, v in src: -    cumsumr.append(cumsumr[-1] + v - (x - px)) -    cummaxr.append(max(cummaxr[-1], cumsumr[-1])) -    cummaxr2.append(max(cummaxr2[-1], cumsumr[-1] - x)) +for x, v in XV: +    d = x - px +    rcum1.append(rcum1[-1] + v - d) +    rcum2.append(rcum2[-1] + v - 2 * d) +    rcummx.append(max(rcummx[-1], rcum1[-1])) -cumsuml = [0] -cummaxl = [0] -cummaxl2 = [0] +lcum1 = [0] +lcum2 = [0] +lcummx = [0] -for x, v in src[::-1]: -    cumsuml.append(cumsuml[-1] + v - (px - x)) -    cummaxl.append(max(cummaxl[-1], cumsuml[-1])) -    cummaxl2.append(max(cummaxl2[-1], cumsuml[-1] - (C - x))) +for x, v in reversed(XV): +    d = px - x +    lcum1.append(lcum1[-1] + v - d) +    lcum2.append(lcum2[-1] + v - 2 * d) +    lcummx.append(max(lcummx[-1], lcum1[-1])) -ans = max(cummaxr[-1], cummaxl[-1]) -for i in range(N): -    ans = max(ans, cummaxr2[i] + cummaxl[N - i]) -    ans = max(ans, cummaxl2[i] + cummaxr[N - i]) +lcum1.reverse() +lcum2.reverse() +lcummx.reverse() +ans = max(rcummx[-1], lcummx[0]) +for i in range(N + 1): +    a = rcum2[i] + lcummx[i] +    b = lcum2[i] + rcummx[i] +    ans = max(ans, a, b)