#include <bits/stdc++.h>
using namespace std;
char buf[1 << 20], *p1, *p2;
template <class T>
inline void read(T &n) {
  char ch =
      (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2)
           ? 0
           : *p1++);
  T w = 1, x = 0;
  while (!isdigit(ch)) {
    if (ch == '-') w = -1;
    ch =
        (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2)
             ? 0
             : *p1++);
  }
  while (isdigit(ch)) {
    x = (x << 3) + (x << 1) + (ch ^ 48);
    ch =
        (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2)
             ? 0
             : *p1++);
  }
  n = x * w;
}
const int maxm = 200005;
const int maxn = 800005;
int n, m, tot, st, ed;
int last[maxn], Next[maxn << 3], to[maxn << 3], w[maxn << 3], cnt = 1;
inline void addedge(int x, int y, int z) {
  Next[++cnt] = last[x], last[x] = cnt;
  to[cnt] = y, w[cnt] = z;
}
inline void add(int x, int y, int z) { addedge(x, y, z), addedge(y, x, 0); }
queue<int> Q;
int dep[maxn];
bool bfs() {
  memset(dep, 0, sizeof(dep));
  dep[st] = 1, Q.push(st);
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (int i = last[u]; i; i = Next[i]) {
      int v = to[i];
      if (w[i] && !dep[v]) {
        dep[v] = dep[u] + 1;
        Q.push(v);
      }
    }
  }
  return dep[ed];
}
int cur[maxn];
int dfs(int u, int low) {
  if (u == ed) return low;
  int add = 0;
  for (int i = cur[u]; i && add < low; i = Next[i]) {
    int v = to[i];
    cur[u] = i;
    if (w[i] && dep[v] == dep[u] + 1) {
      int tmp = dfs(v, min(w[i], low - add));
      w[i] -= tmp;
      w[i ^ 1] += tmp;
      add += tmp;
    }
  }
  return add;
}
const int inf = 0x3f3f3f3f;
int dinic() {
  int res = 0;
  while (bfs()) {
    memcpy(cur, last, sizeof(last));
    res += dfs(st, inf);
  }
  return res;
}
int pre[maxm], suf[maxm];
int main() {
  read(n), read(m), tot = n + m;
  for (int i = 1; i <= m; i++) {
    pre[i] = ++tot;
    add(pre[i], i + n, 1);
    if (i > 1) add(pre[i], pre[i - 1], inf);
  }
  for (int i = m; i >= 1; i--) {
    suf[i] = ++tot;
    add(suf[i], i + n, 1);
    if (i < m) add(suf[i], suf[i + 1], inf);
  }
  ed = ++tot;
  for (int i = 1; i <= m; i++) add(i + n, ed, 1);
  for (int i = 1, l, r; i <= n; i++) {
    read(l), read(r);
    add(st, i, 1);
    if (l >= 1) add(i, pre[l], 1);
    if (r <= m) add(i, suf[r], 1);
  }
  cout << n - dinic() << endl;
  return 0;
}
