#include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T>
inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
int main() {
  int N;
  long long C;
  cin >> N >> C;
  vector<long long> x(N);
  vector<long long> v(N);
  for (int i = 0; i < (int)(N); i++) {
    cin >> x[i] >> v[i];
  }
  long long ans = 0;
  long long total_calorie = 0;
  for (int i = 0; i < (int)(N); i++) {
    total_calorie += v[i];
    ans = max(ans, total_calorie - x[i]);
  }
  total_calorie = 0;
  for (int i = N - 1; i >= 0; i--) {
    total_calorie += v[i];
    ans = max(ans, total_calorie - (C - x[i]));
  }
  vector<pair<long long, long long> > antiClockwise;
  vector<pair<long long, long long> > clockwise;
  for (int i = 0; i < (int)(N); i++) {
    if (x[i] <= C / 2)
      clockwise.push_back(make_pair(x[i], v[i]));
    else
      antiClockwise.push_back(make_pair(C - x[i], v[i]));
  }
  sort((clockwise).begin(), (clockwise).end());
  sort((antiClockwise).begin(), (antiClockwise).end());
  long long maxVal = 0;
  total_calorie = 0;
  for (int i = 0; i < (int)(antiClockwise.size()); i++) {
    total_calorie += antiClockwise[i].second;
    maxVal = max(maxVal, total_calorie - antiClockwise[i].first);
  }
  total_calorie = 0;
  for (int i = 0; i < (int)(clockwise.size()); i++) {
    total_calorie += clockwise[i].second;
    ans = max(ans, maxVal + total_calorie - (2 * clockwise[i].first));
  }
  maxVal = 0;
  total_calorie = 0;
  for (int i = 0; i < (int)(clockwise.size()); i++) {
    total_calorie += clockwise[i].second;
    maxVal = max(maxVal, total_calorie - clockwise[i].first);
  }
  total_calorie = 0;
  for (int i = 0; i < (int)(antiClockwise.size()); i++) {
    total_calorie += antiClockwise[i].second;
    ans = max(ans, maxVal + total_calorie - (2 * antiClockwise[i].first));
  }
  cout << ans << endl;
  return 0;
}
