#include <bits/stdc++.h>
const int MAXN = 200010;
const int MAXP = MAXN * 3;
const int INF = std::numeric_limits<int>::max();
const int MAXM = MAXP * 10;
int head[MAXP], nxt[MAXM], to[MAXM], val[MAXM], tot = 1;
int S, T, idx;
void addedge(int b, int e, int v) {
  nxt[++tot] = head[b];
  to[head[b] = tot] = e;
  val[tot] = v;
  nxt[++tot] = head[e];
  to[head[e] = tot] = b;
  val[tot] = 0;
}
int dis[MAXP];
std::queue<int> q;
bool bfs() {
  memset(dis, 0, idx + 1 << 2);
  q.push(S);
  dis[S] = 1;
  while (!q.empty()) {
    int t = q.front();
    q.pop();
    for (int i = head[t]; i; i = nxt[i])
      if (val[i] && !dis[to[i]]) {
        q.push(to[i]);
        dis[to[i]] = dis[t] + 1;
      }
  }
  return dis[T] > 0;
}
int dinic(int u, int minv) {
  if (!minv || u == T) return minv;
  int res = 0, t;
  for (int& i = head[u]; i; i = nxt[i])
    if (val[i] && dis[to[i]] == dis[u] + 1 &&
        (t = dinic(to[i], std::min(minv, val[i])))) {
      val[i] -= t;
      val[i ^ 1] += t;
      res += t;
      minv -= t;
      if (!minv) break;
    }
  if (!res) dis[u] = -1;
  return res;
}
int flow() {
  static int te[MAXP];
  int ans = 0;
  memcpy(te, head, idx + 1 << 2);
  while (bfs()) {
    ans += dinic(S, INF);
    memcpy(head, te, idx + 1 << 2);
  }
  return ans;
}
int n, m;
int pre[MAXN], suc[MAXN], at[MAXN];
int main() {
  std::ios_base::sync_with_stdio(false), std::cin.tie(0);
  std::cin >> n >> m;
  S = 0, T = idx = 1;
  for (int i = 1; i <= m; ++i) {
    pre[i] = ++idx, suc[i] = ++idx, at[i] = ++idx;
    addedge(pre[i], at[i], 1);
    addedge(suc[i], at[i], 1);
    addedge(at[i], T, 1);
  }
  for (int i = 2; i <= m; ++i) addedge(pre[i], pre[i - 1], INF);
  for (int i = 1; i < m; ++i) addedge(suc[i], suc[i + 1], INF);
  for (int i = 1, l, r; i <= n; ++i) {
    std::cin >> l >> r;
    int now = ++idx;
    addedge(S, now, 1);
    if (l) addedge(now, pre[l], 1);
    if (r) addedge(now, suc[r], 1);
  }
  std::cout << n - flow() << std::endl;
  return 0;
}
