#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:167772160")
using namespace std;
const int INF = (int)1e9 + 10;
const long long LINF = long long(2e18) + 10;
const double PI = acosl(-1);
const double eps = 1e-8;
const long double EPS = 1e-12;
struct Edge {
  int to, flow, cap, rgo;
  Edge(int to, int cap) : flow(), to(to), cap(cap) {}
  inline int left() const { return cap - flow; }
};
const int S = 0;
const int T = 1;
int vptr = 2;
const int N = 2e6 + 10;
int n, m;
vector<Edge>::iterator its[N];
vector<Edge> edges[N];
int lid[N];
int tol[N], tor[N];
int d[N];
int q[N];
int ql, qr;
void add_edge(int a, int b, int cap) {
  edges[a].emplace_back(b, cap);
  edges[b].emplace_back(a, 0);
  edges[a].back().rgo = edges[b].size() - 1;
  edges[b].back().rgo = edges[a].size() - 1;
}
int build(int l, int r) {
  if (l == r) {
    add_edge(vptr, lid[l], 1);
    return vptr++;
  }
  int m = (l + r) / 2;
  int now = vptr++;
  tol[now] = build(l, m);
  tor[now] = build(m + 1, r);
  add_edge(now, tol[now], m - l + 1);
  add_edge(now, tor[now], r - m);
  return now;
}
void add_guy(int ind, int l, int r, int v, int vl, int vr) {
  if (vl > vr) return;
  if (vl <= l && r <= vr) {
    add_edge(v, ind, 1);
    return;
  }
  int m = (l + r) / 2;
  add_guy(tol[ind], l, m, v, vl, min(vr, m));
  add_guy(tor[ind], m + 1, r, v, max(vl, m + 1), vr);
}
bool bfs() {
  fill(d, d + vptr, -1);
  d[S] = 0;
  ql = qr = 0;
  q[qr++] = S;
  for (; ql != qr; ++ql) {
    int v = q[ql];
    for (auto e : edges[v])
      if (d[e.to] == -1 && e.left() > 0) {
        d[e.to] = d[v] + 1;
        q[qr++] = e.to;
      }
  }
  return d[T] != -1;
}
int dfs(int v, int flow) {
  if (v == T || flow == 0) return flow;
  for (auto& it = its[v]; it != edges[v].end(); ++it)
    if (it->left() > 0 && d[it->to] == d[v] + 1) {
      int delta = dfs(it->to, min(flow, it->left()));
      if (delta > 0) {
        it->flow += delta;
        edges[it->to][it->rgo].flow -= delta;
        return delta;
      }
    }
  return 0;
}
int dinic() {
  int res = 0;
  while (bfs()) {
    for (int i = 0; i < vptr; ++i) its[i] = edges[i].begin();
    while (true) {
      int delta = dfs(S, INF);
      if (delta > 0)
        res += delta;
      else
        break;
    }
  }
  return res;
}
void solve() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; ++i) {
    lid[i] = vptr++;
    add_edge(lid[i], T, 1);
  }
  int ftree = build(0, m - 1);
  int stree = build(0, m - 1);
  for (int i = 0; i < n; ++i) {
    int l, r;
    scanf("%d%d", &l, &r);
    --l, --r;
    int v = vptr++;
    add_edge(S, v, 1);
    add_guy(ftree, 0, m - 1, v, 0, l);
    add_guy(stree, 0, m - 1, v, r, m - 1);
  }
  printf("%d\n", n - dinic());
}
int main() { solve(); }
