#include <bits/stdc++.h>
using namespace std;

#define NDEBUG
#ifdef DEBUG
#include "cout11.h"
#undef NDEBUG
#endif
#include <cassert>

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> ii;
typedef pair<ll,ll> llll;
typedef pair<double,double> dd;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ii> vii;
typedef vector<vector<ii>> vvii;
typedef vector<ll> vll;

#define sz(a)  int((a).size())
#define pb  push_back
#define FOR(var,from,to) for(int var=(from);var<=(to);++var)
#define rep(var,n)  for(int var=0;var<(n);++var)
#define rep1(var,n)  for(int var=1;var<=(n);++var)
#define repC2(vari,varj,n)  for(int vari=0;vari<(n)-1;++vari)for(int varj=vari+1;varj<(n);++varj)
#define ALL(c)  (c).begin(),(c).end()
#define RALL(c)  (c).rbegin(),(c).rend()
#define tr(i,c)  for(auto i=(c).begin(); i!=(c).end(); ++i)
#define found(s,e)  ((s).find(e)!=(s).end())
#define mset(arr,val)  memset(arr,val,sizeof(arr))
#define mid(x,y) ((x)+((y)-(x))/2)
#define IN(x,a,b) ((a)<=(x)&&(x)<=(b))

class Q {
public:
    vector<ll> z;
    int N, m;
    ll _min;

    Q(vector<ll>& v){
        N = v.size();
        m=1; while(m<N) m*=2;
        _min = *min_element(ALL(v));
        z.assign(m*2, _min);
        rep(i,m) z[m+i] = v[i];
        for(int w=m/2; w>=1; w/=2) {
            int b=w;
            rep(i,w){
                z[b+i] = max(z[(b+i)*2], z[(b+i)*2+1]);
            }
        }
        //cerr << "Q" << z << endl;
    }
    ll maxv(int n){ // num
        if (n == 0) return 0LL;
        ll ans = _min;
        for (int mask=m,ix=1,w=1; mask>=1; mask>>=1,w<<=1) {
            if (n & mask) {
                ans = max(ans, z[ix]);
                ++ix;
            } else {
                ;
            }
            ix *= 2;
        }
        return ans;
    }
};
ll solve(ll N, ll C, vector<llll>& xv) {
    ll ans = 0LL;

    sort(ALL(xv));

    if (xv[0].first == 0) {
        ans += xv[0].second;
        xv.erase(xv.begin(), xv.begin()+1);
        --N;
    }

    // cerr << N << " " << C << xv << endl;

    // L
    vector<llll> ac0(N);
    ac0[0] = xv[0];
    for(int i=1; i<N; ++i) {
        ac0[i] = llll(xv[i].first, ac0[i-1].second + xv[i].second);
    }

    // R
    reverse(ALL(xv));
    rep(i, N) {
        xv[i].first = C - xv[i].first;
    }
    vector<llll> ac1(N);
    ac1[0] = xv[0];
    for(int i=1; i<N; ++i) {
        ac1[i] = llll(xv[i].first, ac1[i-1].second + xv[i].second);
    }

    //cerr << "ac0:" << ac0 << endl;
    //cerr << "ac1:" << ac1 << endl;

    vector<ll> bc0(N), bc1(N);
    rep(i,N) {
        bc0[i] = ac0[i].second - ac0[i].first;
        bc1[i] = ac1[i].second - ac1[i].first;
    }

    //cerr << "bc0:" << bc0 << endl;
    //cerr << "bc1:" << bc1 << endl;

    vector<ll> cc0(N), cc1(N);
    rep(i,N){
        cc0[i] = ac0[i].second - ac0[i].first*2;
        cc1[i] = ac1[i].second - ac1[i].first*2;
    }

    //cerr << "cc0:" << cc0 << endl;
    //cerr << "cc1:" << cc1 << endl;

    // cc0[i] + max(bc1[i]
    ll res = 0LL;

    Q qc0(cc0);
    Q qc1(cc1);

    rep(i, N) {
        ll sc = max(0LL, bc0[i]) + max(0LL, qc1.maxv(N-i-1));
        res = max(res, sc);
        //cerr << i << " " << bc0[i] << ", " << qc1.maxv(N-i-1) << endl;
    }
    rep(i, N) {
        ll sc = max(0LL, bc1[i]) + max(0LL, qc0.maxv(N-i-1));
        res = max(res, sc);
        //cerr << i << " " << bc1[i] << ", " << qc0.maxv(N-i-1) << endl;
    }

    return res;
}

int main() {
    ll N, C; cin >> N >> C;
    vector<llll> xv(N);
    rep(i,N){
        ll xi,vi; cin >> xi >> vi;
        xv[i] = llll(xi,vi);
    }
    cout << solve(N,C,xv) << endl;
    return 0;
}


Measured memory_bytes: 12160