#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
inline long long getint() {
  long long _x = 0, _tmp = 1;
  char _tc = getchar();
  while ((_tc < '0' || _tc > '9') && _tc != '-') _tc = getchar();
  if (_tc == '-') _tc = getchar(), _tmp = -1;
  while (_tc >= '0' && _tc <= '9') _x *= 10, _x += (_tc - '0'), _tc = getchar();
  return _x * _tmp;
}
inline long long add(long long _x, long long _y,
                     long long _mod = 1000000007LL) {
  _x += _y;
  return _x >= _mod ? _x - _mod : _x;
}
inline long long sub(long long _x, long long _y,
                     long long _mod = 1000000007LL) {
  _x -= _y;
  return _x < 0 ? _x + _mod : _x;
}
inline long long mul(long long _x, long long _y,
                     long long _mod = 1000000007LL) {
  _x *= _y;
  return _x >= _mod ? _x % _mod : _x;
}
long long mypow(long long _a, long long _x, long long _mod) {
  if (_x == 0) return 1LL;
  long long _ret = mypow(mul(_a, _a, _mod), _x >> 1, _mod);
  if (_x & 1) _ret = mul(_ret, _a, _mod);
  return _ret;
}
long long mymul(long long _a, long long _x, long long _mod) {
  if (_x == 0) return 0LL;
  long long _ret = mymul(add(_a, _a, _mod), _x >> 1, _mod);
  if (_x & 1) _ret = add(_ret, _a, _mod);
  return _ret;
}
inline bool equal(double _x, double _y) {
  return _x > _y - 1e-9 && _x < _y + 1e-9;
}
int __ = 1, _cs;
struct Maxflow {
  static const int MAXV = 200010;
  static const int INF = 1000000;
  struct Edge {
    int v, c, r;
    Edge(int _v, int _c, int _r) : v(_v), c(_c), r(_r) {}
  };
  int s, t;
  vector<Edge> G[MAXV * 2];
  int iter[MAXV * 2], d[MAXV * 2], gap[MAXV * 2], tot;
  void init(int x) {
    tot = x + 2;
    s = x + 1, t = x + 2;
    for (int i = 0; i <= tot; i++) {
      G[i].clear();
      iter[i] = d[i] = gap[i] = 0;
    }
  }
  void addEdge(int u, int v, int c) {
    G[u].push_back(Edge(v, c, (int)(G[v]).size()));
    G[v].push_back(Edge(u, 0, (int)(G[u]).size() - 1));
  }
  int dfs(int p, int flow) {
    if (p == t) return flow;
    for (int &i = iter[p]; i < (int)(G[p]).size(); i++) {
      Edge &e = G[p][i];
      if (e.c > 0 && d[p] == d[e.v] + 1) {
        int f = dfs(e.v, min(flow, e.c));
        if (f) {
          e.c -= f;
          G[e.v][e.r].c += f;
          return f;
        }
      }
    }
    if ((--gap[d[p]]) == 0)
      d[s] = tot;
    else {
      d[p]++;
      iter[p] = 0;
      ++gap[d[p]];
    }
    return 0;
  }
  int solve() {
    int res = 0;
    gap[0] = tot;
    for (res = 0; d[s] < tot; res += dfs(s, INF))
      ;
    return res;
  }
} flow;
void build() {}
int n, m;
int id[202020 << 2], nid;
void go(int no, int l, int r) {
  id[no] = ++nid;
  if (l == r) return;
  go((no << 1), l, ((l + r) >> 1));
  go((1 + (no << 1)), ((l + r) >> 1) + 1, r);
}
void go2(int no, int l, int r) {
  if (l == r) {
    flow.addEdge(id[no], flow.t, 1);
    return;
  }
  go2((no << 1), l, ((l + r) >> 1));
  go2((1 + (no << 1)), ((l + r) >> 1) + 1, r);
  flow.addEdge(id[no], id[(no << 1)], ((l + r) >> 1) - l + 1);
  flow.addEdge(id[no], id[(1 + (no << 1))], r - ((l + r) >> 1));
}
void add(int no, int l, int r, int ql, int qr, int who) {
  if (r < ql or l > qr) return;
  if (ql <= l and r <= qr) {
    flow.addEdge(who, id[no], 1);
    return;
  }
  add((no << 1), l, ((l + r) >> 1), ql, qr, who);
  add((1 + (no << 1)), ((l + r) >> 1) + 1, r, ql, qr, who);
}
void init() {
  n = getint();
  m = getint();
  nid = n;
  go(1, 1, m);
  flow.init(nid);
}
void solve() {
  go2(1, 1, m);
  for (int i = 1; i <= n; i++) {
    int li = getint();
    int ri = getint();
    flow.addEdge(flow.s, i, 1);
    if (1 <= li) add(1, 1, m, 1, li, i);
    if (ri <= m) add(1, 1, m, ri, m, i);
  }
  printf("%d\n", n - flow.solve());
}
int main() {
  build();
  while (__--) {
    init();
    solve();
  }
}
