#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <stack>

using namespace std;

int n;
long long ans, sum[100005];
long long f[100005];
long long c;
struct ss{
    long long x;
    long long v;
    bool operator < (const ss& other) {
        if (x == other.x) return v < other.v;
        return x < other.x;
    }
}s[100005];

struct res{
    long long x, num;
} g[100005];

int main() {
    cin>>n>>c;
    for (int i = 0; i < n; i++) {
        scanf("%lld%lld", &s[i].x, &s[i].v);
    }
    sort(s,s+n);
    for (int i = 0; i < n; i++) {
        if (0==i) sum[i] = s[i].v;
        else sum[i] = sum[i-1] + s[i].v;
    }
    for (int i = 0; i < n; i++) {
        f[i] = sum[i] - s[i].x;
        if (i) {
            if (g[i-1].num<f[i]){
                g[i].x=s[i].x;
                g[i].num=f[i];
            }else {
                g[i]=g[i-1];
            }
        }else g[i].x = s[0].x,g[i].num=s[0].v-s[0].x;
    }
    //for (int i = 0; i < n; i++) printf("%lld %lld\n",g[i].x, g[i].num);
    ans=max(ans,g[n-1].num);
    for (int i = n-1; i > 0; i--) {
        ans = max(ans, sum[n-1]-sum[i-1]+g[i-1].num-g[i-1].x-c+s[i].x);
        //cout << ans << endl;
    }

    for (int i = 0; i < n; i++) s[i].x = c-s[i].x;
    sort(s, s+n);
    for (int i = 0; i < n; i++) {
        if (0==i) sum[i] = s[i].v;
        else sum[i] = sum[i-1] + s[i].v;
    }
    for (int i = 0; i < n; i++) f[i] = g[i].x=g[i].num = 0;

    for (int i = 0; i < n; i++) {
        f[i] = sum[i] - s[i].x;
        if (i) {
            if (g[i-1].num<f[i]){
                g[i].x=s[i].x;
                g[i].num=f[i];
            }else {
                g[i]=g[i-1];
            }
        } else g[i].x = s[0].x,g[i].num=s[0].v-s[0].x;
    }
    ans=max(ans,g[n-1].num);
    for (int i = n-1; i > 0; i--) ans = max(ans, sum[n-1]-sum[i-1]+g[i-1].num-g[i-1].x-c+s[i].x);
    cout<<ans;
    return 0;
}