#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, s = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') s = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return s * x;
}
const int MAXN = 1000005, MAXM = 2000006, INF = 2147483647;
int en, u[MAXN], cur[MAXN];
struct edge {
  int v, nt, c;
} e[MAXM];
inline void clear() {
  en = 0;
  memset(u, -1, sizeof(u));
}
inline void addEdge(int x, int y, int z) {
  e[en] = (edge){y, u[x], z};
  u[x] = en++;
  e[en] = (edge){x, u[y], 0};
  u[y] = en++;
}
int S, T, nn, rear, head, Q[MAXN], dis[MAXN];
int BFS() {
  for (int i = (1); i <= (nn); ++i) dis[i] = INF;
  rear = head = 0;
  dis[Q[++rear] = S] = 0;
  while (head < rear) {
    int x = Q[++head];
    for (int i = u[x]; ~i; i = e[i].nt) {
      int j = e[i].v;
      if (e[i].c > 0 && dis[j] > dis[x] + 1) dis[Q[++rear] = j] = dis[x] + 1;
    }
  }
  return dis[T] != INF;
}
int DFS(int now, int flow) {
  if (now == T) return flow;
  int ans = 0;
  for (int& i = cur[now]; ~i; i = e[i].nt) {
    int j = e[i].v;
    if (e[i].c > 0 && dis[j] == dis[now] + 1) {
      int k = min(e[i].c, flow);
      k = DFS(j, k);
      e[i].c -= k;
      e[i ^ 1].c += k;
      ans += k;
      flow -= k;
    }
    if (!flow) break;
  }
  return ans;
}
int Dinic() {
  int ans = 0;
  while (BFS()) {
    for (int i = (1); i <= (nn); ++i) cur[i] = u[i];
    ans += DFS(S, INF);
  }
  return ans;
}
int n, m, q, a[MAXN], b[MAXN];
int main() {
  n = read();
  m = read();
  clear();
  S = m * 3 + n + 1;
  T = nn = S + 1;
  for (int i = (1); i <= (m); ++i) {
    addEdge(i, T, 1);
    if (i != 1) addEdge(i + m, i - 1 + m, INF);
    addEdge(i + m, i, 1);
    if (i != m) addEdge(i + m * 2, i + 1 + m * 2, INF);
    addEdge(i + m * 2, i, 1);
  }
  for (int i = (1); i <= (n); ++i) {
    int l = read(), r = read();
    if (l > 0) addEdge(m * 3 + i, l + m, 1);
    if (r <= m) addEdge(m * 3 + i, r + m * 2, 1);
    addEdge(S, m * 3 + i, 1);
  }
  printf("%d\n", n - Dinic());
  return 0;
}
