#include <bits/stdc++.h>
using namespace std;
const int MAX = 1 << 18;
const int INF = 0x3f3f3f3f;
int n, m, ans;
pair<int, int> arr[MAX], seg[MAX * 2];
;
int find_max(int a, int b, int first, int second, int node) {
  if (a <= first && second <= b) return seg[node].first + seg[node].second;
  if (b <= first || second <= a) return -INF;
  if (seg[node].second != 0) {
    seg[node].first += seg[node].second;
    seg[node * 2].second += seg[node].second;
    seg[node * 2 + 1].second += seg[node].second;
    seg[node].second = 0;
  }
  return max(find_max(a, b, first, (first + second) / 2, node * 2),
             find_max(a, b, (first + second) / 2, second, node * 2 + 1));
}
int update(int a, int b, int first, int second, int node) {
  if (a <= first && second <= b) {
    seg[node].second++;
    return seg[node].first + seg[node].second;
  }
  if (b <= first || second <= a) return seg[node].first + seg[node].second;
  if (seg[node].second != 0) {
    seg[node].first += seg[node].second;
    seg[node * 2].second += seg[node].second;
    seg[node * 2 + 1].second += seg[node].second;
    seg[node].second = 0;
  }
  return seg[node].first =
             max(update(a, b, first, (first + second) / 2, node * 2),
                 update(a, b, (first + second) / 2, second, node * 2 + 1));
}
int build(int first) {
  if (first >= MAX) return seg[first].first;
  return seg[first].first = max(build(first * 2), build(first * 2 + 1));
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    int t1, t2;
    scanf("%d%d", &t1, &t2);
    arr[i].first = t1, arr[i].second = t2;
  }
  sort(arr, arr + n);
  for (int i = MAX; i < 2 * MAX; i++) seg[i].first = -INF;
  for (int i = 1; i <= m + 1; i++) seg[i + MAX].first = i;
  build(1);
  for (int i = 0; i < n; i++) {
    update(0, arr[i].second + 1, 0, MAX, 1);
    ans = max(ans, find_max(arr[i].first + 1, m + 2, 0, MAX, 1) -
                       (arr[i].first + m + 1));
  }
  printf("%d\n", ans);
  return 0;
}
