#include <bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
#define int long long
using namespace std;
typedef pair<int,int>P;
#define fi first
#define se second
P dp[100009][2];
int n,c,w[100009],v[100009],ans=0,p;
int A[100009],B[100009],C[100009],D[100009];
int W[1000009],w2[1000009];
main(){
  cin>>n>>c;
  r(i,n)cin>>w[i]>>v[i],p+=v[i];
 
  r(i,n)w2[i]=W[i]=w[i];
 
  r(i,n)D[i]=C[i]=A[i]=v[i];
  r(i,n-1)A[i+1]+=A[i];
 
  r(i,n)B[i]=v[i];
  for(int i=n-1;i>0;i--)B[i-1]+=B[i];
  for(int i=n-1;i>0;i--)D[i-1]+=D[i];
  for(int i=n-1;i>=0;i--){
    if(i!=n-1)B[i]-=abs(c-w[i]);
    else B[i]-=abs(c-w[i]);
  }
  for(int i=n-1;i>0;i--){
    if(B[i-1]<=B[i]){
      B[i-1]=max(B[i-1],B[i]);
      W[i-1]=W[i];
    }
  }
 
  r(i,n-1)C[i+1]+=C[i];
 
  for(int i=0;i<n;i++){
    if(i)C[i]-=w[i];
    else C[i]-=w[i];
  }
 
  for(int i=0;i<n-1;i++){
    if(C[i+1]<=C[i]){
      C[i+1]=max(C[i+1],C[i]);
      w2[i+1]=w2[i];
    }
  }
 
 
  for(int i=0;i<n;i++){
    int sum=0;
    sum=A[i]-w[i];
    //cout<<sum<<' ';
    ans=max(ans,sum);
    if(i!=n-1)sum+=B[i+1];
    if(i!=n-1)sum-=min(abs(W[i+1]-w[i]),min(abs(c-W[i+1]),w[i]));
    //cout<<B[i+1]<<endl;
    ans=max(ans,sum);
  }
 
  for(int i=n-1;i>=0;i--){
    int sum=0;
    sum=D[i]-abs(c-w[i]);
    ans=max(ans,sum);
    //cout<<sum<<endl;
    if(i)sum+=C[i-1];
    if(i)sum-=min(abs(W[i-1]-w2[i]),min(abs(c-W[i-1]),w2[i]));
    ans=max(ans,sum);
    //cout<<sum<<endl;
  }
  ans=max(ans,p-min(w[0],c-w[n-1]));
  cout<<ans<<endl;
}