N,C=map(int,input().split())
x=[0 for i in range(N+1)]
v=[0 for i in range(N+1)]
for i in range(N):
    x[i+1],v[i+1]=map(int,input().split())
r=N
l=1
#nh is not distance but index
nh=0
S=[0 for i in range(N+1)]
S[0]=0
def dis(a,b):
    return min([abs(b-a)%C,C-abs(b-a)%C])
for k in range(1,N+1):
    L=v[l]-dis(x[l],x[nh])
    R=v[r]-dis(x[r],x[nh])
    #equal no baai bug kanousei ari
    if R > L:
        nh=r
        S[k]=S[k-1]+R
        r-=1
    else:
        nh=l
        S[k]=S[k-1]+L
        l+=1
T=[0 for i in range(N+1)]
T[0]=0
l=1
r=N
nh=0
for k in range(1,N+1):
    L=-dis(x[l],x[nh])
    R=-dis(x[r],x[nh])
    #equal no baai bug kanousei ari
    if R > L:
        nh=r
        T[k]=T[k-1]+R+v[r]
        r-=1
    else:
        nh=l
        T[k]=T[k-1]+L+v[l]
        l+=1
print(max([max(S),max(T)]))
