#include <bits/stdc++.h>
using namespace std;
const long long INF = 10000000;
const double DINF = 1e30;
const long long mod = 1000000007;
const long long dx[4] = {1, 0, -1, 0};
const long long dy[4] = {0, -1, 0, 1};
const complex<double> I = complex<double>(0, 1);
const double EPS = 1e-10;
const long long NCK_MAX = 510000;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long extgcd(long long a, long long b, long long& x, long long& y) {
  if (b == 0) {
    x = 1, y = 0;
    return a;
  }
  long long q = a / b, g = extgcd(b, a - q * b, x, y);
  long long z = x - q * y;
  x = y;
  y = z;
  return g;
}
long long invmod(long long a, long long m) {
  long long x, y;
  extgcd(a, m, x, y);
  x %= m;
  if (x < 0) x += m;
  return x;
}
long long *fac, *finv, *inv;
void nCk_init(long long mod) {
  fac = new long long[NCK_MAX];
  finv = new long long[NCK_MAX];
  inv = new long long[NCK_MAX];
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (long long i = 2; i < NCK_MAX; i++) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}
long long nCk(long long n, long long k, long long mod) {
  if (fac == NULL) nCk_init(mod);
  if (n < k) return 0;
  if (n < 0 || k < 0) return 0;
  return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
template <typename T>
class Zip {
  vector<T> d;
  bool flag;
  void init() {
    sort(d.begin(), d.end());
    d.erase(unique(d.begin(), d.end()), d.end());
    flag = false;
  }

 public:
  Zip() { flag = false; }
  void add(T x) {
    d.push_back(x);
    flag = true;
  }
  long long getNum(T x) {
    if (flag) init();
    return lower_bound(d.begin(), d.end(), x) - d.begin();
  }
  long long size() {
    if (flag) init();
    return (long long)d.size();
  }
};
class UnionFind {
  vector<long long> par, rank;

 public:
  void init(long long n) {
    par.resize(n);
    rank.resize(n);
    fill(par.begin(), par.end(), 1);
    fill(rank.begin(), rank.end(), 0);
  }
  long long getSize(long long x) { return par[find(x)]; }
  long long find(long long x) {
    if (par[x] > 0) return x;
    return -(par[x] = -find(-par[x]));
  }
  void merge(long long x, long long y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (rank[x] < rank[y]) {
      par[y] += par[x];
      par[x] = -y;
    } else {
      par[x] += par[y];
      par[y] = -x;
      if (rank[x] == rank[y]) rank[x]++;
    }
  }
  bool isSame(long long x, long long y) { return find(x) == find(y); }
};
template <typename T>
class SegmentTree {
  long long n;
  vector<T> node;
  function<T(T, T)> fun, fun2;
  bool customChange;
  T outValue, initValue;

 public:
  void init(long long num, function<T(T, T)> resultFunction, T init, T out,
            function<T(T, T)> changeFunction = NULL) {
    fun = resultFunction;
    fun2 = changeFunction;
    customChange = changeFunction != NULL;
    n = 1;
    while (n < num) n *= 2;
    node.resize(2 * n - 1);
    fill(node.begin(), node.end(), init);
    outValue = out;
    initValue = init;
  }
  void valueChange(long long num, T value) {
    num += n - 1;
    if (customChange)
      node[num] = fun2(value, node[num]);
    else
      node[num] = value;
    while (num > 0)
      num = (num - 1) / 2,
      node[num] = fun(node[num * 2 + 1], node[num * 2 + 2]);
  }
  T rangeQuery(long long a, long long b, long long l = 0, long long r = -1,
               long long k = 0) {
    if (r == -1) r = n;
    if (a <= l && r <= b) return node[k];
    if (b <= l || r <= a) return outValue;
    long long mid = (l + r) / 2;
    return fun(rangeQuery(a, b, l, mid, 2 * k + 1),
               rangeQuery(a, b, mid, r, 2 * k + 2));
  }
};
template <typename T>
class Graph {
  struct edge {
    long long to;
    T cost;
  };
  struct edge_data {
    long long from, to;
    T cost;
  };
  long long v;
  vector<vector<edge>> e, re;
  vector<edge_data> ed;
  vector<bool> used;
  vector<long long> vs, cmp;
  bool isDirected, isMinasEdge;

 public:
  Graph(long long _v, bool _isDirected = true) {
    v = _v, isDirected = _isDirected;
    isMinasEdge = false;
    e.resize(v), re.resize(v);
  }
  void add_edge(long long s, long long t, T cost = 1) {
    e[s].push_back((edge){t, cost});
    if (!isDirected)
      e[t].push_back((edge){s, cost});
    else
      re[t].push_back((edge){s, cost});
    ed.push_back((edge_data){s, t, cost});
    if (cost < 0) isMinasEdge = true;
  }
  vector<T> dijkstra(long long s) {
    vector<T> d(v, INF);
    d[s] = 0;
    auto edge_cmp = [](const edge& a, const edge& b) {
      return a.cost > b.cost;
    };
    priority_queue<edge, vector<edge>, decltype(edge_cmp)> pq(edge_cmp);
    pq.push((edge){s, 0});
    while (!pq.empty()) {
      edge temp = pq.top();
      pq.pop();
      if (d[temp.to] < temp.cost) continue;
      for (const edge& next : e[temp.to]) {
        T cost = temp.cost + next.cost;
        if (d[next.to] > cost) {
          d[next.to] = cost;
          pq.push((edge){next.to, cost});
        }
      }
    }
    return d;
  }
  vector<T> bellmanford(long long s) {
    vector<T> d(v, INF);
    d[s] = 0;
    for (long long i = 0; i < v; i++) {
      for (const edge_data& temp : ed) {
        if (d[temp.from] != INF && d[temp.to] > d[temp.from] + temp.cost)
          d[temp.to] = d[temp.from] + temp.cost;
        if (!isDirected && d[temp.to] != INF &&
            d[temp.from] > d[temp.to] + temp.cost)
          d[temp.from] = d[temp.to] + temp.cost;
      }
    }
    for (long long i = 0; i < v; i++) {
      for (const edge_data& temp : ed) {
        if (d[temp.from] != INF && d[temp.to] > d[temp.from] + temp.cost)
          d[temp.to] = -INF;
        if (!isDirected && d[temp.to] != INF &&
            d[temp.from] > d[temp.to] + temp.cost)
          d[temp.from] = -INF;
      }
    }
    return d;
  }
  vector<T> shortest_path(long long s) {
    if (isMinasEdge)
      return bellmanford(s);
    else
      return dijkstra(s);
  }
  T kruskal() {
    UnionFind uf;
    uf.init(v);
    auto edge_data_cmp = [](const edge_data& a, const edge_data& b) {
      return a.cost < b.cost;
    };
    sort(ed.begin(), ed.end(), edge_data_cmp);
    T ans = 0;
    for (const edge_data& temp : ed) {
      if (uf.isSame(temp.from, temp.to)) continue;
      uf.merge(temp.from, temp.to);
      ans += temp.cost;
    }
    return ans;
  }
  void scc_dfs(long long s) {
    used[s] = true;
    for (const edge& i : e[s])
      if (!used[i.to]) scc_dfs(i.to);
    vs.push_back(s);
  }
  void scc_rdfs(long long s, long long k) {
    used[s] = true;
    cmp[s] = k;
    for (const edge& i : re[s])
      if (!used[i.to]) scc_rdfs(i.to, k);
  }
  vector<long long> scc() {
    used.resize(v);
    fill(used.begin(), used.end(), false);
    cmp.resize(v);
    vs.clear();
    for (long long i = 0; i < v; i++)
      if (!used[i]) scc_dfs(i);
    used.resize(v);
    fill(used.begin(), used.end(), false);
    long long k = 0;
    for (long long i = vs.size() - 1; i >= 0; i--)
      if (!used[vs[i]]) scc_rdfs(vs[i], k++);
    return cmp;
  }
};
template <typename T>
class Flow {
  struct edge {
    long long to;
    T cap;
    long long rev;
  };
  vector<vector<edge>> G;
  vector<long long> level, iter;
  bool isDirected;

 public:
  Flow(long long n, bool _isDirected = true)
      : G(n), level(n), iter(n), isDirected(_isDirected) {}
  void add_edge(long long from, long long to, T cap) {
    G[from].emplace_back((edge){to, cap, (long long)G[to].size()});
    G[to].emplace_back(
        (edge){from, isDirected ? 0LL : cap, (long long)G[from].size() - 1});
  }
  void bfs(long long s) {
    fill(level.begin(), level.end(), -1);
    queue<long long> que;
    level[s] = 0;
    que.emplace(s);
    while (!que.empty()) {
      long long v = que.front();
      que.pop();
      for (long long i = 0; i < (long long)G[v].size(); i++) {
        edge& e = G[v][i];
        if (e.cap > 0 && level[e.to] < 0) {
          level[e.to] = level[v] + 1;
          que.emplace(e.to);
        }
      }
    }
  }
  T dfs(long long v, long long t, T f) {
    if (v == t) return f;
    for (long long& i = iter[v]; i < (long long)G[v].size(); i++) {
      edge& e = G[v][i];
      if (e.cap > 0 && level[v] < level[e.to]) {
        T d = dfs(e.to, t, min(f, e.cap));
        if (d == 0) continue;
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
    return 0;
  }
  T maxflow(long long s, long long t, T lim = INF) {
    T fl = 0;
    while (1) {
      bfs(s);
      if (level[t] < 0 || lim == 0) break;
      fill(iter.begin(), iter.end(), 0);
      while (1) {
        T f = dfs(s, t, lim);
        if (f == 0) break;
        fl += f;
        lim -= f;
      }
    }
    return fl;
  }
};
class BipartiteMatching {
  vector<long long> pre, root;
  vector<vector<long long>> to;
  vector<long long> p, q;
  long long n, m;

 public:
  BipartiteMatching(long long n, long long m)
      : pre(n, -1), root(n, -1), to(n), p(n, -1), q(m, -1), n(n), m(m) {}
  void add(long long a, long long b) { to[a].push_back(b); }
  long long solve() {
    long long res = 0;
    bool upd = true;
    while (upd) {
      upd = false;
      queue<long long> s;
      for (long long i = 0; i < n; ++i) {
        if (!~p[i]) {
          root[i] = i;
          s.push(i);
        }
      }
      while (!s.empty()) {
        long long v = s.front();
        s.pop();
        if (~p[root[v]]) continue;
        for (long long i = 0; i < (long long)to[v].size(); ++i) {
          long long u = to[v][i];
          if (!~q[u]) {
            while (~u) {
              q[u] = v;
              swap(p[v], u);
              v = pre[v];
            }
            upd = true;
            ++res;
            break;
          }
          u = q[u];
          if (~pre[u]) continue;
          pre[u] = v;
          root[u] = root[v];
          s.push(u);
        }
      }
      if (upd)
        fill(pre.begin(), pre.end(), -1), fill(root.begin(), root.end(), -1);
    }
    return res;
  }
};
class MinCostFlow {
 public:
  struct edge {
    long long to, cap, cost, rev;
  };
  long long V;
  vector<vector<edge>> G;
  vector<long long> dist, prevv, preve, h;
  MinCostFlow(long long v) : G(v), dist(v), prevv(v), preve(v), h(v) { V = v; }
  void add_edge(long long from, long long to, long long cap, long long cost) {
    G[from].push_back((edge){to, cap, cost, (long long)G[to].size()});
    G[to].push_back((edge){from, 0, -cost, (long long)G[from].size() - 1});
  }
  void add_undirected_edge(long long e1, long long e2, long long cap,
                           long long cost) {
    add_edge(e1, e2, cap, cost);
    add_edge(e2, e1, cap, cost);
  }
  long long min_cost_flow(long long s, long long t, long long f) {
    long long res = 0;
    while (f > 0) {
      fill(dist.begin(), dist.end(), INF);
      dist[s] = 0;
      bool update = true;
      while (update) {
        update = false;
        for (long long v = 0; v < V; v++) {
          if (dist[v] == INF) continue;
          for (long long i = 0; i < (long long)G[v].size(); i++) {
            edge& e = G[v][i];
            if (e.cap > 0 && dist[e.to] > dist[v] + e.cost) {
              dist[e.to] = dist[v] + e.cost;
              prevv[e.to] = v;
              preve[e.to] = i;
              update = true;
            }
          }
        }
      }
      if (dist[t] == INF) {
        return -1;
      }
      long long d = f;
      for (long long v = t; v != s; v = prevv[v]) {
        d = min(d, G[prevv[v]][preve[v]].cap);
      }
      f -= d;
      res += d * dist[t];
      for (long long v = t; v != s; v = prevv[v]) {
        edge& e = G[prevv[v]][preve[v]];
        e.cap -= d;
        G[v][e.rev].cap += d;
      }
    }
    return res;
  }
  long long min_cost_flow_dijkstra(long long s, long long t, long long f) {
    long long res = 0;
    fill(h.begin(), h.end(), 0);
    while (f > 0) {
      priority_queue<pair<long long, long long>,
                     vector<pair<long long, long long>>,
                     greater<pair<long long, long long>>>
          que;
      fill(dist.begin(), dist.end(), 0);
      dist[s] = 0;
      que.push(pair<long long, long long>(0, s));
      while (!que.empty()) {
        pair<long long, long long> p = que.top();
        que.pop();
        long long v = p.second;
        if (dist[v] < p.first) continue;
        for (long long i = 0; i < G[v].size(); i++) {
          edge& e = G[v][i];
          if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
            dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
            prevv[e.to] = v;
            preve[e.to] = i;
            que.push(pair<long long, long long>(dist[e.to], e.to));
          }
        }
      }
      if (dist[t] == INF) {
        return -1;
      }
      for (long long v = 0; v < V; v++) h[v] += dist[v];
      long long d = f;
      for (long long v = t; v != s; v = prevv[v]) {
        d = min(d, G[prevv[v]][preve[v]].cap);
      }
      f -= d;
      res += d * h[t];
      for (long long v = t; v != s; v = prevv[v]) {
        edge& e = G[prevv[v]][preve[v]];
        e.cap -= d;
        G[v][e.rev].cap += d;
      }
      return res;
    }
    return 0;
  }
};
long long n, m, l, r, mx = 1;
void range_add(Flow<long long>& flow, long long from, long long a, long long b,
               long long l = 0, long long r = -1, long long k = 0) {
  if (r == -1) r = mx + 1;
  if (a <= l && r <= b) {
    flow.add_edge(from, k, 1);
    return;
  }
  if (b <= l || r <= a) return;
  long long mid = (l + r) / 2;
  return range_add(flow, from, a, b, l, mid, 2 * k + 1),
         range_add(flow, from, a, b, mid, r, 2 * k + 2);
}
int main() {
  scanf("%lld%lld", &n, &m);
  while (mx <= m + 1) mx *= 2;
  Flow<long long> flow(mx * 2 - 1 + n + 2);
  for (long long i = 0; i < n; i++) {
    scanf("%lld%lld", &l, &r);
    range_add(flow, mx * 2 - 1 + i, 1, l + 1);
    range_add(flow, mx * 2 - 1 + i, r, m + 1);
  }
  long long S = 2 * mx + n - 1, T = 2 * mx + n;
  for (long long i = 0; i < mx - 1; i++)
    flow.add_edge(i, 2 * i + 1, INF), flow.add_edge(i, 2 * i + 2, INF);
  for (long long i = 1; i <= m; i++) flow.add_edge(i + mx - 1, T, 1);
  for (long long i = 0; i < n; i++) flow.add_edge(S, i + 2 * mx - 1, 1);
  printf("%lld\n", n - flow.maxflow(S, T));
}
