#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr long long MOD = 1000000000 + 7;
constexpr long long INF = std::numeric_limits<long long>::max();
const double PI = acos(-1);
struct Tll {
  LL first, second, third;
};
LL h, w, n, m, k, s, t, q, last, cnt, sum[1000000], ans = 0;
pair<LL, LL> a[1000000];
string str, ss;
bool f;
char c;
int di[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
struct Edge {
  int to, cost;
};
vector<Edge> vec;
void YN(bool f) {
  if (f)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void yn(bool f) {
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
int main() {
  cin >> n >> m;
  sum[0] = 0;
  for (LL i = 0; i < n; i++) {
    cin >> a[i].first >> a[i].second;
  }
  a[n] = pair<LL, LL>(0, 0);
  a[n + 1] = pair<LL, LL>(m, 0);
  n += 2;
  sort(a, a + n);
  for (LL i = 0; i < n; i++) {
    sum[i + 1] = sum[i] + a[i].second;
    ans = max(sum[i + 1] - a[i].first, ans);
  }
  for (LL i = 0; i < n; i++) {
    ans = max(sum[n] - sum[n - i - 1] - (m - a[n - i - 1].first), ans);
  }
  LL rm[2] = {}, lm[2] = {};
  for (LL i = 0; i < n; i++) {
    if (a[i].first * 2 <= m) {
      last = i;
      rm[0] = max(sum[i + 1] - a[i].first * 2, rm[0]);
      rm[1] = max(sum[i + 1] - a[i].first, rm[1]);
    }
    if (a[n - i - 1].first * 2 >= m) {
      cnt = n - i - 1;
      lm[0] = max(sum[n] - sum[n - i - 1] - (m - a[n - i - 1].first), lm[0]);
      lm[1] =
          max(sum[n] - sum[n - i - 1] - (m - a[n - i - 1].first) * 2, lm[1]);
    }
  }
  for (LL i = 0; i < 2; i++) {
    if (last == cnt) {
      if (i == 0)
        if (rm[i] + lm[i] == sum[n] + a[last].second - a[last].first - m) {
          ans = max(rm[i] + lm[i] - a[last].second -
                        max((a[last].first - a[last - 1].first) * 2,
                            (a[last + 1].first - a[last].first)),
                    ans);
          continue;
        }
      if (i == 1)
        if (rm[i] + lm[i] ==
            sum[n] + a[last].second - (m - a[last].first) - m) {
          ans = max(rm[i] + lm[i] - a[last].second -
                        max((a[last].first - a[last - 1].first),
                            (a[last + 1].first - a[last].first) * 2),
                    ans);
          continue;
        }
    }
    ans = max(rm[i] + lm[i], ans);
  }
  cout << ans << endl;
  return 0;
}
