#include <bits/stdc++.h>
using namespace std;
const long long linf = 1e18;
const double eps = 1e-12;
const double pi = acos(-1);
template <typename T>
istream& operator>>(istream& is, vector<T>& vec) {
  for (auto&& x : vec) is >> x;
  return is;
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& vec) {
  for (long long i = (0), __last_i = (vec.size()); i < __last_i; i++) {
    if (i) os << " ";
    os << vec[i];
  }
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, const vector<vector<T>>& vec) {
  for (long long i = (0), __last_i = (vec.size()); i < __last_i; i++) {
    if (i) os << endl;
    os << vec[i];
  }
  return os;
}
class MaxFlow {
 public:
  struct Edge {
    long long to, cap, rev;
  };
  vector<vector<Edge>> G;
  vector<long long> iter;

 private:
  bool is_debug;
  long long V;
  vector<long long> bfs(long long s) {
    vector<long long> dist(V, linf);
    dist[s] = 0;
    queue<long long> Q;
    Q.push(s);
    while (!Q.empty()) {
      long long v = Q.front();
      Q.pop();
      for (auto&& e : G[v]) {
        if (e.cap > 0 && dist[e.to] == linf) {
          dist[e.to] = dist[v] + 1;
          Q.push(e.to);
        }
      }
    }
    return dist;
  }
  long long dfs(long long v, long long t, long long f,
                const vector<long long>& dist, vector<bool>& used) {
    if (v == t) return f;
    if (used[v]) return 0;
    used[v] = true;
    for (long long& i = iter[v]; i < G[v].size(); ++i) {
      Edge& e = G[v][i];
      if (e.cap > 0 && dist[e.to] == dist[v] + 1) {
        long long d = dfs(e.to, t, min(f, e.cap), dist, used);
        if (d > 0) {
          e.cap -= d;
          G[e.to][e.rev].cap += d;
          return d;
        }
      }
    }
    return 0;
  }

 public:
  const vector<vector<Edge>> Graph() { return G; }
  MaxFlow(long long V, bool is_debug = false)
      : V(V), G(V), is_debug(is_debug) {}
  void init(long long n) {
    V = n;
    G.assign(V, vector<Edge>());
  }
  void add(long long from, long long to, long long cap) {
    if (is_debug) cout << "ADD: " << from << " " << to << " " << cap << endl;
    assert(V > 0);
    G[from].push_back({to, cap, (long long)G[to].size()});
    G[to].push_back({from, 0, (long long)G[from].size() - 1});
  }
  void add(long long from, long long to, long long min_flow, long long cap,
           long long S, long long T) {
    if (is_debug)
      cout << endl
           << "ADD_MIN:" << from << " " << to << " " << min_flow << " " << cap
           << endl;
    add(from, to, cap - min_flow);
    add(S, to, min_flow);
    add(from, T, cap);
    if (is_debug) cout << endl;
  }
  long long flow(long long s, long long t, long long f = linf) {
    long long res = 0;
    while (f > 0) {
      vector<long long> dist = bfs(s);
      if (dist[t] == linf) break;
      iter.assign(G.size(), 0);
      while (f > 0) {
        vector<bool> used(V, false);
        long long df = dfs(s, t, f, dist, used);
        if (df == 0) break;
        f -= df;
        res += df;
      }
    }
    return res;
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n, m;
  cin >> n >> m;
  long long s = n + 3 * m, t = s + 1;
  MaxFlow mf(t + 1);
  for (long long i = (0), __last_i = (n); i < __last_i; i++) {
    long long l, r;
    cin >> l >> r;
    --l, --r;
    mf.add(s, i, 1);
    if (l >= 0) {
      mf.add(i, n + m + l, 1);
    }
    if (r < m) {
      mf.add(i, n + 2 * m + r, 1);
    }
  }
  for (long long i = (1), __last_i = (m); i < __last_i; i++) {
    mf.add(n + m + i, n + m + i - 1, linf);
    mf.add(n + 2 * m + i - 1, n + 2 * m + i, linf);
  }
  for (long long i = (0), __last_i = (m); i < __last_i; i++) {
    mf.add(n + i, t, 1);
    mf.add(n + 2 * m + i, n + i, 1);
    mf.add(n + m + i, n + i, 1);
  }
  cout << n - mf.flow(s, t) << endl;
}
