#include <bits/stdc++.h>
using namespace std;
__attribute__((constructor)) void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}
pair<long long, long long> a[100001];
long long b[100001], cc[100001];
vector<pair<long long, long long> > v1, v2;
signed main() {
  long long n, c;
  cin >> n >> c;
  for (long long i = (0); i < (n); i++) {
    cin >> a[i].first >> a[i].second;
    if (a[i].first <= c / 2 + 1)
      v1.push_back({a[i].first, a[i].second});
    else
      v2.push_back({a[i].first, a[i].second});
  }
  sort(a, a + n);
  long long cs = 0;
  long long mx = 0;
  long long z = 0;
  for (long long i = (0); i < (n); i++) {
    cs -= a[i].first - z;
    z = a[i].first;
    cs += a[i].second;
    mx = max(mx, cs);
  }
  cs = 0;
  z = 0;
  for (long long i = (n - 1); i >= (0); i--) {
    cs -= (c - a[i].first) - z;
    z = c - a[i].first;
    cs += a[i].second;
    mx = max(mx, cs);
  }
  long long kmx = 0, kmy = 0;
  cs = 0;
  z = 0;
  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());
  for (long long i = (0); i < (v1.size()); i++) {
    cs -= v1[i].first - z;
    z = v1[i].first;
    cs += v1[i].second;
    mx = max(mx, cs);
    b[i + 1] = cs - v1[i].first;
  }
  cs = 0;
  z = 0;
  for (long long i = (v2.size() - 1); i >= (0); i--) {
    cs -= (c - v2[i].first) - z;
    z = c - v2[i].first;
    cs += v2[i].second;
    mx = max(mx, cs);
    cc[n - (v2.size() - 1 - i)] = cs - (c - v2[i].first);
  }
  for (long long i = (1); i < (n + 1); i++) {
    b[i] = max(b[i], b[i - 1]);
  }
  for (long long i = (n - 2); i >= (0); i--) {
    cc[i] = max(cc[i], cc[i + 1]);
  }
  cs = 0;
  z = 0;
  for (long long i = (0); i < (n); i++) {
    cs -= a[i].first - z;
    z = a[i].first;
    cs += a[i].second;
    mx = max(mx, cs + cc[i]);
  }
  cs = 0;
  z = 0;
  for (long long i = (n - 1); i >= (0); i--) {
    cs -= (c - a[i].first) - z;
    z = c - a[i].first;
    cs += a[i].second;
    mx = max(mx, cs + b[i]);
  }
  cout << mx << endl;
}
