#include <bits/stdc++.h>
using P = std::complex<long long>;
template <typename T>
class MaxFlow {
 private:
  struct Edge {
    int v;
    int r;
    T c;
  };

 private:
  std::vector<std::vector<Edge>> g;
  std::vector<int> d, it;

 public:
  MaxFlow(int n) : g(n), d(n), it(n) {}
  void add(int u, int v, T c) {
    int i = g[u].size();
    int j = g[v].size();
    g[u].push_back({v, j, c});
    g[v].push_back({u, i, 0});
  }
  T maxFlow(int s, int t) {
    T sum = 0;
    while (bfs(s, t)) {
      T f;
      while ((f = dfs(s, t, std::numeric_limits<T>::max())) > 0) {
        sum += f;
      }
    }
    return sum;
  }

 private:
  bool bfs(int s, int t) {
    std::fill(it.begin(), it.end(), 0);
    std::fill(d.begin(), d.end(), -1);
    d[s] = 0;
    std::queue<int> q;
    q.push(s);
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      for (Edge e : g[v]) {
        if (e.c > 0 && d[e.v] == -1) {
          d[e.v] = d[v] + 1;
          q.push(e.v);
        }
      }
    }
    return d[t] >= 0;
  }
  T dfs(int v, int t, T f) {
    if (v == t) {
      return f;
    }
    while (it[v] < g[v].size()) {
      Edge &e = g[v][it[v]++];
      if (e.c > 0 && d[v] < d[e.v]) {
        T ff = dfs(e.v, t, std::min(f, e.c));
        if (ff > 0) {
          e.c -= ff;
          g[e.v][e.r].c += ff;
          return ff;
        }
      }
    }
    return 0;
  }
};
constexpr int N = 1 << 18;
MaxFlow<int> mf(200000 + N * 2 - 1 + 2);
constexpr int B = 200000;
constexpr int src = B + N * 2 - 1;
constexpr int sink = src + 1;
void update(int a, int b, int v, int k = 0, int l = 0, int r = N) {
  if (r <= a || b <= l) return;
  if (a <= l && r <= b) {
    mf.add(v, B + k, 1);
    return;
  }
  update(a, b, v, k * 2 + 1, l, (l + r) / 2);
  update(a, b, v, k * 2 + 2, (l + r) / 2, r);
}
int main() {
  int n, m;
  std::cin >> n >> m;
  std::vector<int> size(N * 2 - 1, 1);
  for (int i = N - 2; i >= 0; i--) {
    size[i] = size[i * 2 + 1] * 2;
    mf.add(B + i, B + i * 2 + 1, size[i]);
    mf.add(B + i, B + i * 2 + 2, size[i]);
  }
  for (int i = 0; i < n; i++) {
    mf.add(src, i, 1);
  }
  for (int i = 1; i <= m; i++) {
    mf.add(B + N - 1 + i, sink, 1);
  }
  for (int i = 0; i < n; i++) {
    int l, r;
    scanf("%d %d", &l, &r);
    update(1, l + 1, i);
    update(r, m + 1, i);
  }
  int ans = n - mf.maxFlow(src, sink);
  std::cout << ans << std::endl;
}
