#include <bits/stdc++.h>
using namespace std;
int X[100005];
int V[100005];
long long right_bal[100005];
long long left_bal[100005];
int main() {
  int N, C;
  scanf("%d%d", &N, &C);
  for (int i = 0; i < (N); ++i) scanf("%d%d", &X[i], &V[i]);
  long long ans = 0;
  priority_queue<pair<long long, int> > right_heap;
  priority_queue<pair<long long, int> > left_heap;
  long long v = 0;
  for (int i = 0; i < (N); ++i) {
    long long x = X[i];
    v += V[i];
    right_bal[i] = v - x;
    ans = max(ans, right_bal[i]);
    right_heap.push(make_pair(right_bal[i], i));
  }
  v = 0;
  for (int i = (N - 1); i >= (0); --i) {
    long long x = (C - X[i]) % C;
    v += V[i];
    left_bal[i] = v - x;
    ans = max(ans, left_bal[i]);
    left_heap.push(make_pair(left_bal[i], i));
  }
  v = 0;
  for (int i = (N - 1); i >= (0); --i) {
    long long x = (C - X[i]) % C;
    v += V[i];
    while (!right_heap.empty() && right_heap.top().second >= i)
      right_heap.pop();
    if (right_heap.empty()) break;
    long long total_move = 2 * x + X[right_heap.top().second];
    long long total_bal = right_heap.top().first + v - total_move;
    ans = max(ans, total_bal);
  }
  v = 0;
  for (int i = (N - 1); i >= (0); --i) {
    long long x = X[i];
    v += V[i];
    while (!left_heap.empty() && left_heap.top().second <= i) left_heap.pop();
    if (left_heap.empty()) break;
    long long total_move = 2 * x + (C - X[left_heap.top().second]) % C;
    long long total_bal = left_heap.top().first + v - total_move;
    ans = max(ans, total_bal);
  }
  cout << ans << endl;
}
