#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
//const ll mod = 1000000007;
struct LazySegmentTree {
private:
    int n;
    vector<ll> node, lazy;

public:
    LazySegmentTree() {
        int sz = 300010;
        n = 1; while(n < sz) n *= 2;
        node.resize(2*n-1, 0);
        lazy.resize(2*n-1, 0);

        for(int i=0; i<sz; i++) node[i+n-1] = -1e9;
        for(int i=n-2; i>=0; i--) node[i] = max(node[i*2+1], node[i*2+2]);
    }
    void eval(int k, int l, int r) {
        if(lazy[k] != 0) {
            node[k] += lazy[k];
            if(r - l > 1) {
                lazy[2*k+1] += lazy[k];
                lazy[2*k+2] += lazy[k];
            }
            lazy[k] = 0;
        }
    }

    void add(int a, int b, ll x, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;
        eval(k, l, r);
        if(b <= l || r <= a) return;
        if(a <= l && r <= b) {
            lazy[k] += x;
            eval(k, l, r);
        }
        else {
            add(a, b, x, 2*k+1, l, (l+r)/2);
            add(a, b, x, 2*k+2, (l+r)/2, r);
            node[k] = max(node[2*k+1], node[2*k+2]);
        }
    }

    ll getmax(int a, int b, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;
        eval(k, l, r);

        if(b <= l || r <= a) return -1e9;
        if(a <= l && r <= b) return node[k];
        ll vl = getmax(a, b, 2*k+1, l, (l+r)/2);
        ll vr = getmax(a, b, 2*k+2, (l+r)/2, r);
        return max(vl, vr);
    }

};

ll N, M;
ll L[305000], R[305000];
vector<ll> v[300000];
ll ans = 0;
ll Add[305000];

LazySegmentTree seg;

void print() {
    return;
    for(int i = 0; i <= M + 1; i++) {
        cerr << seg.getmax(i, i + 1) << " ";
    }
    cerr << endl;
}

int solve() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    for(int i = 0; i < N; i++) {
        v[L[i]].push_back(i);
    }
    seg = LazySegmentTree();
    print();
    seg.add(0, M + 2, 1e9);
    print();
    for(int i = M; i >= 0; i--) {
        seg.add(i, i + 1, -(M + 1 - i));
    }
    seg.add(0, 1, 1);
    print();
    for(int i = 0; i <= M + 1; i++) {
        //seg.add(0, v[i].first, -1e9);
        for(auto person : v[i]) {
            seg.add(0, R[person] + 1, 1);
            Add[R[person] + 1]++;
        }
        seg.add(i, i + 1, Add[i]);
        Add[i+1] += Add[i];
        print();
        ll maxi = seg.getmax(0, M + 2);
        ll now = - i + maxi;
        if(i == M + 1) now++;
        //cerr << now << endl;
        chmax(ans, now);
    }
    //cout << ans << endl;
    return 0;
}

int main() {
    cin >> N >> M;
    for(int i = 0; i < N; i++) {
        cin >> L[i] >> R[i];
    }
    solve();
    /*
    for(int i = 0; i < N; i++) {
        ll tmp = M + 1 - L[i];
        L[i] = M + 1 - R[i];
        R[i] = tmp;
    }
    solve();
    //*/
    cout << ans << endl;
}

Measured memory_bytes: 48384