#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <cstring>
#include <numeric>
#include <algorithm>
#include <functional>
#include <fstream>
#include <array>
#include <map>
#include <queue>
#include <time.h>
#include <limits.h>
#include <set>
#include <stack>
#include <random>
#include <complex>
#include <unordered_map>
#include <assert.h>
#include <unordered_set>
#define rep(i,s,n) for(int i = (s); (n) > i; i++)
#define REP(i,n) rep(i,0,n)
#define RANGE(x,a,b) (min(a,b) <= (x) && (x) <= max(a,b)) //hei
#define DUPLE(a,b,c,d) (RANGE(a,c,d) || RANGE(b,c,d) || RANGE(c,a,b) || RANGE(d,a,b))
#define INCLU(a,b,c,d) (RANGE(a,c,d) && (b,c,d))
#define PW(x) ((x)*(x))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MODU 1000000007LL
#define bitcheck(a,b)   ((a >> b) & 1)
#define bitset(a,b)      ( a |= (1 << b))
#define bitunset(a,b)    (a &= ~(1 << b))
#define MP(a,b) make_pair((a),(b))
#define Manh(a,b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#define PI 3.141592653589
 
 
#define izryt bool
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template<typename A, size_t N, typename T>
void Fill(A(&array)[N], const T & val) {
	std::fill((T*)array, (T*)(array + N), val);
}
 
//[a, b)
#define Getsum(ar, a,b) (ar[b] - ar[a])
#define INF 10000000000000000LL
#define chmax(a,b) a = max(a,b)
#define chmin(a,b) a = min(a,b)
 
struct Edge {
	int from, to;
	ll w;
	bool operator<(const Edge& rhs) const {
		return MP(w, MP(from, to)) < MP(rhs.w, MP(rhs.from, rhs.to));
	}
 
};
typedef vector<vector<Edge>> Graph;
typedef unsigned long long ull;

ll facmemo[323456] = {};
ll factorial(int n) {//階乗
  if (n > 0) {
    assert(n < 323456);
    if(facmemo [n]!= 0) return facmemo[n];
		return facmemo[n] = (n * factorial(n - 1)) % MODU;
	}
	else {
		return 1;
	}
}
ll mod_pow(ll x, ll n) {
	ll res = 1;
	while (n > 0) {
		if (n & 1) res = (res * x ) %MODU;
		x = (x*x)%MODU;
		n >>= 1;
	}
	return res;
}

ll divmod(ll a, ll b) { // a/b(mod m)
								  // a/b (mod m)
								  // a*b^-1
								  // b^-1 * b = 1
								  // b^(m-1) = 1 
								  // b^(m-2) * b = 1
								  // b^-1 = b^(m-2)

								  // a/b = a * b^(m-2)
 // if(bmemo[b] == -1) bmemo[b] = mod_pow(b, MODU - 2);
	return (a * mod_pow(b, MODU - 2)) % MODU;
}


ll ncr(ll n, ll r) {
  if(n < r || r < -1) return 1;
	return divmod(factorial(n), (factorial(r) * (factorial(n - r))) % MODU);
}
struct UnionFind {
	vector<int> data;
	UnionFind(int size) : data(size, -1) { }
	bool unionSet(int x, int y) { //ｘの入ってる集合と yの入ってる集合を併合
		x = root(x); y = root(y);
		if (x != y) {
			if (data[y] < data[x]) swap(x, y);
			data[x] += data[y]; data[y] = x;
		}
		return x != y;
	}
	bool findSet(int x, int y) { //xとyが同じ集合に入っているかどうかを判定
		return root(x) == root(y);
	}
	int root(int x) {
		return data[x] < 0 ? x : data[x] = root(data[x]);
	}
	bool isroot(int x) {
		return data[x] < 0;
	}
	int size(int x) {
		return -data[root(x)];
	}
};


  int n;
  ll c;
ll solve(vector<pll> cw, vector<pll> cc){

  ll rui[100001] = {}, rum[100001] = {};

  REP(i,n){
    rui[i + 1] = rui[i] + cc[i].second;
    rum[i + 1] = max(rum[i], rui[i + 1] - cc[i].first);
  }
  ll ans = -1, csum = 0, cpos = 0;
  REP(i,n + 1){ //i miman
    ans = max(ans, csum - cpos);
    ans = max(ans, csum - cpos * 2 + rum[n-i]);

    csum += cw[i].second;
    cpos = cw[i].first;
  }

  return ans;
}

int main(){
  cin >> n >> c;
  vector<pll> cw,cc;
  REP(i,n){
    ll x,v;
    cin >> x >> v;
    cw.push_back({x,v});
    cc.push_back({c-x,v});
  }

  sort(ALL(cc));
  sort(ALL(cw));

  ll ans = solve(cw,cc);
  ans = max(ans, solve(cc,cw));
  cout << ans << endl;
}

Measured memory_bytes: 7524