#include <bits/stdc++.h>
using namespace std;
const int maxn = 2000005;
const int maxm = 10000005;
const int inf = 1e9;
struct edge {
  int v, c, nex;
  edge(int v = 0, int c = 0, int nex = 0) : v(v), c(c), nex(nex) {}
} e[maxm];
int n, m, M, s, t, en, head[maxn], cur[maxn], dist[maxn];
void add(int u, int v, int c) {
  e[en] = edge(v, c, head[u]), head[u] = en++;
  e[en] = edge(u, 0, head[v]), head[v] = en++;
}
bool bfs() {
  static int q[maxn], qN;
  for (int i = (s); i <= (t); ++i) dist[i] = 0;
  q[qN = 1] = s, dist[s] = 1;
  for (int i = (1); i <= (qN); ++i) {
    int u = q[i];
    for (int j = head[u]; ~j; j = e[j].nex)
      if (e[j].c > 0 && !dist[e[j].v]) {
        dist[e[j].v] = dist[u] + 1;
        q[++qN] = e[j].v;
      }
  }
  return dist[t];
}
int dfs(int u, int a) {
  if (u == t || a == 0) return a;
  int res = 0, f;
  for (int &i = cur[u]; ~i; i = e[i].nex)
    if (e[i].c > 0 && dist[e[i].v] == dist[u] + 1 &&
        (f = dfs(e[i].v, min(a, e[i].c))) > 0) {
      res += f, a -= f;
      e[i].c -= f, e[i ^ 1].c += f;
      if (!a) break;
    }
  return res;
}
int dinic() {
  int res = 0;
  while (bfs()) {
    for (int i = (s); i <= (t); ++i) cur[i] = head[i];
    res += dfs(s, inf);
  }
  return res;
}
void get(int l, int r, int id) {
  for (l += M - 1, r += M + 1; l ^ r ^ 1; l >>= 1, r >>= 1) {
    if (~l & 1) add(id, l ^ 1, 1);
    if (r & 1) add(id, r ^ 1, 1);
  }
}
int main() {
  memset(head, -1, sizeof head);
  scanf("%d%d", &n, &m);
  for (M = 1; M < m + 2; M <<= 1)
    ;
  for (int i = (1); i <= (M - 1); ++i) {
    add(i, i << 1, inf);
    add(i, i << 1 | 1, inf);
  }
  s = 0, t = M * 2 + n + 1;
  for (int i = (1); i <= (n); ++i) add(s, M * 2 + i, 1);
  for (int i = (1); i <= (m); ++i) add(M + i, t, 1);
  for (int i = (1); i <= (n); ++i) {
    int l, r;
    scanf("%d%d", &l, &r);
    if (l) get(1, l, 2 * M + i);
    if (r <= m) get(r, m, 2 * M + i);
  }
  printf("%d\n", n - dinic());
  return 0;
}
