from numpy import array as n

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

xy = [list(map(int, input().split())) for _ in range(N)]

r1, r2, l1, l2 = [0],[0],[0],[0]

sum = 0

for x, cal in xy:

    sum += cal

    r1.append(max(r1[-1], sum-x))

    r2.append(max(r2[-1], sum-2*x))

sum = 0

for x, cal in xy[::-1]:

    sum += cal

    l1.append(max(l1[-1], sum-C+x))

    l2.append(max(l2[-1], sum-2*(C-x)))

out = max(n(r1+r2)+n(l2[::-1]+l1[::-1]))

print(out)
def main():

    n, c = list(map(int, input().split()))

    xv = [list(map(int, input().split())) for _ in range(n)]

    v, left_max, right_max = 0, 0, 0

    gl = [None]*n

    gr = [None]*n

    _2oas = [None]*(n+1)

    _2obs = [None]*(n+1)

    _2oas[0], _2obs[0] = 0, 0

    ans = 0

    for i, (xi, vi) in enumerate(xv):

        v += vi

        left = v - xi

        if left_max < left:

            left_max = left

        gl[i] = left_max

        _2oas[i+1] = v - 2*xi

    v = 0

    for i, (xi, vi) in enumerate(xv[::-1]):

        v += vi

        right = v - c + xi

        if right_max < right:

            right_max = right

        gr[i] = right_max

        _2obs[i+1] = v - 2*(c - xi)

    for i in range(1, n+1):

        oa = gl[-i]

        _2ob = _2obs[i-1]

        ob = gr[-i]

        _2oa = _2oas[i-1]

        if ans < _2oa+ob:

            ans = _2oa+ob

        if ans < _2ob+oa:

            ans = _2ob+oa

    print(ans)

        

if __name__ == "__main__":

    main()
from numpy import array as n

N, C = list(map(int, input().split()))
xy = [list(map(int, input().split())) for _ in range(N)]
r1, r2, l1, l2 = [0], [0], [0], [0]
sum = 0
for x, cal in xy:
    sum += cal
    r1.append(max(r1[-1], sum - x))
    r2.append(max(r2[-1], sum - 2 * x))
sum = 0
for x, cal in xy[::-1]:
    sum += cal
    l1.append(max(l1[-1], sum - C + x))
    l2.append(max(l2[-1], sum - 2 * (C - x)))
out = max(n(r1 + r2) + n(l2[::-1] + l1[::-1]))
print(out)

def main():
    n, c = list(map(int, input().split()))
    xv = [list(map(int, input().split())) for _ in range(n)]
    v, left_max, right_max = 0, 0, 0
    gl = [None] * n
    gr = [None] * n
    _2oas = [None] * (n + 1)
    _2obs = [None] * (n + 1)
    _2oas[0], _2obs[0] = 0, 0
    ans = 0
    for i, (xi, vi) in enumerate(xv):
        v += vi
        left = v - xi
        if left_max < left:
            left_max = left
        gl[i] = left_max
        _2oas[i + 1] = v - 2 * xi
    v = 0
    for i, (xi, vi) in enumerate(xv[::-1]):
        v += vi
        right = v - c + xi
        if right_max < right:
            right_max = right
        gr[i] = right_max
        _2obs[i + 1] = v - 2 * (c - xi)
    for i in range(1, n + 1):
        oa = gl[-i]
        _2ob = _2obs[i - 1]
        ob = gr[-i]
        _2oa = _2oas[i - 1]
        if ans < _2oa + ob:
            ans = _2oa + ob
        if ans < _2ob + oa:
            ans = _2ob + oa
    print(ans)


if __name__ == "__main__":
    main()

-from numpy import array as n +def main(): +    n, c = list(map(int, input().split())) +    xv = [list(map(int, input().split())) for _ in range(n)] +    v, left_max, right_max = 0, 0, 0 +    gl = [None] * n +    gr = [None] * n +    _2oas = [None] * (n + 1) +    _2obs = [None] * (n + 1) +    _2oas[0], _2obs[0] = 0, 0 +    ans = 0 +    for i, (xi, vi) in enumerate(xv): +        v += vi +        left = v - xi +        if left_max < left: +            left_max = left +        gl[i] = left_max +        _2oas[i + 1] = v - 2 * xi +    v = 0 +    for i, (xi, vi) in enumerate(xv[::-1]): +        v += vi +        right = v - c + xi +        if right_max < right: +            right_max = right +        gr[i] = right_max +        _2obs[i + 1] = v - 2 * (c - xi) +    for i in range(1, n + 1): +        oa = gl[-i] +        _2ob = _2obs[i - 1] +        ob = gr[-i] +        _2oa = _2oas[i - 1] +        if ans < _2oa + ob: +            ans = _2oa + ob +        if ans < _2ob + oa: +            ans = _2ob + oa +    print(ans) -N, C = list(map(int, input().split())) -xy = [list(map(int, input().split())) for _ in range(N)] -r1, r2, l1, l2 = [0], [0], [0], [0] -sum = 0 -for x, cal in xy: -    sum += cal -    r1.append(max(r1[-1], sum - x)) -    r2.append(max(r2[-1], sum - 2 * x)) -sum = 0 -for x, cal in xy[::-1]: -    sum += cal -    l1.append(max(l1[-1], sum - C + x)) -    l2.append(max(l2[-1], sum - 2 * (C - x))) -out = max(n(r1 + r2) + n(l2[::-1] + l1[::-1])) -print(out) + +if __name__ == "__main__": +    main()