#include <bits/stdc++.h>
using namespace std;
const int inf = 10000000;
int edge[4000000], nxt[4000000], flow[4000000], first[1000000];
int g[1000000], h[1000000];
int i, m, n, s, x, y, head, tail, sum_edge;
inline void addedge(int x, int y, int f) {
  sum_edge++, edge[sum_edge] = y, nxt[sum_edge] = first[x], flow[sum_edge] = f,
              first[x] = sum_edge;
  return;
}
inline int oppedge(int x) { return ((x - 1) ^ 1) + 1; }
inline void buildgraph() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; i++) {
    if (i < m) addedge(i, i + 1, inf), addedge(i + 1, i, 0);
    if (i < m) addedge(m + i + 1, m + i, inf), addedge(m + i, m + i + 1, 0);
    addedge(i, m + m + i, 1), addedge(m + m + i, i, 0);
    addedge(m + i, m + m + i, 1), addedge(m + m + i, m + i, 0);
    addedge(m + m + i, m + m + m + n + 1, 1),
        addedge(m + m + m + n + 1, m + m + i, 0);
  }
  for (i = 1; i <= n; i++) {
    scanf("%d%d", &x, &y);
    addedge(0, m + m + m + i, 1), addedge(m + m + m + i, 0, 0);
    if (x > 0)
      addedge(m + m + m + i, m + x, 1), addedge(m + x, m + m + m + i, 0);
    if (y <= m) addedge(m + m + m + i, y, 1), addedge(y, m + m + m + i, 0);
  }
  return;
}
inline int dinic_bfs() {
  memset(h, 0, sizeof(h));
  tail = 1, g[tail] = 0, h[g[tail]] = 1;
  for (head = 1; (head <= tail) && (!h[m + m + m + n + 1]); head++)
    for (i = first[g[head]]; i != 0; i = nxt[i])
      if ((flow[i]) && (!h[edge[i]]))
        tail++, g[tail] = edge[i], h[g[tail]] = h[g[head]] + 1;
  return h[m + m + m + n + 1];
}
inline int dinic_dfs(int now, int cap) {
  if (now == m + m + m + n + 1) return cap;
  int s = 0, t = 0;
  for (int i = first[now]; i != 0; i = nxt[i])
    if ((flow[i]) && (h[edge[i]] == h[now] + 1)) {
      t = dinic_dfs(edge[i], min(cap, flow[i]));
      s = s + t, cap = cap - t;
      flow[i] = flow[i] - t, flow[oppedge(i)] = flow[oppedge(i)] + t;
      if (!cap) break;
    }
  if (!s) h[now] = 0;
  return s;
}
inline void maxflow() {
  while (dinic_bfs()) s = s + dinic_dfs(0, inf);
  printf("%d", n - s);
  return;
}
int main() {
  buildgraph();
  maxflow();
  return 0;
}
