#include <bits/stdc++.h>
static const int MAXN = 2e5 + 4;
static const int MAXM = 2e5 + 4;
static const int INF = 0x3fffffff;
int n, m;
namespace nwf {
static const int MAXN = ::MAXN + ::MAXM * 2 + 2;
int n, source, sink;
struct edge {
  int dest, cap, resi, tag;
  edge() {}
  edge(int dest, int cap, int resi, int tag)
      : dest(dest), cap(cap), resi(resi), tag(tag) {}
};
std::vector<edge> e[MAXN];
inline void init(int _n) {
  n = _n;
  for (int i = 0; i < n; ++i) {
    e[i].clear();
    e[i].reserve(MAXN);
  }
}
inline std::vector<edge>::iterator add_edge(int u, int v, int c, int r = 0) {
  e[u].push_back(edge(v, c, e[v].size(), c));
  e[v].push_back(edge(u, r, e[u].size() - 1, r));
  return e[v].end() - 1;
}
inline void reset() {
  for (int u = 0; u < n; ++u) {
    for (std::vector<edge>::iterator w = e[u].begin(); w != e[u].end(); ++w) {
      w->cap = w->tag;
    }
  }
}
inline void remove_edge(std::vector<edge>::iterator w) {
  w->cap = 0;
  e[w->dest][w->resi].cap = 0;
}
inline void recover_edge(std::vector<edge>::iterator w) {
  w->cap = w->tag;
  e[w->dest][w->resi].cap = e[w->dest][w->resi].tag;
}
int lim;
int d[MAXN];
inline bool level_graph() {
  static int q[MAXN], qhead, qtail;
  for (int i = 0; i < n; ++i) d[i] = -1;
  d[source] = 0, q[0] = source, qhead = 0, qtail = 1;
  while (qhead < qtail) {
    int u = q[qhead++];
    for (std::vector<edge>::iterator w = e[u].begin(); w != e[u].end(); ++w)
      if (w->cap >= lim && d[w->dest] == -1) {
        d[w->dest] = d[u] + 1;
        q[qtail++] = w->dest;
      }
  }
  return (d[sink] >= 0);
}
int cur[MAXN];
inline int augment(int u, int cur_cap) {
  if (u == sink) return cur_cap;
  int new_cap;
  for (int &i = cur[u]; i < e[u].size(); ++i) {
    edge &w = e[u][i];
    if (w.cap >= lim && d[w.dest] == d[u] + 1 &&
        (new_cap = augment(w.dest, std::min(w.cap, cur_cap))) >= lim) {
      w.cap -= new_cap;
      e[w.dest][w.resi].cap += new_cap;
      return new_cap;
    }
  }
  return 0;
}
inline int deluge() {
  int ans = 0, aug;
  for (lim = 1 << 22; lim > 0; lim >>= 1) {
    while (level_graph()) {
      for (int i = 0; i < n; ++i) cur[i] = 0;
      while ((aug = augment(source, INF)) > 0) ans += aug;
    }
  }
  return ans;
}
}  // namespace nwf
int main() {
  scanf("%d%d", &n, &m);
  nwf::init(n + m * 2 + 2);
  nwf::source = n + m * 2;
  nwf::sink = n + m * 2 + 1;
  for (int i = 0; i < m; ++i) {
    if (i < m - 1) nwf::add_edge(i, i + 1, INF);
    if (i > 0) nwf::add_edge(m + i, m + i - 1, INF);
    nwf::add_edge(i, m + i, 1);
    nwf::add_edge(m + i, nwf::sink, 1);
  }
  for (int i = 0, l, r; i < n; ++i) {
    scanf("%d%d", &l, &r);
    --l, --r;
    nwf::add_edge(nwf::source, m * 2 + i, 1);
    if (l != -1) nwf::add_edge(m * 2 + i, m + l, 1);
    if (r != m) nwf::add_edge(m * 2 + i, r, 1);
  }
  printf("%d\n", n - nwf::deluge());
  return 0;
}
