import sys

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

table=[]

for i in range(N):

    x,v=list(map(int,input().split()))

    table.append([x,v])

ans=0

if N==1:

    ans=max(0,table[0][1]-table[0][0],table[0][1]+table[0][0]-C)

    print(ans)

    sys.exit()

V=[0]*N

clo=[0]*N

V[0]=table[0][1]

clo[0]=V[0]-table[0][0]

for i in range(1,N):

    V[i]=V[i-1]+table[i][1]

    clo[i]=max(clo[i-1],V[i]-table[i][0])

    ans = max(clo[i],ans)

rclo=[0]*N

rclo[N-1]=V[N-1]-V[N-2]-C+table[N-1][0]

for i in range(N-1,1,-1):

    rclo[i-1]=max(rclo[i],V[N-1]-V[i-2]-C+table[i-1][0])

    ans = max(clo[i],ans)

rclo[0]=max(rclo[1],V[N-1]-C+table[0][0])

ans = max(rclo[0],ans)



for i in range(N-1):

    t = V[i]+rclo[i+1]-2*table[i][0]

    ans=max(t,ans)

for i in range(N-1,0,-1):

    t = V[N-1]-V[i-1] + clo[i-1]-2*C+2*table[i][0]

    ans=max(t,ans)



print(ans)





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

X=[]

V=[]

for i in range(N):

    x,v=list(map(int,input().split()))

    X.append(x)

    V.append(v)

#時計回りで見る

R=[0]*(N+1)#0番目は１に格納する

Rans=[0]*(N+1)

for i in range(N):

    R[i+1]=R[i]+V[i]

    Rans[i+1]=max(Rans[i],R[i+1]-X[i])

L=[0]*(N+1)

Lans=[0]*(N+1)

for i in range(N,0,-1):

    L[i-1]=L[i]+V[i-1]#N-1にN-1を格納

    Lans[i-1]=max(Lans[i],L[i-1]-C+X[i-1])

ans_1=max(Rans[N],Lans[0])

ans_2=max(Lans[i+1]+R[i+1]-2*X[i] for i in range(N))

ans_3=max(Rans[i-1]+L[i-1]-2*C+2*X[i-1] for i in range(N,0,-1))

print((max(ans_1,ans_2,ans_3)))
import sys

N, C = list(map(int, input().split()))
table = []
for i in range(N):
    x, v = list(map(int, input().split()))
    table.append([x, v])
ans = 0
if N == 1:
    ans = max(0, table[0][1] - table[0][0], table[0][1] + table[0][0] - C)
    print(ans)
    sys.exit()
V = [0] * N
clo = [0] * N
V[0] = table[0][1]
clo[0] = V[0] - table[0][0]
for i in range(1, N):
    V[i] = V[i - 1] + table[i][1]
    clo[i] = max(clo[i - 1], V[i] - table[i][0])
    ans = max(clo[i], ans)
rclo = [0] * N
rclo[N - 1] = V[N - 1] - V[N - 2] - C + table[N - 1][0]
for i in range(N - 1, 1, -1):
    rclo[i - 1] = max(rclo[i], V[N - 1] - V[i - 2] - C + table[i - 1][0])
    ans = max(clo[i], ans)
rclo[0] = max(rclo[1], V[N - 1] - C + table[0][0])
ans = max(rclo[0], ans)
for i in range(N - 1):
    t = V[i] + rclo[i + 1] - 2 * table[i][0]
    ans = max(t, ans)
for i in range(N - 1, 0, -1):
    t = V[N - 1] - V[i - 1] + clo[i - 1] - 2 * C + 2 * table[i][0]
    ans = max(t, ans)
print(ans)

N, C = list(map(int, input().split()))
X = []
V = []
for i in range(N):
    x, v = list(map(int, input().split()))
    X.append(x)
    V.append(v)
# 時計回りで見る
R = [0] * (N + 1)  # 0番目は１に格納する
Rans = [0] * (N + 1)
for i in range(N):
    R[i + 1] = R[i] + V[i]
    Rans[i + 1] = max(Rans[i], R[i + 1] - X[i])
L = [0] * (N + 1)
Lans = [0] * (N + 1)
for i in range(N, 0, -1):
    L[i - 1] = L[i] + V[i - 1]  # N-1にN-1を格納
    Lans[i - 1] = max(Lans[i], L[i - 1] - C + X[i - 1])
ans_1 = max(Rans[N], Lans[0])
ans_2 = max(Lans[i + 1] + R[i + 1] - 2 * X[i] for i in range(N))
ans_3 = max(Rans[i - 1] + L[i - 1] - 2 * C + 2 * X[i - 1] for i in range(N, 0, -1))
print((max(ans_1, ans_2, ans_3)))

-import sys - -table = [] +X = [] +V = [] -    table.append([x, v]) -ans = 0 -if N == 1: -    ans = max(0, table[0][1] - table[0][0], table[0][1] + table[0][0] - C) -    print(ans) -    sys.exit() -V = [0] * N -clo = [0] * N -V[0] = table[0][1] -clo[0] = V[0] - table[0][0] -for i in range(1, N): -    V[i] = V[i - 1] + table[i][1] -    clo[i] = max(clo[i - 1], V[i] - table[i][0]) -    ans = max(clo[i], ans) -rclo = [0] * N -rclo[N - 1] = V[N - 1] - V[N - 2] - C + table[N - 1][0] -for i in range(N - 1, 1, -1): -    rclo[i - 1] = max(rclo[i], V[N - 1] - V[i - 2] - C + table[i - 1][0]) -    ans = max(clo[i], ans) -rclo[0] = max(rclo[1], V[N - 1] - C + table[0][0]) -ans = max(rclo[0], ans) -for i in range(N - 1): -    t = V[i] + rclo[i + 1] - 2 * table[i][0] -    ans = max(t, ans) -for i in range(N - 1, 0, -1): -    t = V[N - 1] - V[i - 1] + clo[i - 1] - 2 * C + 2 * table[i][0] -    ans = max(t, ans) -print(ans) +    X.append(x) +    V.append(v) +# 時計回りで見る +R = [0] * (N + 1)  # 0番目は１に格納する +Rans = [0] * (N + 1) +for i in range(N): +    R[i + 1] = R[i] + V[i] +    Rans[i + 1] = max(Rans[i], R[i + 1] - X[i]) +L = [0] * (N + 1) +Lans = [0] * (N + 1) +for i in range(N, 0, -1): +    L[i - 1] = L[i] + V[i - 1]  # N-1にN-1を格納 +    Lans[i - 1] = max(Lans[i], L[i - 1] - C + X[i - 1]) +ans_1 = max(Rans[N], Lans[0]) +ans_2 = max(Lans[i + 1] + R[i + 1] - 2 * X[i] for i in range(N)) +ans_3 = max(Rans[i - 1] + L[i - 1] - 2 * C + 2 * X[i - 1] for i in range(N, 0, -1)) +print((max(ans_1, ans_2, ans_3)))