#include <bits/stdc++.h>
using namespace std;
int tree[880000], flag[880000], n, m;
struct node {
  int x, y;
} a[880000];
inline bool cmp(node x, node y) {
  return (x.x < y.x or (x.x == y.x and x.y < y.y)) ? true : false;
}
inline void update(int p) { tree[p] = max(tree[p << 1], tree[p << 1 | 1]); }
inline void pushdown(int p) {
  if (flag[p] == 0) return;
  tree[p << 1] += flag[p];
  flag[p << 1] += flag[p];
  tree[p << 1 | 1] += flag[p];
  flag[p << 1 | 1] += flag[p];
  flag[p] = 0;
}
void build(int p, int l, int r) {
  if (l == r) {
    tree[p] = l;
    flag[p] = 0;
    return;
  }
  int mid = (l + r) / 2;
  build(p << 1, l, mid);
  build(p << 1 | 1, mid + 1, r);
  update(p);
}
void change(int p, int l, int r, int ql, int qr, int x) {
  if (ql > qr) return;
  if (ql <= l and r <= qr) {
    tree[p] += x;
    flag[p] += x;
    return;
  }
  pushdown(p);
  int mid = (l + r) / 2;
  if (ql <= mid) change(p << 1, l, mid, ql, qr, x);
  if (mid < qr) change(p << 1 | 1, mid + 1, r, ql, qr, x);
  update(p);
}
int query(int p, int l, int r, int ql, int qr) {
  if (ql > qr) return 0;
  if (ql <= l and r <= qr) return tree[p];
  pushdown(p);
  int mid = (l + r) / 2;
  int ans = 0;
  if (ql <= mid) ans = max(ans, query(p << 1, l, mid, ql, qr));
  if (mid < qr) ans = max(ans, query(p << 1 | 1, mid + 1, r, ql, qr));
  return ans;
}
void print(int p, int l, int r) {
  if (l == r) {
    printf("%d ", tree[p]);
    return;
  }
  int mid = (l + r) / 2;
  pushdown(p);
  print(p << 1, l, mid);
  print(p << 1 | 1, mid + 1, r);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) scanf("%d%d", &a[i].x, &a[i].y);
  sort(a + 1, a + n + 1, cmp);
  build(1, 1, m);
  int ans = n - m;
  for (int i = 1; i <= n; ++i) {
    if (max(a[i].x + 1, 1) < a[i].y)
      change(1, 1, m, max(a[i].x + 1, 1), m, 1), change(1, 1, m, a[i].y, m, -1);
    ans = max(ans, query(1, 1, m, max(a[i].x + 1, 1), m) - max(a[i].x, 0) - m);
  }
  printf("%d\n", ans);
  return 0;
}
