#include <bits/stdc++.h>
using namespace std;
namespace MaxFlow {
const int INF = (int)1e9;
struct Edge {
  int from;
  int to;
  int flow;
  int cap;
  Edge(int from_, int to_, int cap_)
      : from(from_), to(to_), flow(0), cap(cap_) {}
};
struct Flow {
  vector<Edge> es;
  vector<vector<int>> g;
  vector<int> dist;
  vector<int> start;
  int st{-1};
  int en{-1};
  Flow(int n) : g(n), dist(n), start(n) {}
  void add_edge(int from, int to, int cap) {
    g[from].push_back(es.size());
    g[to].push_back(es.size() + 1);
    es.emplace_back(from, to, cap);
    es.emplace_back(to, from, 0);
  }
  bool bfs() {
    fill(dist.begin(), dist.end(), INF);
    queue<int> q;
    dist[st] = 0;
    q.push(st);
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      for (int i : g[v])
        if (es[i].cap - es[i].flow != 0 && dist[es[i].to] > dist[v] + 1) {
          dist[es[i].to] = dist[v] + 1;
          q.push(es[i].to);
        }
    }
    return dist[en] != INF;
  }
  int dfs(int v, int lim) {
    if (v == en) return lim;
    if (dist[v] >= dist[en]) return 0;
    for (; start[v] < (int)g[v].size(); start[v]++) {
      int i = g[v][start[v]];
      if (es[i].cap - es[i].flow != 0 && dist[es[i].to] == dist[v] + 1) {
        int add = dfs(es[i].to, min(lim, es[i].cap - es[i].flow));
        if (add != 0) {
          es[i].flow += add;
          es[i ^ 1].flow -= add;
          return add;
        }
      }
    }
    return 0;
  }
  int get_flow(int st_, int en_) {
    st = st_;
    en = en_;
    int flow = 0;
    while (bfs()) {
      fill(start.begin(), start.end(), 0);
      int add = 0;
      while ((add = dfs(st, INF)) != 0) flow += add;
    }
    return flow;
  }
};
};  // namespace MaxFlow
using ll = long long;
using pii = pair<int, int>;
using ld = long double;
using pll = pair<ll, ll>;
const int N = 1000 * 1000;
int last;
int id[N];
int pref[N];
int suf[N];
int fin[N];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  cout.setf(ios::fixed);
  cout.precision(12);
  int n, m;
  cin >> n >> m;
  last = n;
  for (int i = 0; i < m; i++) {
    pref[i] = last++;
    suf[i] = last++;
    fin[i] = last++;
  }
  int S = last++;
  int T = last++;
  MaxFlow::Flow flow(last);
  for (int i = 0; i < n; i++) {
    flow.add_edge(S, i, 1);
    int l, r;
    cin >> l >> r;
    if (l > 0) flow.add_edge(i, pref[l - 1], 1);
    if (r <= m) flow.add_edge(i, suf[r - 1], 1);
  }
  for (int i = 0; i < m; i++) {
    flow.add_edge(fin[i], T, 1);
    flow.add_edge(pref[i], fin[i], 1);
    flow.add_edge(suf[i], fin[i], 1);
    if (i > 0) flow.add_edge(pref[i], pref[i - 1], N);
    if (i + 1 < m) flow.add_edge(suf[i], suf[i + 1], N);
  }
  cout << n - flow.get_flow(S, T) << "\n";
}
