#include <bits/stdc++.h>
using namespace std;
inline void read(long long &x) {
  char ch;
  bool flag = false;
  for (ch = getchar(); !isdigit(ch); ch = getchar())
    if (ch == '-') flag = true;
  for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
    ;
  x = flag ? -x : x;
}
inline void write(long long x) {
  static const long long maxlen = 100;
  static char s[maxlen];
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (!x) {
    putchar('0');
    return;
  }
  long long len = 0;
  for (; x; x /= 10) s[len++] = x % 10 + '0';
  for (long long i = len - 1; i >= 0; --i) putchar(s[i]);
}
inline void read(int &x) {
  char ch;
  bool flag = false;
  for (ch = getchar(); !isdigit(ch); ch = getchar())
    if (ch == '-') flag = true;
  for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
    ;
  x = flag ? -x : x;
}
const long long MAXN = 1001000;
int n, m;
struct kt {
  int l, r;
} a[MAXN];
bool cmp_l(kt A, kt B) { return A.l < B.l; }
int main() {
  read(n);
  read(m);
  int cnt = 0;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int x, y;
    read(x);
    read(y);
    if (x == y - 1) {
      ans++;
      continue;
    }
    x += m;
    swap(x, y);
    ++cnt;
    a[cnt].l = x;
    a[cnt].r = y;
  }
  sort(a + 1, a + cnt + 1, cmp_l);
  int pre = 0;
  for (int i = 1; i <= cnt; i++)
    if (pre < a[i].r) {
      pre = max(pre + 1, a[i].l);
      ans++;
    }
  ans = min(ans, m);
  cout << n - ans << endl;
  return 0;
}
