#include <bits/stdc++.h>
using namespace std;

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

struct TreapNode { // Multiset, can query max(index - val)
  int sz, prior, key;
  int val, ans, lazy;
  TreapNode *l, *r;

  TreapNode(int v, int k) {
    key = k;
    sz = 1, ans = val = v, lazy = 0;
    prior = rnd();
    l = r = nullptr;
  }
};

using tnode = TreapNode;
using pt = tnode*;

int sz(pt t) {
  return t != nullptr ? t->sz : 0;
}

int val(pt t) {
  return t != nullptr ? t->val : -1e9;
}

int ans(pt t) {
  return t != nullptr ? t->ans : -1e9;
}

void upd(pt t) {
  if (t != nullptr) {
    t->sz = sz(t->l) + sz(t->r) + 1;
    t->ans = max({ans(t->l), ans(t->r), t->val});
  }
}

void push(pt t) {
  if (t == nullptr) {
    return;
  }
  if (t->l != nullptr) {
    t->l->val += t->lazy;
    t->l->ans += t->lazy;
    t->l->lazy += t->lazy;
  }
  if (t->r != nullptr) {
    t->r->val += t->lazy;
    t->r->ans += t->lazy;
    t->r->lazy += t->lazy;
  }
  t->lazy = 0;
}

void split(pt t, pt &l, pt &r, int key) {
  push(t);
  if (t == nullptr) {
    l = r = nullptr;
  } else if (t->key <= key) {
    split(t->r, t->r, r, key), l = t;
  } else {
    split(t->l, l, t->l, key), r = t;
  }
  upd(t);
}

void merge(pt &t, pt l, pt r) {
  push(l), push(r);
  if (l == nullptr || r == nullptr) {
    t = l == nullptr ? r : l;
  } else if (l->prior > r->prior) {
    merge(l->r, l->r, r), t = l;
  } else {
    merge(r->l, l, r->l), t = r;
  }
  upd(t);
}

void tour(pt t) {
  if (t == nullptr) {
    return;
  }
  tour(t->l);
  cout << "Node: " << t->val << " " << t->ans << "\n";
  tour(t->r);
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int N, M;
  cin >> N >> M;

  vector<pair<int, int>> seg;
  for (int i = 0; i < N; i++) {
    int l, r;
    cin >> l >> r;
    seg.emplace_back(l, r);
  }

  auto brute = [&]() {
    int res = 0;
    for (int mask = 0; mask < (1 << N); mask++) {
      int l = 0, r = 0, cnt = 0;
      for (int i = 0; i < N; i++) {
        if (mask & (1 << i)) {
          cnt++;
          l = max(l, seg[i].first);
          r = max(r, M + 1 - seg[i].second);
        }
      }
      res = max(res, cnt - min(l + r, M));
    }
    return res;
  };

  // cout << "Brute " << brute() << "\n";

  sort(begin(seg), end(seg), [&](pair<int, int> &a, pair<int, int> &b) {
    return a.second > b.second;
  });

  int res = 0; // maximum difference
  multiset<int> L;
  
  pt treap = nullptr;


  for (int i = 0; i < N; i++) {
    int r = M + 1 - seg[i].second;
    // int cnt = 0;

    // L.emplace(seg[i].first);

    pt tl, tr, n;
    split(treap, tl, tr, seg[i].first);
    n = new tnode(sz(tl) + 1 - seg[i].first, seg[i].first);

    if (tr != nullptr) {
      tr->ans += 1;
      tr->val += 1;
      tr->lazy += 1;
    }
    // cout << "INSERT " << seg[i].first << "\n";
    merge(treap, tl, n);
    merge(treap, treap, tr);

    res = max(res, ans(treap) - r);
    // cout << seg[i].first << " " << ans(treap) << " " << r << "\n";
    // cout << ans(treap) << "\n";
    // for (auto &l : L) {
      // cnt++;
      // res = max(res, max(cnt - l - r, cnt - M));
    // }
    // cout << "TOUR\n";
    // tour(treap);
  }

  res = max(res, N - M);
  cout << res << "\n";
  return 0;
}


Measured memory_bytes: 11248