#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const long long LINF = 2e16;
const int INF = 1e9;
const int magic = 348;
const double eps = 1e-10;
const double pi = 3.14159265;
inline int getint() {
  char ch;
  int res;
  bool f;
  while (!isdigit(ch = getchar()) && ch != '-') {
  }
  if (ch == '-')
    f = false, res = 0;
  else
    f = true, res = ch - '0';
  while (isdigit(ch = getchar())) res = res * 10 + ch - '0';
  return f ? res : -res;
}
int n, m;
pair<int, int> a[200048];
namespace SegmentTree {
struct node {
  int left, right, sz;
  int minval;
} tree[1000048];
inline void build(int cur, int left, int right) {
  tree[cur].left = left;
  tree[cur].right = right;
  tree[cur].sz = 0;
  tree[cur].minval = INF;
  if (left != right) {
    int mid = (left + right) >> 1;
    build(cur << 1, left, mid);
    build(cur << 1 | 1, mid + 1, right);
  }
}
inline int pushup(int val1, int sz, int val2) { return min(val1, val2 - sz); }
inline void Insert(int cur, int pos) {
  if (tree[cur].left == tree[cur].right) {
    tree[cur].sz++;
    tree[cur].minval = pos - tree[cur].sz;
    return;
  }
  int mid = (tree[cur].left + tree[cur].right) >> 1;
  if (pos <= mid)
    Insert(cur << 1, pos);
  else
    Insert(cur << 1 | 1, pos);
  tree[cur].minval = pushup(tree[cur << 1].minval, tree[cur << 1].sz,
                            tree[cur << 1 | 1].minval);
}
inline int query_rank(int cur, int pos) {
  if (tree[cur].left == tree[cur].right) return tree[cur].sz;
  int mid = (tree[cur].left + tree[cur].right) >> 1;
  if (pos <= mid)
    return query_rank(cur << 1, pos);
  else
    return query_rank(cur << 1 | 1, pos) + tree[cur << 1].sz;
}
inline pair<int, int> query_min(int cur, int left, int right) {
  if (left > right) return make_pair(INF, 0);
  if (left <= tree[cur].left && tree[cur].right <= right)
    return make_pair(tree[cur].minval, tree[cur].sz);
  pair<int, int> res1 = make_pair(INF, 0), res2 = make_pair(INF, 0);
  int mid = (tree[cur].left + tree[cur].right) >> 1;
  if (left <= mid) res1 = query_min(cur << 1, left, right);
  if (mid + 1 <= right) res2 = query_min(cur << 1 | 1, left, right);
  pair<int, int> res;
  res.first = pushup(res1.first, res1.second, res2.first);
  res.second = res1.second + res2.second;
  return res;
}
}  // namespace SegmentTree
int main() {
  int i;
  n = getint();
  m = getint();
  for (i = 1; i <= n; i++) a[i].first = getint(), a[i].second = getint();
  sort(a + 1, a + n + 1);
  SegmentTree::build(1, 0, m);
  int ans = 0, curans;
  for (i = 1; i <= n; i++) {
    SegmentTree::Insert(1, m + 1 - a[i].second);
    if (a[i].first + 1 >= a[i].second) ans = max(ans, i - m);
    int ck = SegmentTree::query_rank(1, m + 1 - a[i].second);
    curans = a[i].first + (m + 1 - a[i].second) - ck;
    pair<int, int> res = SegmentTree::query_min(1, m + 1 - a[i].second + 1,
                                                m + 1 - a[i].first - 2);
    curans = min(curans, a[i].first + res.first - ck);
    ans = max(ans, -curans);
  }
  printf("%d\n", ans);
  return 0;
}
