#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int SIZE = 1e6 + 10;
using namespace std;
struct E {
  int k, c;
  E() {}
  E(int _k, int _c) : k(_k), c(_c) {}
} es[8200010];
struct Flow {
  int n, m, dis[1000010];
  int qq[1000010], qr, ql;
  vector<int> e[1000010];
  void init(int _n) {
    n = _n;
    m = 0;
    for (int i = 0; i < n; i++) e[i] = vector<int>();
  }
  void add_edge(int a, int b, int c) {
    e[a].push_back(m);
    es[m] = E(b, c);
    m++;
    e[b].push_back(m);
    es[m] = E(a, 0);
    m++;
  }
  bool BFS() {
    memset(dis, -1, n * sizeof(int));
    ql = qr = 0;
    qq[qr++] = 0;
    dis[0] = 0;
    while (ql != qr && dis[n - 1] == -1) {
      int p = qq[ql++];
      for (__typeof((e[p]).begin()) it = (e[p]).begin(); it != (e[p]).end();
           it++) {
        E ee = es[*it];
        if (ee.c == 0 || dis[ee.k] != -1) continue;
        dis[ee.k] = dis[p] + 1;
        qq[qr++] = ee.k;
      }
    }
    return dis[n - 1] != -1;
  }
  long long go(int p, long long c) {
    if (p == n - 1) return c;
    long long ret = 0, tmp;
    for (__typeof((e[p]).begin()) it = (e[p]).begin(); it != (e[p]).end();
         it++) {
      E &ee = es[*it];
      if (ee.c == 0 || dis[p] + 1 != dis[ee.k]) continue;
      tmp = go(ee.k, min(c - ret, (long long)ee.c));
      ret += tmp;
      ee.c -= tmp;
      es[(*it) ^ 1].c += tmp;
      if (ret == c) break;
    }
    if (ret == 0) dis[p] = -1;
    return ret;
  }
  long long maxflow() {
    long long ret = 0;
    while (BFS()) ret += go(0, 1LL << 60);
    return ret;
  }
} flow;
int main() {
  int N, M;
  scanf("%d%d", &N, &M);
  flow.init(4 * M + 2);
  for (int i = (1); i < (N + 1); ++i) {
    flow.add_edge(0, i, 1);
    int L, R;
    scanf("%d%d", &L, &R);
    if (L) flow.add_edge(i, M + L, 1);
    if (R <= M) flow.add_edge(i, M * 2 + R, 1);
  }
  for (int i = (1); i < (M + 1); ++i) {
    if (i != 1) flow.add_edge(M + i, M + i - 1, 1034567890);
    if (i < M) flow.add_edge(2 * M + i, 2 * M + i + 1, 1034567890);
    flow.add_edge(M + i, 3 * M + i, 1);
    flow.add_edge(2 * M + i, 3 * M + i, 1);
    flow.add_edge(3 * M + i, 4 * M + 1, 1);
  }
  printf("%d\n", N - flow.maxflow());
  return 0;
}
