#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void init(T& x) {
  x = 0;
  char ch = getchar();
  bool t = 0;
  for (; ch > '9' || ch < '0'; ch = getchar())
    if (ch == '-') t = 1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = (x << 1) + (x << 3) + (ch - 48);
  if (t) x = -x;
  return;
}
const int N = 2e5 + 10;
const int MAXN = N << 2;
const int M = 5e6;
const int INF = 1e9;
int L[N], R[N], n, m;
int ls[MAXN], rs[MAXN], cur = 0;
int S = 0, T;
struct edge {
  int to, next, cap;
} a[M];
int head[MAXN], cnt = 0;
int Now[MAXN], d[MAXN];
inline void add(int x, int y, int z) {
  a[cnt] = (edge){y, head[x], z};
  head[x] = cnt++;
}
inline void add_edge(int x, int y, int z) { add(x, y, z), add(y, x, 0); }
int TAIL = 0;
void Build(int& u, int l, int r) {
  u = ++cur;
  if (l == r) return add_edge(T + u, T, 1);
  int mid = (l + r) >> 1;
  Build(ls[u], l, mid);
  if (ls[u]) add_edge(T + u, T + ls[u], INF);
  Build(rs[u], mid + 1, r);
  if (rs[u]) add_edge(T + u, T + rs[u], INF);
  return;
}
void Link(int u, int l, int r, int L, int R, int fr) {
  if (l >= L && r <= R) return add_edge(fr, T + u, INF);
  int mid = (l + r) >> 1;
  if (mid >= L) Link(ls[u], l, mid, L, R, fr);
  if (mid < R) Link(rs[u], mid + 1, r, L, R, fr);
  return;
}
int dfs(int u, int flow) {
  if (u == T) return flow;
  int rest = flow;
  for (int v, &i = Now[u]; ~i; i = a[i].next) {
    v = a[i].to;
    if (!a[i].cap || d[v] != d[u] + 1) continue;
    int f = dfs(v, min(rest, a[i].cap));
    rest -= f, a[i].cap -= f, a[i ^ 1].cap += f;
    if (!f) d[v] = 0;
    if (!rest) break;
  }
  return flow - rest;
}
queue<int> Q;
inline bool bfs() {
  for (int i = S; i <= TAIL; ++i) d[i] = 0;
  d[S] = 1;
  while (!Q.empty()) Q.pop();
  Q.push(S);
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (int v, i = head[u]; ~i; i = a[i].next) {
      v = a[i].to;
      if (!a[i].cap || d[v]) continue;
      d[v] = d[u] + 1;
      if (v == T) return 1;
      Q.push(v);
    }
  }
  return d[T];
}
inline int Dinic() {
  int flow = 0;
  while (bfs()) {
    for (int i = S; i <= TAIL; ++i) Now[i] = head[i];
    flow += dfs(S, INF);
  }
  return flow;
}
int main() {
  init(n), init(m);
  memset(head, -1, sizeof(head));
  S = 0;
  T = n + 1;
  int rt = 0;
  Build(rt, 1, m);
  for (int i = 1; i <= n; ++i) {
    add_edge(S, i, 1);
    init(L[i]), init(R[i]);
    if (L[i]) Link(rt, 1, m, 1, L[i], i);
    if (R[i] <= m) Link(rt, 1, m, R[i], m, i);
  }
  TAIL = T + cur;
  printf("%d\n", n - Dinic());
  return 0;
}
