#include <bits/stdc++.h>
using namespace std;
int h[400025], to[1300005], ne[1300005], fl[1300005], en = 0, n, m, k;
int S = 400025 - 2, T = 400025 - 1, dis[400025];
queue<int> q;
void add(int a, int b, int c) {
  to[en] = b;
  ne[en] = h[a];
  fl[en] = c;
  h[a] = en++;
  to[en] = a;
  ne[en] = h[b];
  fl[en] = 0;
  h[b] = en++;
}
bool tell() {
  memset(dis, -1, sizeof dis);
  dis[S] = 0;
  q.push(S);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = h[x]; i >= 0; i = ne[i])
      if (dis[to[i]] == -1 && fl[i] > 0) {
        dis[to[i]] = dis[x] + 1;
        q.push(to[i]);
      }
  }
  return dis[T] != -1;
}
int zeng(int k, int now) {
  if (k == T) return now;
  int ret = 0;
  for (int i = h[k]; i >= 0 && ret < now; i = ne[i])
    if (dis[to[i]] == dis[k] + 1 && fl[i] > 0) {
      int tmp = zeng(to[i], min(fl[i], now - ret));
      fl[i] -= tmp;
      fl[i ^ 1] += tmp;
      ret += tmp;
    }
  if (!ret) dis[k] = -1;
  return ret;
}
int dinic() {
  int ret = 0, tmp;
  while (tell())
    while ((tmp = zeng(S, 0x3f3f3f3f))) ret += tmp;
  return ret;
}
int main() {
  scanf("%d%d", &n, &m);
  memset(h, -1, sizeof h);
  for (int i = 1; i <= n; ++i) add(S, i, 1);
  for (int i = 1; i < m; ++i) add(i + n, i + n + 1, 0x3f3f3f3f);
  for (int i = m; i > 1; --i) add(i + m + n, i + m + n - 1, 0x3f3f3f3f);
  for (int i = 1; i <= m; ++i)
    add(i + n, n + m + m + i, 1), add(i + m + n, n + m + m + i, 1),
        add(n + m + m + i, T, 1);
  for (int i = 1; i <= n; ++i) {
    int l, r;
    scanf("%d%d", &l, &r);
    if (r <= m) add(i, n + r, 1);
    if (l >= 1) add(i, n + m + l, 1);
  }
  printf("%d\n", n - dinic());
}
