#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

typedef long long int LL;
typedef pair<LL, LL> PLL;


int main(int argc, char *argv[]) {
  LL N, C;
  cin >> N >> C;
  N+=2;
  vector<PLL> sushi(N);
  sushi[0].first = sushi[0].second = 0;
  sushi[1].first = C;
  sushi[1].second = 0;
  for (int i = 2; i < N; ++i) {
    cin >> sushi[i].first >> sushi[i].second;
  }
  sort(sushi.begin(), sushi.end());

  vector<LL> acum_cw(N);
  vector<LL> best_cw(N);
  LL acum = 0;
  LL best = 0;
  for (int i = 0; i < N; ++i) {
    acum += sushi[i].second;
    acum_cw[i] = acum;

    LL benefit = acum_cw[i] - 2 * sushi[i].first;
    if (i == 0) {
      best = benefit;
      best_cw[i] = 0;
    } else if (benefit > best) {
      best = benefit;
      best_cw[i] = i;
    } else {
      best_cw[i] = best_cw[i - 1];
    }
  }

  vector<LL> acum_ccw(N);
  vector<LL> best_ccw(N);
  acum = 0;
  best = 0;
  for (int i = N - 1; i >= 0; i--) {
    acum += sushi[i].second;
    acum_ccw[i] = acum;

    LL benefit = acum_ccw[i] - 2 * (C - sushi[i].first);
    if (i == N - 1) {
      best = benefit;
      best_ccw[i] = i;
    } else if (benefit > best) {
      best = benefit;
      best_ccw[i] = i;
    } else {
      best_ccw[i] = best_ccw[i + 1];
    }
  }

  //cout << "cw:"; for (int i = 0; i < N; ++i) { cout << " " << acum_cw[i]; } cout << endl;
  //cout << "ccw:"; for (int i = 0; i < N; ++i) { cout << " " << best_cw[i]; } cout << endl;
  //cout << "ccw:"; for (int i = 0; i < N; ++i) { cout << " " << acum_ccw[i]; } cout << endl;
  //cout << "ccw:"; for (int i = 0; i < N; ++i) { cout << " " << best_ccw[i]; } cout << endl;

  best = 0;
  for (int i = 0; i < N-1; ++i) {
    LL walk_cw = sushi[i].first;
    LL benefit_cw = acum_cw[i];
    LL benefit_one_dir = benefit_cw - walk_cw;
    best = max(best, benefit_one_dir);

    
    LL pos_ccw = best_ccw[i + 1];
    LL walk_ccw = C - sushi[pos_ccw].first;
    LL benefit_ccw = acum_ccw[pos_ccw];
    LL benefit_two_dir = benefit_one_dir + benefit_ccw - 2*walk_ccw;
    best = max(best, benefit_two_dir);
    //cout << best << " " << i << " (" << benefit_cw << ") " << pos_ccw << " (" << benefit_ccw << endl;
    //cout << " " << benefit_ccw << " " << walk_ccw << endl;
    //cout << i << " " << best << " " << benefit_one_dir << " " << benefit_two_dir << endl;

    
    /*
    for (int j = N - 1; j > i; j--) {
      LL walk_ccw = C - sushi[j].first;
      LL benefit_ccw = acum_ccw[j];
      LL benefit = benefit_cw + benefit_ccw - walk_cw - walk_ccw - min(walk_cw, walk_ccw);
      if (best < benefit) {
        cout << benefit << " " << i << " (" << benefit_cw << ") " << j << " (" << benefit_ccw << endl;
      }
      best = max(best, benefit);
    }*/
  }
  cout << endl;
  for (int i = N-1; i > 0; --i) {
    LL walk_ccw = C - sushi[i].first;
    LL benefit_ccw = acum_ccw[i];
    LL benefit_one_dir = benefit_ccw - walk_ccw;
    best = max(best, benefit_one_dir);

    LL pos_cw = best_cw[i - 1];
    LL walk_cw = sushi[pos_cw].first;
    LL benefit_cw = acum_cw[pos_cw];
    LL benefit_two_dir = benefit_one_dir + benefit_cw - 2*walk_cw;
    best = max(best, benefit_two_dir);
    //cout << best << " " << i << " (" << benefit_ccw << ") " << pos_cw << " (" << benefit_cw << endl;
  }



  cout << best << endl;
  return EXIT_SUCCESS;
}

Measured memory_bytes: 4992