#include <bits/stdc++.h>
signed long long int answer1();
signed long long int answer2();
int main() {
  signed long long int answer = answer2();
  printf("%llu\n", answer);
}
signed long long int answer1() {
  signed long int n;
  signed long long int c;
  signed long long int x[100000], oa, ob;
  signed long int v[100000];
  signed long long int calorie, init_calorie = 0, max_calorie = 0, answer;
  x[0] = 0;
  v[0] = 0;
  scanf("%li %llu", &n, &c);
  for (signed int i = 1; i <= n; i++) {
    scanf("%llu %li", &x[i], &v[i]);
    if (x[i] == 0) {
      init_calorie += v[i];
      i--;
      n--;
    }
  }
  for (signed long int i1 = 0; i1 <= n; i1++) {
    oa = x[i1];
    for (signed long int i2 = i1 + 1; i2 <= n + 1; i2++) {
      ob = (c - x[i2 % (n + 1)]) % c;
      calorie = 0;
      for (signed long int i3 = 0; i3 <= i1; i3++) {
        calorie += v[i3];
      }
      for (signed long int i4 = i2; i4 <= n + 1; i4++) {
        calorie += v[i4 % (n + 1)];
      }
      if (oa < ob) {
        calorie -= 2 * oa + ob;
      } else {
        calorie -= oa + 2 * ob;
      }
      if (calorie > max_calorie) {
        max_calorie = calorie;
      }
    }
  }
  answer = init_calorie + max_calorie;
  return answer;
}
signed long long int answer2() {
  signed long int n;
  signed long long int c;
  signed long long int x[100000], oa, ob;
  signed long int v[100000];
  signed long long int va[100000], vb[100000];
  signed long long int calorie, init_calorie = 0, max_calorie = 0;
  signed long long int answer;
  x[0] = 0;
  v[0] = 0;
  scanf("%li %llu", &n, &c);
  for (signed int i = 1; i <= n; i++) {
    scanf("%llu %li", &x[i], &v[i]);
    if (x[i] == 0) {
      init_calorie += v[i];
      i--;
      n--;
    }
  }
  va[0] = 0;
  vb[0] = 0;
  for (signed long int i = 1, m; i <= n; i++) {
    va[i] = va[i - 1] + v[i];
    m = (n - (i - 1));
    vb[m] = vb[(m + 1) % (n + 1)] + v[m];
  }
  for (signed long int i1 = 0; i1 <= n; i1++) {
    oa = x[i1];
    for (signed long int i2 = i1 + 1; i2 <= n + 1; i2++) {
      ob = (c - x[i2 % (n + 1)]) % c;
      calorie = 0;
      calorie += va[i1];
      calorie += vb[i2 % (n + 1)];
      if (oa < ob) {
        calorie -= 2 * oa + ob;
      } else {
        calorie -= oa + 2 * ob;
      }
      if (calorie > max_calorie) {
        max_calorie = calorie;
      }
    }
  }
  answer = init_calorie + max_calorie;
  return answer;
}
