#include <bits/stdc++.h>
#define GET_MACRO(_1,_2,_3,_4,_5,_6,NAME,...) NAME
#define pr(...) GET_MACRO(__VA_ARGS__,pr6,pr5,pr4,pr3,pr2,pr1)(__VA_ARGS__)
#define Pr(a) (#a)<<"="<<(a)<<" "
#define pr1(a) cerr<<Pr(a)<<endl;
#define pr2(a,b) cerr<<Pr(a)<<Pr(b)<<endl;
#define pr3(a,b,c) cerr<<Pr(a)<<Pr(b)<<Pr(c)<<endl;
#define pr4(a,b,c,d) cerr<<Pr(a)<<Pr(b)<<Pr(c)<<Pr(d)<<endl;
#define pr5(a,b,c,d,e) cerr<<Pr(a)<<Pr(b)<<Pr(c)<<Pr(d)<<Pr(e)<<endl;
#define pr6(a,b,c,d,e,f) cerr<<Pr(a)<<Pr(b)<<Pr(c)<<Pr(d)<<Pr(e)<<Pr(f)<<endl;
#define int long long
#define double long double
using namespace std;
const int N = 100010;
const int INF = 1LL<<55;
const int mod = (1e9)+7;
const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);
typedef pair<int,int> P;
typedef long long ll;
template<class T> T Max(T &a,T b){return a=max(a,b);}
template<class T> T Min(T &a,T b){return a=min(a,b);}
ostream& operator<<(ostream& o,P p){return o<<"("<<p.first<<","<<p.second<<")";}
istream& operator>>(istream& i,P &p){return i>>p.first>>p.second;}
ostream& operator<<(ostream& o,vector<auto> &a){int i=0;for(auto t:a)o<<(i++?" ":"")<<t;return o;}
istream& operator>>(istream& i,vector<auto> &a){for(auto &t:a)i>>t;return i;}
void prArr(auto a,string s=" "){int i=0;for(auto t:a)cout<<(i++?s:"")<<t;cout<<endl;}

class RMQ{
public : 
  typedef long long __int128_t;
  ll INF = 1LL<<55;
  struct data{
    bool type; //0 - empty   , 1 - update
    ll value;
  };
  ll n,n_;
  vector<ll> dat;
  vector<data> td;
  int toMax; //0 -> RangeMin 1->RangeMax;
  
  RMQ(){n=-1;}
  RMQ(int n_,int toMax = 0):n_(n_),toMax(toMax){
    n=1;
    while(n<n_)n*=2;
    td.resize(2*n-1,(data){0,0});
    dat.resize(2*n-1,INF);
  }
  
  //[a,b)の値をxに変更　update(a,b,x)
  ll update(int a,int b,ll x,bool flg=true,int k=0,int l=0,int r=-1){
    if(r == -1) {
      r = n;
      //assert(a <= b), assert(a <= n && b <= n), assert(a >= 0 && b >= 0);
      if(toMax) x *= -1;
    }
    
    if(r<=a||b<=l)return flg? dat[k]:INF;
    if(a<=l&&r<=b){
      if(flg==true){
        td[k]=(data){1,x};
        dat[k]=x;
      }
      return dat[k];
    }
    
    if(td[k].type){
      td[k].type=0;
      dat[k*2+1] = dat[k*2+2] = td[k].value;
      td[k*2+1] = td[k*2+2] = (data){1,td[k].value};
    }
    
    ll vl=update(a,b,x,flg,k*2+1,l,(l+r)/2);
    ll vr=update(a,b,x,flg,k*2+2,(l+r)/2,r);
    if(flg==true)dat[k]=min(vl,vr);
    return min(vl,vr);
  }
  
  //[a,b)の最小値を得る　find(a,b);
  ll find(int a,int b){return (toMax? -1:1) * update(a,b,0,false);};
};


class CumulativeSum{
public:
  vector<__int128_t> sum;
  CumulativeSum(){}
  CumulativeSum(vector<int> A){
    int n = A.size();
    sum.resize(n+1,0);
    for(int i=1;i<=n;i++) sum[i] += sum[i-1] + A[i-1];
  }
  __int128_t get(int l,int r){/*assert(l>=0 && r<=(int)sum.size());*/return sum[r] - sum[l];} //[l,r)
};


signed main(){
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int n,C;
  cin>>n>>C;
  vector<P> X(n);
  cin>>X;
  X.insert(X.begin(),P(0,0));
  n++;
  
  vector<int> tmp,tmp2;
  int p = 0;
  for(int i=0;i<3*n;i++)tmp.push_back(X[i].first - p), p = X[i].first;
  for(int i=0;i<3*n;i++) tmp2.push_back(X[i].second);
  CumulativeSum Cost(tmp),Score(tmp2);

  tmp.clear();
  tmp2.clear();
  for(int i=0;i<n;i++) X[i] = P((C-X[i].first)%C, X[i].second);
  sort(X.begin(),X.end());
  p = 0;
  for(int i=0;i<3*n;i++)tmp.push_back(X[i].first - p), p = X[i].first;
  for(int i=0;i<3*n;i++) tmp2.push_back(X[i].second);
  CumulativeSum rCost(tmp),rScore(tmp2);

  auto calc=[&](){

    int ans = -INF;
    {
      RMQ rmq(n+10,1);
      for(int i=0;i<n;i++){
        rmq.update(i,i+1,rScore.get(0,i+1) - rCost.get(0,i+1));
      }
      
      for(int i=0;i<n;i++){
        int cost = Score.get(0,i+1) - Cost.get(0,i+1);
        Max(ans,cost);
        cost = cost - Cost.get(0,i+1) + rmq.find(0,n-i-1);
        Max(ans,cost);
      }
    }
    return ans;
  };
  
  int a = calc();
  swap(Cost,rCost), swap(Score,rScore);
  int b = calc();
  int ans = max(a,b);
  cout<<ans<<endl;
  
  return 0;
}
