#include <bits/stdc++.h>
using namespace std;
int MOD = 1e9 + 7;
int INF = 1e9;
struct edge {
  int from, to, cap;
  edge *rev;
  edge(int u, int v, int c) : from(u), to(v), cap(c), rev(NULL) {}
};
int n, s, t;
int dist[4111111], vis[4111111];
vector<edge *> g[4111111];
void addedge(int u, int v, int c) {
  edge *in = new edge(u, v, c);
  edge *out = new edge(v, u, 0);
  in->rev = out;
  out->rev = in;
  g[u].push_back(in);
  g[v].push_back(out);
}
int dfs(int u, int fo) {
  if (u == t) return fo;
  for (; vis[u] < g[u].size(); vis[u]++) {
    edge *e = g[u][vis[u]];
    int v = e->to;
    if (dist[v] != dist[u] + 1 || e->cap <= 0) continue;
    int foo = dfs(v, min(fo, e->cap));
    if (foo > 0) {
      e->cap -= foo;
      e->rev->cap += foo;
      return foo;
    }
  }
  return 0;
}
int maxflow() {
  int mxf = 0;
  for (;;) {
    for (int i = 0; i < n; i++) dist[i] = INF, vis[i] = 0;
    queue<int> q;
    q.push(s);
    dist[s] = 0;
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      if (u == t) break;
      for (edge *e : g[u]) {
        int v = e->to;
        if (e->cap > 0 && dist[v] == INF) {
          dist[v] = dist[u] + 1;
          q.push(v);
        }
      }
    }
    if (dist[t] == INF) break;
    while (int fo = dfs(s, INF)) mxf += fo;
  }
  return mxf;
}
int N, M, l[200005], r[200005], cnt = 0;
void segbuild(int u = 1, int lo = 0, int hi = M - 1) {
  if (lo == hi) {
    cnt = max(u, cnt);
    addedge(N + M + u, N + lo, 1);
  } else {
    int mi = lo + hi >> 1;
    addedge(N + M + u, N + M + (u << 1), INF);
    addedge(N + M + u, N + M + (u << 1 | 1), INF);
    segbuild(u << 1, lo, mi);
    segbuild(u << 1 | 1, mi + 1, hi);
  }
}
void segquery(int l, int r, int from, int u = 1, int lo = 0, int hi = M - 1) {
  if (l == lo && r == hi) {
    addedge(from, N + M + u, INF);
    return;
  }
  int mi = lo + hi >> 1;
  if (r <= mi)
    return segquery(l, r, from, u << 1, lo, mi);
  else if (l > mi)
    return segquery(l, r, from, u << 1 | 1, mi + 1, hi);
  else {
    segquery(l, mi, from, u << 1, lo, mi);
    segquery(mi + 1, r, from, u << 1 | 1, mi + 1, hi);
  }
}
int solve() {
  cin >> N >> M;
  segbuild();
  cnt++;
  n = N + M + cnt + 2;
  s = N + M + cnt, t = N + M + cnt + 1;
  for (int i = 0; i < N; i++) addedge(s, i, 1);
  for (int i = 0; i < N; i++) {
    cin >> l[i] >> r[i];
    if (l[i] > 0) l[i]--, segquery(0, l[i], i);
    if (r[i] < M + 1) r[i]--, segquery(r[i], M - 1, i);
  }
  for (int i = 0; i < M; i++) addedge(N + i, t, 1);
  cout << N - maxflow() << endl;
  return 0;
}
int main() {
  ios::sync_with_stdio(0);
  solve();
  return 0;
}
