class SegmentTree:
    def __init__(self, L, op, ide):
        self.op = op
        self.ide = ide
        self.sz = len(L)
        self.n = 1
        self.s = 1
        for i in range(1000):
            self.n *= 2
            self.s += 1
            if self.n >= self.sz:
                break
        self.node = [self.ide]*(2*self.n-1)

        for i in range(self.sz):
            self.node[i+self.n-1] = L[i]

        for i in range(self.n-2,-1,-1):
            self.node[i] = self.op(self.node[i*2+1],self.node[i*2+2])
    
    def add(self, a, x):
        k = a+self.n-1
        self.node[k] += x
        for i in range(1000):
            k = (k-1)//2
            self.node[k] = self.op(self.node[2*k+1], self.node[2*k+2])
            if k <= 0:
                break
    
    def substitute(self, a, x):
        k = a+self.n-1
        self.node[k] = x
        for i in range(1000):
            k = (k-1)//2
            self.node[k] = self.op(self.node[2*k+1], self.node[2*k+2])
            if k <= 0:
                break

    def get_one(self, a):
        k = a+self.n-1
        return self.node[k]
    
    def get(self, l, r):
        res = self.ide
        n = self.n
        if self.sz <= r or 0 > l:
            print("ERROR: the indice are wrong.")
            return False

        for i in range(self.s):
            count = 2**i-1
            a = (r+1)//n
            b = (l-1)//n
            if a-b == 3:
                res = self.op(self.node[count+b+1],res)
                res = self.op(self.node[count+b+2],res)
                right = a*n
                left = (b+1)*n-1
                break
            if a-b == 2:
                res = self.op(self.node[count+b+1],res)
                right = a*n
                left = (b+1)*n-1
                break
            n = n//2

        #left
        n1 = n//2        
        for j in range(i+1,self.s):
            count = 2**j-1 
            a = (left+1)//n1
            b = (l-1)//n1
            if a-b == 2:
                res = self.op(self.node[count+b+1],res)
                left = (b+1)*n1-1                
            n1 = n1//2

        #right
        n1 = n//2        
        for j in range(i+1,self.s):
            count = 2**j-1 
            a = (r+1)//n1
            b = (right-1)//n1
            if a-b == 2:
                res = self.op(self.node[count+b+1],res)
                right = a*n1                
            n1 = n1//2
        return res

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

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

R1 = [0]
R2 = [0]
r1,r2 = 0,0

prev = 0

for x in X:
    r1+=x[1]-(x[0]-prev)
    r2+=x[1]-2*(x[0]-prev)
    R1.append(r1)
    R2.append(r2)
    prev = x[0]

L1 = [0]
L2 = [0]
l1,l2 = 0,0

X = X[::-1]

prev = C

for x in X:
    l1+=x[1]-((C-x[0])-(C-prev))
    l2+=x[1]-2*((C-x[0])-(C-prev))
    L1.append(l1)
    L2.append(l2)
    prev = x[0]

STR1 = SegmentTree(R1,max,-10**27)
STR2 = SegmentTree(R2,max,-10**27)
STL1 = SegmentTree(L1,max,-10**27)
STL2 = SegmentTree(L2,max,-10**27)
max_score = 0
for i in range(N+1):
    max_score = max(STR1.get(0,N-i)+STL2.get(0,i),STR2.get(0,N-i)+STL1.get(0,i),max_score)

print(max_score)

Measured memory_bytes: 95500