#include <bits/stdc++.h>
using namespace std;
//make_tuple emplace_back next_permutation push_back make_pair second first setprecision

#if MYDEBUG
#include "lib/cp_debug.h"
#else
#define DBG(...) ;
#endif

using LL = long long;
constexpr LL LINF=334ll<<53;
constexpr int INF=15<<26;
constexpr LL  MOD=1E9+7;

struct RangeMax{
    using T = long long;
    T operator()(const T &a, const T &b){return max(a,b);}
    static constexpr T id(){return T(-LINF);};
};

struct RangeAdd{
    using T = long long;
    T operator()(const T &a, const T &b){return a+b;};
    static constexpr T id(){return T(0);};
};

struct Max_Add{
    using V = RangeMax;
    using A = RangeAdd;
    V::T operator()(const V::T &a, const A::T &b, const int &h){return a+b;};
};

template<typename M>
struct LazySegmentTree{
    using Val = typename M::V::T;
    using Del = typename M::A::T;
    typename M::A composite;
    typename M::V calc;
    M act;
    vector<Val> val;
    vector<Del> delay;

    int n,height;
    explicit LazySegmentTree(int size) : n(1),height(0){
        while(n<size){n<<=1;++height;}
        val=vector<Val>(n<<1,calc.id());
        delay=vector<Del>(n<<1,composite.id());
    }
    explicit LazySegmentTree(vector<Val> &v):n(1),height(0){
        while(n < v.size()) {n <<=1;++height;}
        val = vector<Val> (n<<1,calc.id());
        delay=vector<Del>(n<<1,composite.id());
        copy(v.begin(),v.end(),val.begin()+n);
        for(int i=n-1; i>0; --i){val[i]=calc(val[i*2],val[i*2+1]);}
    }
    void propagate(int k,int h){
        val[k]=act(val[k],delay[k],h);
        if(h>0){
            delay[2*k]=composite(delay[2*k], delay[k]);
            delay[2*k+1]=composite(delay[2*k+1], delay[k]);
        }
        delay[k]=composite.id();
    }
    void act_leaf(int k){
        val[k]=act(val[k],delay[k],0);
        delay[k]=composite.id();
    }
    inline void update(int l, int r, Del v){update(l,r,v,1,0,n,height);}
    void update(int l, int r, Del v, int k, int a, int b, int h){
        if(h==height)DBG(l,r,k,v)
        if(l<=a && b<=r){
            delay[k]=composite(delay[k],v);
            propagate(k,h);
            return;
        }
        propagate(k,h);
        if(b<=l || r<=a){return ;}//
        update(l,r,v,2*k,a,(a+b)/2,h-1);
        update(l,r,v,2*k+1,(a+b)/2,b,h-1);
        val[k]=calc(val[2*k],val[2*k+1]);
    }
    /*void update(int l, int r, Del v){
        l+=n;r+=n;
        int hl=__builtin_ctz(l),hr=__builtin_ctz(r),h=0;
        for(int i=height; i>hl; --i){propagate(l>>i,i);}
        for(int i=height,rr=r-1; i>hr; --i){propagate(rr>>i,i);}
        for(h=0;l<r; l>>=1, r>>=1,++h){
            if(l&1){
                delay[l]=composite(delay[l],v);
                //has_delay[l]=1;
                if(l<n)propagate(l,h),propagate(l^1,h);
                else act_leaf(l),act_leaf(l^1);
            }
            val[l>>1]=calc(val[(l>>1)<<1],val[((l>>1)<<1)|1]);
            ++l;
            if(r&1){
                delay[r-1]=composite(delay[r-1],v);
                //has_delay[r-1]=1;
                if(r-1<n)propagate(r-1,h),propagate((r-1)^1,h);
                else act_leaf(r-1),act_leaf((r-1)^1);
            }
            val[(r-1)>>1]=calc(val[((r-1)>>1)<<1],val[(((r-1)>>1)<<1)|1]);
        }
        for(--l;l>1;l>>=1,++h){
            propagate(l^1,h);
            val[l>>1]=calc(val[(l>>1)<<1],val[((l>>1)<<1)|1]);
        }
    }*/
    inline Val query(int l,int r){return query(l,r,1,0,n,height);}
    Val query(int l, int r, int k, int a, int b, int h){
        if(h==height)DBG(l,r)
        if(b<=l || r<=a){return calc.id();}//
        propagate(k,h);
        if(l<=a && b<=r){
            return val[k];
        }
        Val vall=query(l,r,2*k,a,(a+b)/2,h-1);
        Val valr=query(l,r,2*k+1,(a+b)/2,b,h-1);
        return calc(vall,valr);
    }
    /*Val query(int l, int r){
        Val resl=calc.id(),resr=calc.id();
        l+=n;r+=n;
        int hl=__builtin_ctz(l),hr=__builtin_ctz(r);
        for(int h=height; h>hl; --h){propagate(l>>h,h);}
        for(int h=height,rr=r-1; h>hr; --h){propagate(rr>>h,h);}
        if(l&1){
            act_leaf(l);
            resl=calc(resl,val[l++]);
        }
        if(r&1){
            act_leaf(r-1);
            resr=calc(val[r-1],resr);
        }
        l>>=1;r>>=1;
        for(int h=1;l<r; l>>=1, r>>=1,++h){
            if(l&1){
                propagate(l,h);
                resl=calc(resl,val[l++]);
            }
            if(r&1){
                propagate(r-1,h);
                resr=calc(val[r-1],resr);
            }
        }
        return calc(resl,resr);
    }*/
};

struct Problem{
    int n,m;
    vector<pair<int,int>> lr;
    Problem(LL n, LL m):n(n),m(m){};

    void solve(){
        for(int i=0; i<n; ++i){
            int a,b;
            cin >> a >>b;
            lr.emplace_back(a,b);
        }
        sort(lr.begin(),lr.end());
        vector<LL> tmp(m+2);
        for(int i=1; i<=m; ++i){
            tmp[i]=i-m-1;
        }
        LazySegmentTree<Max_Add> seg(tmp);
        LL ans =0;
        for(int i=0,p=0; i<m; ++i){
            if(i>0)seg.update(i+2,m+2,-1);
            while(p<n and lr[p].first<=i){
                seg.update(i,lr[p].second+1,1);
                ++p;
            }
        DBG(seg.val,seg.delay)
        DBG(i,m,seg.query(i+2,m+2))
            ans=max(ans,seg.query(i+2,m+2));
        }
        ans=max<LL>(n-m,ans);
        cout <<ans <<"\n";
    }
};

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    long long n=0,m;
    cin >> n >> m;
    Problem p(n,m);
    p.solve();
    return 0;
}



Measured memory_bytes: 11760