#include <bits/stdc++.h>
using namespace std;
const int N = 800005, M = 1500005, mod = 1e9 + 7;
template <class T>
inline void gmin(T &x, const T &y) {
  if (x > y) x = y;
}
template <class T>
inline void gmax(T &x, const T &y) {
  if (x < y) x = y;
}
inline void ch(int &x, int y) { x = (x + y) % mod; }
inline void read(int &x) {
  x = 0;
  char ch = getchar(), rev = 0;
  while (ch > '9' || ch < '0') rev = (ch == '-'), ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  x = rev ? -x : x;
}
inline int exp(int x, int y) {
  int ans = 1;
  while (y) {
    if (y & 1) ans = (long long)ans * x % mod;
    x = (long long)x * x % mod;
    y >>= 1;
  }
  return ans;
}
struct node {
  int to, next, cap;
} p[M << 1];
int n, m, S, T, tot = 1, cnt, head[N], st[N];
int l[N], r[N], c[N];
int q[N];
inline void add(int x, int y, int z) {
  p[++tot] = (node){y, head[x], z};
  head[x] = tot;
  p[++tot] = (node){x, head[y], 0};
  head[y] = tot;
}
inline bool bfs() {
  memset(st, 0, sizeof(st));
  int l = 1, r = 1;
  q[r] = S;
  st[S] = 1;
  while (l <= r) {
    int x = q[l++];
    for (int i = head[x]; i; i = p[i].next) {
      if (!p[i].cap) continue;
      int y = p[i].to;
      if (st[y]) continue;
      q[++r] = y;
      st[y] = st[x] + 1;
    }
  }
  return st[T] > 0;
}
inline int dfs(int x, int f) {
  if (x == T) return f;
  int r = 0;
  for (int i = head[x]; i; i = p[i].next) {
    if (!p[i].cap) continue;
    int y = p[i].to;
    if (st[y] != st[x] + 1) continue;
    int tmp = dfs(y, min(f - r, p[i].cap));
    p[i].cap -= tmp;
    p[i ^ 1].cap += tmp;
    r += tmp;
    if (r == f) break;
  }
  if (!r) st[x] = -2;
  return r;
}
int dinic() {
  int ans = 0, t;
  while (bfs())
    while (t = dfs(S, 0x3f3f3f3f)) ans += t;
  return ans;
}
int main() {
  scanf("%d%d", &n, &m);
  S = 0;
  T = n + m * 2 + 1;
  int cnt = n;
  for (int i = 1; i <= m; i++) c[i] = ++cnt, l[i] = ++cnt, r[i] = ++cnt;
  for (int i = 1; i <= m; i++) {
    add(l[i], c[i], 1), add(r[i], c[i], 1);
    if (i > 1) add(l[i], l[i - 1], 0x3f3f3f3f);
    if (i < m) add(r[i], r[i + 1], 0x3f3f3f3f);
    add(c[i], T, 1);
  }
  for (int i = 1; i <= n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    add(i, l[x], 1);
    add(i, r[y], 1);
    add(S, i, 1);
  }
  printf("%d\n", n - dinic());
  return 0;
}
