#include <bits/stdc++.h>
using namespace std;
template <class I>
void gi(I& x) {
  static char c;
  for (c = getchar(); c < '0' || c > '9'; c = getchar())
    ;
  for (x = 0; c <= '9' && c >= '0'; c = getchar()) x = x * 10 + (c & 15);
}
const int N = 8e5 + 5, M = 4e6 + 5, inf = 1e9;
int S, T, h[N], nxt[M], to[M], flow[M], cnt = 1, qu[N], ql, qr, d[N];
void addedge(int u, int v, int c) {
  to[++cnt] = v, nxt[cnt] = h[u], h[u] = cnt, flow[cnt] = c;
  to[++cnt] = u, nxt[cnt] = h[v], h[v] = cnt, flow[cnt] = 0;
}
bool bfs() {
  memset(d + S + 1, -1, (T - S) << 2);
  d[S] = 0, qu[ql = qr = 1] = S;
  while (ql <= qr) {
    int u = qu[ql++];
    for (int i = h[u], v; i; i = nxt[i])
      if (d[v = to[i]] == -1 && flow[i]) d[v] = d[u] + 1, qu[++qr] = v;
    if (d[T] != -1) break;
  }
  return d[T] != -1;
}
int dfs(int u, int mf) {
  if (u == T) return mf;
  int r = 0, p;
  for (int i = h[u], v; i; i = nxt[i])
    if (d[v = to[i]] == d[u] + 1 && flow[i])
      p = dfs(v, min(mf, flow[i])), flow[i] -= p, flow[i ^ 1] += p, r += p,
      mf -= p;
  return r;
}
int dinic() {
  int r = 0;
  while (bfs()) r += dfs(S, inf);
  return r;
}
int n, m;
int main() {
  gi(n), gi(m);
  S = 0, T = n + m + m + m + 1;
  for (int i = 1; i <= n; i++) addedge(S, i, 1);
  for (int i = 1; i <= m; i++) addedge(i + n, T, 1);
  for (int i = 1; i <= m; i++)
    addedge(i + n + m, i + n, inf), addedge(i + n + m + m, i + n, inf);
  for (int i = 2; i <= m; i++)
    addedge(i + n + m, i + n + m - 1, inf),
        addedge(i + n + m + m - 1, i + n + m + m, inf);
  for (int i = 1, l, r; i <= n; i++) {
    gi(l), gi(r);
    if (l >= 1) addedge(i, l + n + m, inf);
    if (r <= m) addedge(i, r + n + m + m, inf);
  }
  printf("%d\n", n - dinic());
  return 0;
}
