#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
const int INF = 0x3f3f3f3f;
int _w;
int n, m;
int idx[N], idy[N * 4], s, t, nid;
namespace Dinic {
const int V = 1000010;
const int E = 4000010;
struct Edge {
  int u, v, c, f;
  Edge() {}
  Edge(int u, int v, int c, int f) : u(u), v(v), c(c), f(f) {}
};
int n, m, s, t;
int head[V], nxt[E];
Edge edge[E];
int dis[V], cur[V], q[V], begin, end;
void init(int _n) {
  n = _n, m = 0;
  memset(head, -1, sizeof head);
}
void link(int u, int v, int c) {
  edge[m] = Edge(u, v, c, 0);
  nxt[m] = head[u], head[u] = m++;
  edge[m] = Edge(v, u, 0, 0);
  nxt[m] = head[v], head[v] = m++;
}
bool bfs() {
  for (int i = 0; i < n; ++i) dis[i] = -1;
  begin = end = 0, q[end++] = s, dis[s] = 0;
  while (begin != end) {
    int u = q[begin++];
    for (int i = head[u]; ~i; i = nxt[i]) {
      Edge &e = edge[i];
      if (e.c > e.f && dis[e.v] == -1) {
        dis[e.v] = dis[u] + 1;
        q[end++] = e.v;
      }
    }
  }
  return dis[t] != -1;
}
int dfs(int u, int res) {
  if (u == t || !res) return res;
  int flow = 0;
  for (int &i = cur[u]; ~i; i = nxt[i]) {
    Edge &e = edge[i];
    if (e.c > e.f && dis[u] + 1 == dis[e.v]) {
      int f = dfs(e.v, min(res, e.c - e.f));
      flow += f, res -= f;
      e.f += f, edge[i ^ 1].f -= f;
      if (!res) break;
    }
  }
  return flow;
}
int solve(int _s, int _t) {
  s = _s, t = _t;
  int flow = 0;
  while (bfs()) {
    for (int i = 0; i < n; ++i) cur[i] = head[i];
    flow += dfs(s, INF);
  }
  return flow;
}
}  // namespace Dinic
namespace SGT {
int ql, qr, qu;
void _init(int o, int L, int R) {
  idy[o] = nid++;
  if (L == R) return;
  int M = (L + R) / 2, lc = o << 1, rc = lc | 1;
  _init(lc, L, M), _init(rc, M + 1, R);
}
void init() { _init(1, 1, m); }
void _build(int o, int L, int R) {
  if (L == R) {
    Dinic::link(idy[o], t, 1);
  } else {
    int M = (L + R) / 2, lc = o << 1, rc = lc | 1;
    Dinic::link(idy[o], idy[lc], INF);
    Dinic::link(idy[o], idy[rc], INF);
    _build(lc, L, M), _build(rc, M + 1, R);
  }
}
void build() { _build(1, 1, m); }
void _link(int o, int L, int R) {
  if (L >= ql && R <= qr) {
    Dinic::link(qu, idy[o], 1);
  } else {
    int M = (L + R) / 2, lc = o << 1, rc = lc | 1;
    if (ql <= M) _link(lc, L, M);
    if (qr > M) _link(rc, M + 1, R);
  }
}
void link(int u, int l, int r) {
  ql = l, qr = r, qu = u;
  _link(1, 1, m);
}
}  // namespace SGT
int main() {
  _w = scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) idx[i] = nid++;
  SGT::init();
  s = nid++, t = nid++;
  Dinic::init(nid);
  SGT::build();
  for (int i = 1; i <= n; ++i) {
    int l, r;
    _w = scanf("%d%d", &l, &r);
    if (l >= 1) SGT::link(idx[i], 1, l);
    if (r <= m) SGT::link(idx[i], r, m);
    Dinic::link(s, idx[i], 1);
  }
  printf("%d\n", n - Dinic::solve(s, t));
  return 0;
}
