#include <bits/stdc++.h>
using namespace std;
const long double PI = acos(-1.0l);
const long long MOD = 1e9 + 7;
bool debug = 0;
const int N = 1e5 + 10;
int pre[N];
vector<int> tot;
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    pre[l]++;
    pre[r + 1]--;
  }
  int cur = 0;
  for (int i = 1; i <= m; i++) {
    cur += pre[i];
    tot.push_back(cur);
  }
  map<int, int> mi, mid;
  int ans = 0;
  for (int i = 0; i < m; i++) {
    {
      if (mi.size() and (--mi.end())->first >= tot[i]) {
        auto it = mid.lower_bound(tot[i]);
        int cur = 0;
        if (it != mid.end()) cur = it->second;
        mid[tot[i]] = max(cur + 1, (--mi.end())->second + 1);
        cur = mid[tot[i]];
        for (auto it = mid.find(tot[i]); it != mid.end();) {
          if (it == mid.begin()) break;
          it--;
          if (it->second > cur) break;
          it = mid.erase(it);
        }
      }
    }
    {
      int cur = 0;
      auto it = mi.lower_bound(tot[i]);
      if (it != mi.begin()) {
        it--;
        cur = it->second;
      }
      mi[tot[i]] = max(mi[tot[i]] + 1, cur + 1);
      cur = mi[tot[i]];
      for (auto it = ++mi.find(tot[i]); it != mi.end();) {
        if (it->second > mi[tot[i]]) break;
        it = mi.erase(it);
      }
    }
  }
  if (debug)
    cout << "mid.begin()->se"
         << " = " << (mid.begin()->second) << endl;
  if (debug)
    cout << "(--mi.end())->se"
         << " = " << ((--mi.end())->second) << endl;
  ans = max({ans, mid.begin()->second, (--mi.end())->second});
  reverse(tot.begin(), tot.end());
  mi.clear();
  mid.clear();
  for (int i = 0; i < m; i++) {
    {
      if (mi.size() and (--mi.end())->first >= tot[i]) {
        auto it = mid.lower_bound(tot[i]);
        int cur = 0;
        if (it != mid.end()) cur = it->second;
        mid[tot[i]] = max(cur + 1, (--mi.end())->second + 1);
        cur = mid[tot[i]];
        for (auto it = mid.find(tot[i]); it != mid.end();) {
          if (it == mid.begin()) break;
          it--;
          if (it->second > cur) break;
          it = mid.erase(it);
        }
      }
    }
    {
      int cur = 0;
      auto it = mi.lower_bound(tot[i]);
      if (it != mi.begin()) {
        it--;
        cur = it->second;
      }
      mi[tot[i]] = max(mi[tot[i]] + 1, cur + 1);
      cur = mi[tot[i]];
      for (auto it = ++mi.find(tot[i]); it != mi.end();) {
        if (it->second > mi[tot[i]]) break;
        it = mi.erase(it);
      }
    }
  }
  if (debug)
    cout << "mid.begin()->se"
         << " = " << (mid.begin()->second) << endl;
  if (debug)
    cout << "(--mi.end())->se"
         << " = " << ((--mi.end())->second) << endl;
  ans = max({ans, mid.begin()->second, (--mi.end())->second});
  cout << ans << endl;
  return 0;
}
