#include <bits/stdc++.h>
using namespace std;
int fst[5555555], nxt[5555555], vb[5555555], cap[5555555], ff[5555555], M = 1,
                                                                        N = 0;
void _ad_dl(int a, int b, int c) {
  ++M;
  nxt[M] = fst[a];
  fst[a] = M;
  vb[M] = b;
  cap[M] = c;
}
void ad_dl(int a, int b, int c) {
  _ad_dl(a, b, c);
  _ad_dl(b, a, 0);
}
int S, T, q[5555555], d[5555555];
bool bfs() {
  memset(d, -1, 4 * (N + 1));
  d[T] = 0;
  q[1] = T;
  int h = 1, t = 2;
  while (h != t) {
    int cur = q[h++];
    for (int e = fst[cur]; e; e = nxt[e]) {
      int b = vb[e];
      if (d[b] == -1 && cap[e ^ 1]) {
        d[b] = d[cur] + 1, q[t++] = b;
        if (b == S) return 1;
      }
    }
  }
  return d[S] != -1;
}
int dfs(int x, int f) {
  if (f <= 0) return 0;
  if (x == T) return f;
  int ca = 0;
  for (int& e = fst[x]; e; e = nxt[e]) {
    int b = vb[e];
    if (d[b] + 1 != d[x]) continue;
    int w = dfs(b, (cap[e] < f - ca) ? cap[e] : (f - ca));
    cap[e] -= w;
    cap[e ^ 1] += w;
    ca += w;
    if (ca == f) break;
  }
  if (!ca) d[x] = -1;
  return ca;
}
int dinic() {
  int ans = 0;
  memcpy(ff, fst, 4 * (N + 1));
  while (bfs()) {
    ans += dfs(S, 1e9 + 3);
    memcpy(fst, ff, 4 * (N + 1));
  }
  return ans;
}
int n, m, l[5555555], r[5555555], qz[5555555], ps[5555555], hz[5555555];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", l + i, r + i);
    l[i] = max(l[i], 1);
    r[i] = min(r[i], m);
  }
  S = ++N, T = ++N;
  for (int i = 1; i <= m; i++) ps[i] = ++N, ad_dl(S, ps[i], 1);
  for (int i = 1; i <= m; i++) {
    qz[i] = ++N;
    ad_dl(ps[i], qz[i], 1e9);
    if (i) ad_dl(qz[i - 1], qz[i], 1e9);
  }
  for (int i = m; i >= 1; --i) {
    hz[i] = ++N;
    ad_dl(ps[i], hz[i], 1e9);
    if (i != m) ad_dl(hz[i + 1], hz[i], 1e9);
  }
  for (int i = 1; i <= n; i++) {
    int g = ++N;
    ad_dl(qz[l[i]], g, 1e9);
    ad_dl(hz[r[i]], g, 1e9);
    ad_dl(g, T, 1);
  }
  printf("%d\n", n - dinic());
}
