#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
//---------------------------------------------------------------------------------------------------
template<class V, int NV> struct LazySegTree { // [L,R)
    vector<V> dat, lazy; LazySegTree() { dat.resize(NV * 2, def); lazy.resize(NV * 2, ldef); }
    void update(int a, int b, V v, int k, int l, int r) {
        push(k, l, r); if (r <= a || b <= l) return;
        if (a <= l && r <= b) { setLazy(k, v); push(k, l, r); }
        else {
            update(a, b, v, k * 2 + 1, l, (l + r) / 2); update(a, b, v, k * 2 + 2, (l + r) / 2, r);
            dat[k] = comp(dat[k * 2 + 1], dat[k * 2 + 2]);
        }
    }
    V get(int a, int b, int k, int l, int r) {
        push(k, l, r); if (r <= a || b <= l) return def;
        if (a <= l && r <= b) return dat[k]; auto x = get(a, b, k * 2 + 1, l, (l + r) / 2);
        auto y = get(a, b, k * 2 + 2, (l + r) / 2, r); return comp(x, y);
    }
    void update(int a, int b, V v) { update(a, b, v, 0, 0, NV); }
    V get(int a, int b) { return get(a, b, 0, 0, NV); }
    // ---- Template ---------------------------------------------------------------------------------
    const V def = 0, ldef = 0;
    V comp(V l, V r) { return max(l, r); }
    void setLazy(int i, V v) { lazy[i] += v; }
    void push(int k, int l, int r) {
        if (lazy[k] != ldef) {
            // modify------------------------------
            dat[k] += lazy[k];
            // ------------------------------------
            if (r - l > 1) { setLazy(k * 2 + 1, lazy[k]); setLazy(k * 2 + 2, lazy[k]); }
            lazy[k] = ldef;
        }
    }
};
/*---------------------------------------------------------------------------------------------------
　　　　　　　　　　　 ∧＿∧  
　　　　　 ∧＿∧ 　（´<_｀ ）　 Welcome to My Coding Space!
　　　　 （ ´_ゝ`）　/　 ⌒i     
　　　　／　　　＼　 　  |　|     
　　　 /　　 /￣￣￣￣/　　|  
　 ＿_(__ﾆつ/　    ＿/ .| .|＿＿＿＿  
　 　　　＼/＿＿＿＿/　（u　⊃  
---------------------------------------------------------------------------------------------------*/



int N, M, L[201010], R[201010];
//---------------------------------------------------------------------------------------------------
int solveO2N() { // O(2^N)
    int ans = 0;
    rep(mask, 1, 1 << N) {
        int LL = -1, RR = M + 2, human = 0;
        rep(i, 0, N) if (mask & (1 << i)) {
            LL = max(LL, L[i]);
            RR = min(RR, R[i]);
            human++;
        }

        int chair = -1;
        if (RR <= LL) chair = M;
        else chair = LL + (M + 1 - RR);

        ans = max(ans, human - chair);
    }
    return ans;
}
//---------------------------------------------------------------------------------------------------
int solveOM2N() { // O(M^2N)
    int ans = max(0, N - M);
    rep(LL, 0, M + 2) rep(RR, LL + 1, M + 2) {
        int chair = LL + M + 1 - RR;
        int human = 0;
        rep(i, 0, N) if (L[i] <= LL && RR <= R[i]) human++;

        ans = max(ans, human - chair);
    }
    return ans;
}
//---------------------------------------------------------------------------------------------------
pair<int, int> LR[201010];
LazySegTree<int,1<<18> st;
int solveONlogM() { // O(NlogM)
    rep(i, 0, N) LR[i] = { L[i], R[i] };
    sort(LR, LR + N);

    rep(i, 1, M + 2) st.update(i, i + 1, i);

    int ans = max(0, N - M);
    rep(i, 0, N) {
        st.update(0, LR[i].second + 1, 1);

        // human - (LL + M + 1 - RR) = human + RR - (LL + M + 1)
        ans = max(ans, st.get(0, M + 2) - (LR[i].first + M + 1));
    }
    return ans;
}
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> N >> M;
    rep(i, 0, N) cin >> L[i] >> R[i];
    
    cout << solveONlogM() << endl;
}

Measured memory_bytes: 7552