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

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

xc.sort()

ans=0

p=0

#右を取る範囲と左を取る範囲を決めればいい

s=[0]*(N+1)#時計回り

ss=[0]*(N+1)#反時計回り

for i in range(N):

  s[i+1]=s[i]+xc[i][1]



for i in range(N):

  ss[i+1]=ss[i]+xc[N-1-i][1]

maxs=[0]*(N+1)

maxss=[0]*(N+1)

for i in range(N):

  s[i+1]=s[i+1]-xc[i][0]

  ss[i+1]=ss[i+1]-(C-xc[N-1-i][0])

  maxs[i+1]=max(maxs[i],s[i+1])

  maxss[i+1]=max(maxss[i],ss[i+1])

ans=max(0,maxs[-1])

ans=max(ans,maxss[-1])

for i in range(N):

  if s[i+1]>xc[i][0]:

    ans=max(ans,maxss[N-i-1]+(s[i+1]-xc[i][0]))

  if ss[i+1]>(C-xc[N-1-i][0]):

    ans=max(ans,maxs[N-i-1]+(ss[i+1]-(C-xc[N-1-i][0])))

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



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

xv2 = [(C - x, v)for x, v in xv[::-1]]





L = [0]*N

ans = 0

for i, X in enumerate(xv):

    x, v = X

    L[i] = L[i-1] + v

R = [0]*N

for i in range(N):

    L[i] -= xv[i][0]

for i, X in enumerate(xv2):

    x, v = X

    R[i] = R[i-1] + v

for i in range(N):

    R[i] -= xv2[i][0]



LL = [0]*(N+1)

RR = [0]*(N+1)

for i in range(N):

    LL[i+1] = max(LL[i], L[i])

    RR[i+1] = max(RR[i], R[i])



ans = 0

ans = max(ans, LL[-1])

ans = max(ans, RR[-1])

for i in range(N):

    ans = max(ans, LL[N - 1 - i] + R[i] - xv2[i][0])

    ans = max(ans, RR[N - 1 - i] + L[i] - xv[i][0])

print(ans)

N, C = list(map(int, input().split()))
xc = [tuple(map(int, input().split())) for i in range(N)]
xc.sort()
ans = 0
p = 0
# 右を取る範囲と左を取る範囲を決めればいい
s = [0] * (N + 1)  # 時計回り
ss = [0] * (N + 1)  # 反時計回り
for i in range(N):
    s[i + 1] = s[i] + xc[i][1]
for i in range(N):
    ss[i + 1] = ss[i] + xc[N - 1 - i][1]
maxs = [0] * (N + 1)
maxss = [0] * (N + 1)
for i in range(N):
    s[i + 1] = s[i + 1] - xc[i][0]
    ss[i + 1] = ss[i + 1] - (C - xc[N - 1 - i][0])
    maxs[i + 1] = max(maxs[i], s[i + 1])
    maxss[i + 1] = max(maxss[i], ss[i + 1])
ans = max(0, maxs[-1])
ans = max(ans, maxss[-1])
for i in range(N):
    if s[i + 1] > xc[i][0]:
        ans = max(ans, maxss[N - i - 1] + (s[i + 1] - xc[i][0]))
    if ss[i + 1] > (C - xc[N - 1 - i][0]):
        ans = max(ans, maxs[N - i - 1] + (ss[i + 1] - (C - xc[N - 1 - i][0])))
print(ans)

N, C = list(map(int, input().split()))
xv = [tuple(map(int, input().split())) for i in range(N)]
xv2 = [(C - x, v) for x, v in xv[::-1]]
L = [0] * N
ans = 0
for i, X in enumerate(xv):
    x, v = X
    L[i] = L[i - 1] + v
R = [0] * N
for i in range(N):
    L[i] -= xv[i][0]
for i, X in enumerate(xv2):
    x, v = X
    R[i] = R[i - 1] + v
for i in range(N):
    R[i] -= xv2[i][0]
LL = [0] * (N + 1)
RR = [0] * (N + 1)
for i in range(N):
    LL[i + 1] = max(LL[i], L[i])
    RR[i + 1] = max(RR[i], R[i])
ans = 0
ans = max(ans, LL[-1])
ans = max(ans, RR[-1])
for i in range(N):
    ans = max(ans, LL[N - 1 - i] + R[i] - xv2[i][0])
    ans = max(ans, RR[N - 1 - i] + L[i] - xv[i][0])
print(ans)

-xc = [tuple(map(int, input().split())) for i in range(N)] -xc.sort() +xv = [tuple(map(int, input().split())) for i in range(N)] +xv2 = [(C - x, v) for x, v in xv[::-1]] +L = [0] * N -p = 0 -# 右を取る範囲と左を取る範囲を決めればいい -s = [0] * (N + 1)  # 時計回り -ss = [0] * (N + 1)  # 反時計回り +for i, X in enumerate(xv): +    x, v = X +    L[i] = L[i - 1] + v +R = [0] * N -    s[i + 1] = s[i] + xc[i][1] +    L[i] -= xv[i][0] +for i, X in enumerate(xv2): +    x, v = X +    R[i] = R[i - 1] + v -    ss[i + 1] = ss[i] + xc[N - 1 - i][1] -maxs = [0] * (N + 1) -maxss = [0] * (N + 1) +    R[i] -= xv2[i][0] +LL = [0] * (N + 1) +RR = [0] * (N + 1) -    s[i + 1] = s[i + 1] - xc[i][0] -    ss[i + 1] = ss[i + 1] - (C - xc[N - 1 - i][0]) -    maxs[i + 1] = max(maxs[i], s[i + 1]) -    maxss[i + 1] = max(maxss[i], ss[i + 1]) -ans = max(0, maxs[-1]) -ans = max(ans, maxss[-1]) +    LL[i + 1] = max(LL[i], L[i]) +    RR[i + 1] = max(RR[i], R[i]) +ans = 0 +ans = max(ans, LL[-1]) +ans = max(ans, RR[-1]) -    if s[i + 1] > xc[i][0]: -        ans = max(ans, maxss[N - i - 1] + (s[i + 1] - xc[i][0])) -    if ss[i + 1] > (C - xc[N - 1 - i][0]): -        ans = max(ans, maxs[N - i - 1] + (ss[i + 1] - (C - xc[N - 1 - i][0]))) +    ans = max(ans, LL[N - 1 - i] + R[i] - xv2[i][0]) +    ans = max(ans, RR[N - 1 - i] + L[i] - xv[i][0])