#include <bits/stdc++.h>
using namespace std;
inline int inn() {
  int x, ch;
  while ((ch = getchar()) < '0' || ch > '9')
    ;
  x = ch ^ '0';
  while ((ch = getchar()) >= '0' && ch <= '9')
    x = (x << 1) + (x << 3) + (ch ^ '0');
  return x;
}
struct edges {
  int to, pre, resf;
} e[200010 * 16];
int h[200010 * 4], etop, pre[200010], suf[200010], P[200010], Q[200010],
    lev[200010 * 4], cur[200010 * 4];
queue<int> q;
inline int add_edge(int u, int v, int f) {
  return e[++etop].to = v, e[etop].pre = h[u], e[etop].resf = f, h[u] = etop;
}
inline int bfs(int s, int t) {
  memset(lev, 0, sizeof(int) * (t + 1));
  while (!q.empty()) q.pop();
  q.push(s), lev[s] = 1;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = h[x], y; i; i = e[i].pre)
      if (e[i].resf && !lev[y = e[i].to]) lev[y] = lev[x] + 1, q.push(y);
  }
  return lev[t] > 0;
}
int dfs(int s, int t, int a) {
  if (s == t || !a) return a;
  int flow = 0, f;
  for (int &i = cur[s]; i; i = e[i].pre)
    if (lev[e[i].to] == lev[s] + 1 &&
        (f = dfs(e[i].to, t, min(a, e[i].resf))) > 0) {
      e[i].resf -= f, e[((i - 1) ^ 1) + 1].resf += f, flow += f, a -= f;
      if (!a) break;
    }
  return flow;
}
inline int dinic(int s, int t, int flow = 0) {
  for (; bfs(s, t); flow += dfs(s, t, (INT_MAX / 10)))
    memcpy(cur, h, sizeof(int) * (t + 1));
  return flow;
}
int main() {
  int n = inn(), m = inn(), c = 0;
  for (int i = 1; i <= n; i++) P[i] = ++c;
  for (int i = 1; i <= m; i++) Q[i] = ++c;
  for (int i = 1; i <= m; i++) pre[i] = ++c, suf[i] = ++c;
  for (int i = 1; i <= m; i++) {
    if (i > 1)
      add_edge(pre[i], pre[i - 1], (INT_MAX / 10)),
          add_edge(pre[i - 1], pre[i], 0);
    add_edge(pre[i], Q[i], (INT_MAX / 10)), add_edge(Q[i], pre[i], 0);
    if (i < m)
      add_edge(suf[i], suf[i + 1], (INT_MAX / 10)),
          add_edge(suf[i + 1], suf[i], 0);
    add_edge(suf[i], Q[i], (INT_MAX / 10)), add_edge(Q[i], suf[i], 0);
  }
  int s = ++c, t = ++c;
  for (int i = 1; i <= n; i++) add_edge(s, P[i], 1), add_edge(P[i], s, 0);
  for (int i = 1; i <= m; i++) add_edge(Q[i], t, 1), add_edge(t, Q[i], 0);
  for (int i = 1; i <= n; i++) {
    int L = inn(), R = inn();
    if (L >= 1)
      add_edge(P[i], pre[L], (INT_MAX / 10)), add_edge(pre[L], P[i], 0);
    if (R <= m)
      add_edge(P[i], suf[R], (INT_MAX / 10)), add_edge(suf[R], P[i], 0);
  }
  return !printf("%d\n", n - dinic(s, t));
}
