#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using p = pair<ll, ll>;
ll N, C;
vector<p> sushi;
ll dist(ll i, ll pos) {
  ll x = sushi[i].first;
  return min(abs(x - pos), min(abs(x - (pos + C)), abs(x + C - pos)));
}
ll solve(ll cw, ll ccw) {
  ll sum = 0;
  for (ll i = 0; i <= cw; i++) {
    sum += sushi[i].second;
  }
  sum -= sushi[cw].first;
  if (cw == ccw) {
    return sum;
  }
  for (ll i = N - 1; i >= ccw; i--) {
    sum += sushi[i].second;
  }
  sum -= sushi[cw].first;
  sum -= (C - sushi[ccw].first);
  return sum;
}
ll solve_rev(ll ccw, ll cw) {
  ll sum = 0;
  for (ll i = N - 1; i >= ccw; i--) {
    sum += sushi[i].second;
  }
  sum -= (C - sushi[ccw].first);
  if (cw == ccw) {
    return sum;
  }
  for (ll i = 0; i <= cw; i++) {
    sum += sushi[i].second;
  }
  sum -= (C - sushi[ccw].first);
  sum -= sushi[cw].first;
  return sum;
}
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> C;
  for (ll i = 0; i < N; i++) {
    ll x, v;
    cin >> x >> v;
    sushi.push_back(p(x, v));
  }
  sort(sushi.begin(), sushi.end());
  vector<ll> used(N + 1, false);
  ll current = 0;
  ll ans = 0;
  ll sum = 0;
  for (ll cw = 0; cw < N; cw++) {
    for (ll ccw = N - 1; ccw >= cw; ccw--) {
      ans = max(ans, solve(cw, ccw));
    }
  }
  for (ll ccw = N - 1; ccw >= 0; ccw--) {
    for (ll cw = 0; cw <= ccw; cw++) {
      ans = max(ans, solve_rev(ccw, cw));
    }
  }
  cout << ans << endl;
  return 0;
}
