#include <bits/stdc++.h>
#pragma GCC optimize("O2,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,ssse3")
using namespace std;
using ll = long long;
using ld = long double;
const long long maxn = 1 << 20, mod = 1e9 + 7;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
struct node {
  long long sum, lazy;
  node() {
    sum = -(1 << 30);
    lazy = 0;
  }
  node(long long val) {
    sum = val;
    lazy = 0;
  }
};
node temp, broken;
node merge(node l, node r) {
  temp.sum = max(l.sum, r.sum);
  temp.lazy = 0;
  return temp;
}
struct segment_tree {
  node tr[4 * (1 << 18)];
  void push(long long l, long long r, long long idx) {
    if (tr[idx].lazy) {
      tr[idx].sum += tr[idx].lazy;
      if (l != r) {
        tr[2 * idx + 1].lazy += tr[idx].lazy;
        tr[2 * idx + 2].lazy += tr[idx].lazy;
      }
      tr[idx].lazy = 0;
    }
  }
  void init(long long l, long long r, long long idx) {
    if (l == r) {
      tr[idx] = -l;
      return;
    }
    long long mid = (l + r) >> 1;
    init(l, mid, 2 * idx + 1);
    init(mid + 1, r, 2 * idx + 2);
    tr[idx] = merge(tr[2 * idx + 1], tr[2 * idx + 2]);
  }
  void update(long long qL, long long qR, long long val, long long l,
              long long r, long long idx) {
    push(l, r, idx);
    if (qL > r || l > qR) return;
    if (qL <= l && r <= qR) {
      tr[idx].lazy += val;
      push(l, r, idx);
      return;
    }
    long long mid = (l + r) >> 1;
    update(qL, qR, val, l, mid, 2 * idx + 1);
    update(qL, qR, val, mid + 1, r, 2 * idx + 2);
    tr[idx] = merge(tr[2 * idx + 1], tr[2 * idx + 2]);
  }
  node query(long long qL, long long qR, long long l, long long r,
             long long idx) {
    push(l, r, idx);
    if (l > qR || r < qL) return broken;
    if (qL <= l && r <= qR) return tr[idx];
    long long mid = (l + r) >> 1;
    return merge(query(qL, qR, l, mid, 2 * idx + 1),
                 query(qL, qR, mid + 1, r, 2 * idx + 2));
  }
};
long long a[maxn], n, m;
vector<long long> cl[maxn];
long long op[maxn];
signed main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> n >> m;
  for (long long l, r, i = 0; i < n; i++) {
    cin >> l >> r;
    cl[r].push_back(l);
    op[l]++;
  }
  segment_tree s;
  s.init(0, m + 1, 1);
  long long curOP = 0, duh = 0;
  for (long long i = 0; i <= m + 1; i++) {
    curOP += op[i];
    if (i) {
      s.update(0, i - 1, -op[i], 0, m + 1, 1);
    }
    duh = max((ll)duh, curOP + s.query(0, i, 0, m + 1, 1).sum - (m + 1 - i));
    for (auto &j : cl[i]) {
      if (j) s.update(0, j - 1, 1, 0, m + 1, 1);
      curOP--;
    }
  }
  cout << duh << endl;
}
