#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, c;
  cin >> n >> c;
  vector<pair<long long, long long>> v;
  vector<pair<long long, long long>> gyaku;
  vector<pair<long long, long long>> tokei;
  vector<pair<long long, long long>> han;
  for (long long(i) = (0); (i) < (n); ++i) {
    long long a, b;
    cin >> a >> b;
    v.push_back(make_pair(a, b));
    gyaku.push_back(make_pair(c - a, b));
    if (a <= c / 2) {
      tokei.push_back(make_pair(a, b));
    } else {
      han.push_back(make_pair(c - a, b));
    }
  }
  sort(v.begin(), v.end());
  sort(gyaku.begin(), gyaku.end());
  sort(tokei.begin(), tokei.end());
  sort(han.begin(), han.end());
  long long ans = 0;
  long long sum = 0;
  for (long long(i) = (0); (i) < (n); ++i) {
    sum += v[i].second;
    ans = max(ans, sum - v[i].first);
  }
  sum = 0;
  for (long long(i) = (0); (i) < (n); ++i) {
    sum += gyaku[i].second;
    ans = max(ans, sum - gyaku[i].first);
  }
  long long kata = 0;
  long long ryou = 0;
  sum = 0;
  for (long long(i) = (0); (i) < (tokei.size()); ++i) {
    sum += tokei[i].second;
    ryou = max(ryou, sum - 2 * tokei[i].first);
  }
  sum = 0;
  for (long long(i) = (0); (i) < (han.size()); ++i) {
    sum += han[i].second;
    kata = max(kata, sum - han[i].first);
  }
  ans = max(ans, kata + ryou);
  kata = 0;
  ryou = 0;
  sum = 0;
  for (long long(i) = (0); (i) < (han.size()); ++i) {
    sum += han[i].second;
    ryou = max(ryou, sum - 2 * han[i].first);
  }
  sum = 0;
  for (long long(i) = (0); (i) < (tokei.size()); ++i) {
    sum += tokei[i].second;
    kata = max(kata, sum - tokei[i].first);
  }
  ans = max(ans, kata + ryou);
  cout << ans << endl;
  return 0;
}
