#include <bits/stdc++.h>
inline int read() {
  int x = 0;
  char c = getchar();
  for (; c < 48 || c > 57; c = getchar())
    ;
  for (; c > 47 && c < 58; c = getchar()) x = x * 10 + c - 48;
  return x;
}
int n, m, s, t;
int st[1000005], pv[5000005], p1[5000005], pf[5000005], tot = 1;
inline void side1(int u, int v, int f) {
  pv[++tot] = v;
  pf[tot] = f;
  p1[tot] = st[u];
  st[u] = tot;
}
inline void side(int u, int v) {
  side1(u, v, 1);
  side1(v, u, 0);
}
inline void init() {
  n = read();
  m = read();
  s = n + m * 3 + 1;
  t = s + 1;
  for (int i = 1; i <= n; ++i) {
    int l = read(), r = read();
    side(s, i);
    if (l) side(i, n + l);
    if (r <= m) side(i, n + m + r);
  }
  int b2 = n + m, b3 = b2 + m;
  for (int i = 1; i < m; ++i) {
    side(n + i + 1, n + i);
    side(b2 + i, b2 + i + 1);
  }
  for (int i = 1; i <= m; ++i) {
    side(n + i, b3 + i);
    side(b2 + i, b3 + i);
    side(b3 + i, t);
  }
}
int d[1000005], gap[1000005];
inline int sap(int x, int flow) {
  if (x == t) return flow;
  int f, rest = flow;
  for (int i = st[x]; i; i = p1[i]) {
    int v = pv[i];
    if (pf[i] && d[x] == d[v] + 1) {
      f = sap(v, pf[i] < rest ? pf[i] : rest);
      pf[i] -= f;
      pf[i ^ 1] += f;
      rest -= f;
      if (!rest || d[s] == t) return flow - rest;
    }
  }
  if (!--gap[d[x]]) d[s] = t;
  ++gap[++d[x]];
  return flow - rest;
}
inline void solve() {
  int ans = n;
  while (d[s] < t) ans -= sap(s, 0x7fffffff);
  printf("%d\n", ans);
}
int main() {
  init();
  solve();
  return 0;
}
