#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <complex>
using namespace std;

typedef long long ll;
typedef uint64_t ull;
typedef pair<int, int> P;
typedef pair<int, double> Pid;
typedef pair<double, int> Pdi;
typedef pair<ll, int> Pl;
typedef pair<ll, ll> Pll;
typedef pair<int, pair<int, int>> PP;
typedef pair<P, int> PPi;
constexpr double PI = 3.1415926535897932;   // acos(-1)
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define chadd(x, y) x = (x + y) % mod

template<typename Monoid>
struct SegmentTree{
    using F = function<Monoid(Monoid, Monoid)>;

    int sz;
    vector<Monoid> seg;

    const F f;      // モノイドに対して二項演算を行う関数オブジェクト
    const Monoid M1;

    SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1){
        sz = 1;
        while(sz < n)   sz <<= 1;
        seg.assign(2 * sz, M1);
    }

    void set(int k, const Monoid &x){
        seg[k + sz] = x;
    }

    void build(){
        for(int k = sz - 1; k > 0; --k){
            seg[k] = f(seg[k << 1], seg[k << 1 | 1]);
        }
    }

    void update(int k, const Monoid &x){
        k += sz;
        seg[k] = x;
        while(k >>= 1){
            seg[k] = f(seg[k << 1], seg[k << 1 | 1]);
        }
    }

    Monoid query(int a, int b){
        Monoid L = M1, R = M1;
        for(a += sz, b += sz; a < b; a >>= 1, b >>= 1){
            if(a & 1)   L = f(L, seg[a++]);
            if(b & 1)   R = f(seg[--b], R);
        }
        return f(L, R);
    }

    Monoid operator[](const int &k) const{
        return seg[k + sz];
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    ll c;
    cin >> n >> c;
    vector<ll> x(n), v(n), rev(n);
    for(int i = 0; i < n; ++i){
        cin >> x[i] >> v[i];
        rev[i] = c - x[i];
    }
    
    vector<SegmentTree<ll>> clockwise(2, SegmentTree<ll>(n + 1, [](ll a, ll b){return max(a, b);}, 0ll));
    ll val = 0;
    for(int i = 0; i < n; ++i){
        val += v[i];
        clockwise[0].update(i + 1, val - x[i]);
        clockwise[1].update(i + 1, val - x[i] * 2);
    }

    vector<SegmentTree<ll>> counter_clockwise(2, SegmentTree<ll>(n + 1, [](ll a, ll b){return max(a, b);}, 0ll));
    val = 0;
    for(int i = n - 1; i >= 0; --i){
        val += v[i];
        counter_clockwise[0].update(i, val - rev[i]);
        counter_clockwise[1].update(i, val - rev[i] * 2);
    }

    // for(int i = 0; i <= n; ++i) cerr << clockwise[0][i] << " ";
    // cerr << "\n";
    // for(int i = 0; i <= n; ++i) cerr << clockwise[1][i] << " ";
    // cerr << "\n";
    // for(int i = 0; i <= n; ++i) cerr << counter_clockwise[0][i] << " ";
    // cerr << "\n";
    // for(int i = 0; i <= n; ++i) cerr << counter_clockwise[1][i] << " ";
    // cerr << "\n";

    ll ans = 0;
    for(int i = 0; i <= n; ++i){
        ll foo = clockwise[0].query(0, i + 1) + counter_clockwise[1].query(i, n + 1);
        chmax(ans, foo);
        foo = counter_clockwise[0].query(i, n + 1) + clockwise[1].query(0, i + 1);
        chmax(ans, foo);
    }

    cout << ans << endl;
}

Measured memory_bytes: 12924