#include <bits/stdc++.h>
using namespace std;
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;
template <class S, class T>
istream& operator>>(istream& is, pair<S, T>& p) {
  return is >> p.first >> p.second;
}
template <class S, class T>
ostream& operator<<(ostream& os, const pair<S, T>& p) {
  return os << p.first << " " << p.second;
}
template <class T>
void maxi(T& x, T y) {
  if (x < y) x = y;
}
template <class T>
void mini(T& x, T y) {
  if (x > y) x = y;
}
const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;
const int INF = 1e9;
struct EdgeEx {
  int to, cap, rev;
  EdgeEx(int to_ = 0, int cap_ = 0, int rev_ = 0)
      : to(to_), cap(cap_), rev(rev_) {}
};
vector<vector<EdgeEx> > G;
vector<bool> used;
void add_edge(int from, int to, int cap) {
  G[from].push_back(EdgeEx(to, cap, G[to].size()));
  G[to].push_back(EdgeEx(from, 0, G[from].size() - 1));
}
int max_flow_dfs(int u, int t, int f, vector<bool>& finished,
                 vector<int>& level) {
  if (u == t || f == 0) return f;
  if (finished[u]) return 0;
  finished[u] = true;
  for (auto& e : G[u]) {
    if (level[e.to] > level[u]) {
      int fp = max_flow_dfs(e.to, t, min(f, e.cap), finished, level);
      if (fp > 0) {
        e.cap -= fp;
        G[e.to][e.rev].cap += fp;
        finished[u] = false;
        return fp;
      }
    }
  }
  return 0;
}
int maximumFlow(int s, int t) {
  int N = G.size();
  int total = 0;
  for (bool up = true; up;) {
    up = false;
    vector<int> level(N, -1);
    level[s] = 0;
    queue<int> Q;
    Q.push(s);
    for (int d = N; !Q.empty() && level[Q.front()] < d;) {
      int u = Q.front();
      Q.pop();
      if (u == t) d = level[u];
      for (auto& e : G[u])
        if (e.cap > 0 && level[e.to] == -1)
          Q.push(e.to), level[e.to] = level[u] + 1;
    }
    vector<bool> finished(N);
    for (int f = 1; f > 0;) {
      f = max_flow_dfs(s, t, INF, finished, level);
      if (f == 0) break;
      total += f;
      up = true;
    }
  }
  return total;
}
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  G.resize(M * 3 + N + 10);
  int S = M * 3 + N + 1;
  int T = S + 1;
  for (int i = (0); i < (M); ++i) {
    add_edge(i, T, 1);
    add_edge(M + i, i, 1);
    if (0 < i) add_edge(M + i, M + i - 1, INF);
    add_edge(M + M + i, i, 1);
    if (i + 1 < M) add_edge(M + M + i, M + M + i + 1, INF);
  }
  for (int i = (0); i < (N); ++i) {
    int l, r;
    cin >> l >> r;
    if (l > 0) add_edge(3 * M + i, M + l - 1, 1);
    if (r <= M) add_edge(3 * M + i, M + M + r - 1, 1);
    add_edge(S, 3 * M + i, 1);
  }
  int ans = maximumFlow(S, T);
  cout << N - ans << endl;
  return 0;
}
