#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e12;
const long long MOD = 1e9 + 7;
long long sq(long long num) { return num * num; }
long long mod_pow(long long x, long long n) {
  if (n == 0) return 1;
  if (n == 1) return x % MOD;
  long long res = sq(mod_pow(x, n / 2));
  res %= MOD;
  if (n % 2 == 1) {
    res *= x;
    res %= MOD;
  }
  return res;
}
long long mod_add(long long a, long long b) { return (a + b) % MOD; }
long long mod_sub(long long a, long long b) { return (a - b + MOD) % MOD; }
long long mod_mul(long long a, long long b) { return a * b % MOD; }
class BIT {
 public:
  vector<long long> bit;
  int n;
  BIT() {}
  BIT(int n) : n(n) { bit.assign(n + 1, 0); }
  long long sum(long long i) {
    long long s = 0;
    while (i > 0) {
      s += bit[i];
      i -= i & -i;
    }
    return s;
  }
  long long sum(long long i, long long j) { return sum(j) - sum(i); }
  void add(long long i, long long x) {
    while (i <= n) {
      bit[i] += x;
      i += i & -i;
    }
  }
  long long maximum(long long i) {
    long long s = 0;
    while (i > 0) {
      s = max(s, bit[i]);
      i -= i & -i;
    }
    return s;
  }
  long long minimum(long long i) {
    long long s = 0;
    while (i > 0) {
      s = min(s, bit[i]);
      i -= i & -i;
    }
    return s;
  }
  void update(long long i, long long x) {
    while (i <= n) {
      bit[i] = max(bit[i], x);
      i += i & -i;
    }
  }
};
long long n, c;
long long x[100100], v[100100];
int main() {
  cin >> n >> c;
  BIT b1(n), b2(n), b3(n);
  for (int i = 0; i < (n); i++) {
    cin >> x[i] >> v[i];
    b3.add(i + 1, v[i]);
    if (x[i] <= c / 2) b1.add(i + 1, v[i]);
  }
  long long cnt = 1;
  for (int i = n - 1; i >= 0; i--) {
    if (x[i] > c / 2) b2.add(cnt, v[i]);
    cnt++;
  }
  long long a = 0;
  for (int i = 0; i < (n); i++) {
    if (a < b3.sum(i + 1) - x[i]) {
      a = max(a, b3.sum(i + 1) - x[i]);
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    if (a < b3.sum(n, i) - (c - x[i])) {
      a = max(a, b3.sum(n, i) - (c - x[i]));
    }
  }
  long long ans[2][2] = {};
  for (int i = 0; i < (n); i++) {
    if (x[i] > c / 2) break;
    if (ans[0][0] < b1.sum(i + 1) - x[i]) {
      ans[0][0] = b1.sum(i + 1) - x[i];
    }
    if (ans[0][1] < b1.sum(i + 1) - 2 * x[i]) {
      ans[0][1] = b1.sum(i + 1) - 2 * x[i];
    }
  }
  cnt = 1;
  for (int i = n - 1; i >= 0; i--) {
    if (x[i] <= c / 2) break;
    if (ans[1][0] < b2.sum(cnt) - (c - x[i])) {
      ans[1][0] = b2.sum(cnt) - (c - x[i]);
    }
    if (ans[1][1] < b2.sum(cnt) - 2 * (c - x[i])) {
      ans[1][1] = b2.sum(cnt) - 2 * (c - x[i]);
    }
    cnt++;
  }
  long long o = max(ans[0][0], ans[1][0]);
  o = max(ans[0][1] + ans[1][0], o);
  o = max(ans[1][1] + ans[0][0], o);
  cout << max(a, o) << endl;
}
