#include <bits/stdc++.h>
using namespace std;
int read_p, read_ca, read_f;
inline int read() {
  read_p = 0;
  read_ca = getchar();
  read_f = 1;
  while (read_ca < '0' || read_ca > '9')
    read_f = read_ca == '-' ? -1 : read_f, read_ca = getchar();
  while (read_ca >= '0' && read_ca <= '9')
    read_p = read_p * 10 + read_ca - 48, read_ca = getchar();
  return read_p * read_f;
}
const int INF = 1e9;
struct na {
  int y, z, ne;
} b[3000001];
int n, m, num = 1, no = 0, S, T, mmh = 0, g[3000001], c[3000001], d[3000001],
          l[3000001], ro, ls[3000001], rs[3000001];
inline void in(int x, int y, int z) {
  b[++num].y = y;
  b[num].z = z;
  b[num].ne = l[x];
  l[x] = num;
}
inline void add(int x, int y, int z) {
  in(x, y, z);
  in(y, x, 0);
}
inline int min(int a, int b) { return a < b ? a : b; }
int sap(int x, int f) {
  if (x == T) return f;
  int h = 0, q;
  for (int i = d[x]; i; i = b[i].ne)
    if (b[i].z && g[b[i].y] + 1 == g[x]) {
      q = sap(b[i].y, min(b[i].z, f - h));
      h += q;
      b[i].z -= q;
      b[i ^ 1].z += q;
      if (h == f || g[S] == no) return h;
    }
  if (!(--c[g[x]])) g[S] = no;
  d[x] = l[x];
  c[++g[x]]++;
  return h;
}
int build(int l, int r) {
  int p = ++num, mid = l + r >> 1;
  if (l == r) {
    add(p, T, 1);
  } else {
    add(p, ls[p] = build(l, mid), mid - l + 1);
    add(p, rs[p] = build(mid + 1, r), r - mid);
  }
  return p;
}
void ADD(int p, int l, int r, int L, int R, int v) {
  if (l == L && r == R)
    add(v, p, 1);
  else {
    int mid = l + r >> 1;
    if (R <= mid)
      ADD(ls[p], l, mid, L, R, v);
    else if (L > mid)
      ADD(rs[p], mid + 1, r, L, R, v);
    else
      ADD(ls[p], l, mid, L, mid, v), ADD(rs[p], mid + 1, r, mid + 1, R, v);
  }
}
int main() {
  register int i, j;
  n = read();
  m = read();
  S = 0;
  T = n + 1;
  num = n + 1;
  ro = build(1, m);
  for (i = 1; i <= n; i++) {
    int l = read(), r = read();
    add(S, i, 1);
    if (l) ADD(ro, 1, m, 1, l, i);
    if (r != m + 1) ADD(ro, 1, m, r, m, i);
  }
  no = num;
  for (; g[S] < no; mmh += sap(S, INF))
    ;
  printf("%d\n", n - mmh);
}
