#include <bits/stdc++.h>

using namespace std;

#define int long long

int n, c;
int x[100010], v[100010];

main()
{
    ios_base::sync_with_stdio(false);
    cin >> n >> c;
    for(int i = 0; i < n; i++) cin >> x[i] >> v[i];
    int cur = 0;
    int an = 0;
    int pos = 0;
    for(int i = 0; i < n; i++){
        cur -= (x[i] - pos);
        cur += v[i];
        an = max(an, cur);
        pos = x[i];
    }
    pos = 0;
    cur = 0;
    for(int i = n - 1; i >= 0; i--){
        int X = c - x[i];
        cur -= (X - pos);
        cur += v[i];
        an = max(an, cur);
        pos = X;
    }
    deque<pair<int, int>> a;
    a.push_back({0, 0});
    for(int i = 0; i < n; i++){
        if(x[i] * 2 < c) a.push_back({x[i], v[i]});
    }
    for(int i = n - 1; i >= 0; i--){
        int X = c - x[i];
        if(X * 2 <= c) a.push_front({X, v[i]});
    }
    vector<int> a1;
    bool ok = false;
    for(int i = 0; i < n + 1; i++){
        auto u = a[i];
        int x = u.first, v = u.second;
        if(x == 0){
            ok = true;
        }
        else{
            if(!ok){
                x -= a[i+1].first;
            }
            else{
                x -= a[i-1].first;
            }
        }
        if(!ok){
            a1.push_back(v - 2*x);
        }
        else{
            a1.push_back(v - x);
        }
    }
    int su = 0;
    int lo = 0;
    for(int i = 0; i < n + 1; i++){
        su += a1[i];
        while(su < 0){
            su -= a1[lo];
            if(a1[lo] == 0) break;
            lo++;
        }
        an = max(an, su);
    }
    vector<int> a2;
    ok = false;
    for(int i = 0; i < n + 1; i++){
        auto u = a[i];
        int x = u.first, v = u.second;
        if(x == 0){
            ok = true;
        }
        else{
            if(!ok){
                x -= a[i+1].first;
            }
            else{
                x -= a[i-1].first;
            }
        }
        if(!ok){
            a2.push_back(v - x);
        }
        else{
            a2.push_back(v - 2*x);
        }
    }
    su = 0;
    lo = 0;
    for(int i = 0; i < n + 1; i++){
        su += a2[i];
        while(su < 0){
            if(a2[lo] == 0) break;
            su -= a2[lo];
            lo++;
        }
        an = max(an, su);
    }
    cout << an << endl;
}
