#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <string>
#include <string.h>
#include <algorithm>
#include <ctime>
#include <map>

using namespace std;

typedef long long lint;

int main() {

    ios_base::sync_with_stdio(false);

    int n, m; cin >> n >> m;
    vector<int> pl(m+1, 0);
    for(int i = 0; i < n; i++){
        int a, b;
        cin >> a >> b;
        pl[a]++; pl[b+1]--;
    }
    int it = 0;
    vector<int> much(m+1, 0);
    for(int i = 0; i <= m; i++){
        it += pl[i];
        much[i] = it;
    }

    vector<int> len(m+1, 1); len[0] = 0;
    vector<int> seq;
    seq.push_back(much[1]);
    for(int i = 2; i <= m; i++){
        if(much[i] >= seq.back()){
            seq.push_back(much[i]);
        } else {
            int l = -1, r = seq.size()-1;
            while(r - 1 > l){
                int mid = l + (r-l)/2;

                if(seq[mid] <= much[i]) l = mid;
                else r = mid;
            }
            seq[r] = much[i];
        }
        len[i] = seq.size();
    }
    reverse(much.begin(), much.end());
    vector<int> len2(m+1, 1);
    seq = vector<int>();
    seq.push_back(much[0]);
    for(int i = 1; i < m; i++){
        if(much[i] >= seq.back()){
            seq.push_back(much[i]);
        } else {
            int l = -1, r = seq.size()-1;
            while(r - 1 > l){
                int mid = l + (r-l)/2;

                if(seq[mid] <= much[i]) l = mid;
                else r = mid;
            }
            seq[r] = much[i];
        }
        len2[i+1] = seq.size();
    }

    int result = 0;
    for(int i = 0; i <= m; i++){
        result = max(result, len[i] + len2[m-i]);
    }
    cout << result <<endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <string>
#include <string.h>
#include <algorithm>
#include <ctime>
#include <map>

using namespace std;

typedef long long lint;

int main() {

    ios_base::sync_with_stdio(false);

    int n, m; cin >> n >> m;
    vector<int> pl(m+1, 0);
    for(int i = 0; i < n; i++){
        int a, b;
        cin >> a >> b;
        pl[a]++; pl[b+1]--;
    }
    int it = 0;
    vector<int> much(m+1, 0);
    for(int i = 0; i <= m; i++){
        it += pl[i];
        much[i] = it;
    }

    vector<int> len(m+1, 1); len[0] = 0;
    vector<int> seq;
    seq.push_back(much[1]);
    for(int i = 2; i <= m; i++){
        if(much[i] >= seq.back()){
            seq.push_back(much[i]);
        } else {
            int l = -1, r = seq.size()-1;
            while(r - 1 > l){
                int mid = l + (r-l)/2;

                if(seq[mid] <= much[i]) l = mid;
                else r = mid;
            }
            seq[r] = much[i];
        }
        len[i] = seq.size();
    }
    reverse(much.begin(), much.end());
    vector<int> len2(m+1, 1); len2[0] = 0;
    seq = vector<int>();
    seq.push_back(much[0]);
    for(int i = 1; i < m; i++){
        if(much[i] >= seq.back()){
            seq.push_back(much[i]);
        } else {
            int l = -1, r = seq.size()-1;
            while(r - 1 > l){
                int mid = l + (r-l)/2;

                if(seq[mid] <= much[i]) l = mid;
                else r = mid;
            }
            seq[r] = much[i];
        }
        len2[i+1] = seq.size();
    }
    int result = 0;
    for(int i = 0; i <= m; i++){
        result = max(result, len[i] + len2[m-i]);
    }
    cout << result <<endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <string>
#include <string.h>
#include <algorithm>
#include <ctime>
#include <map>

using namespace std;

typedef long long lint;

int main() {

    ios_base::sync_with_stdio(false);

    int n, m; cin >> n >> m;
    vector<int> pl(m+2, 0);
    for(int i = 0; i < n; i++){
        int a, b;
        cin >> a >> b;
        pl[a]++; pl[b+1]--;
    }
    int it = 0;
    vector<int> much(m+1, 0);
    for(int i = 0; i <= m; i++){
        it += pl[i];
        much[i] = it;
    }

    vector<int> len(m+1, 1); len[0] = 0;
    vector<int> seq;
    seq.push_back(much[1]);
    for(int i = 2; i <= m; i++){
        if(much[i] >= seq.back()){
            seq.push_back(much[i]);
        } else {
            int l = -1, r = seq.size()-1;
            while(r - 1 > l){
                int mid = l + (r-l)/2;

                if(seq[mid] <= much[i]) l = mid;
                else r = mid;
            }
            seq[r] = much[i];
        }
        len[i] = seq.size();
    }
    reverse(much.begin(), much.end());
    vector<int> len2(m+1, 1); len2[0] = 0;
    seq = vector<int>();
    seq.push_back(much[0]);
    for(int i = 1; i < m; i++){
        if(much[i] >= seq.back()){
            seq.push_back(much[i]);
        } else {
            int l = -1, r = seq.size()-1;
            while(r - 1 > l){
                int mid = l + (r-l)/2;

                if(seq[mid] <= much[i]) l = mid;
                else r = mid;
            }
            seq[r] = much[i];
        }
        len2[i+1] = seq.size();
    }

    int result = 0;
    for(int i = 0; i <= m; i++){
        result = max(result, len[i] + len2[m-i]);
    }
    cout << result <<endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <bitset>
#include <sstream>
#include <cstdlib>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <climits>
#include <cstdio>
#include <ctype.h>
#include <time.h>
using namespace std;

#define PI acos(-1.0)
#define EPS 1e-8
#define MOD 1000000007
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
#define go(i, a, b) for(int i = a; i <= b; ++i)
#define gorev(i, a, b) for(int i = a; i >= b; --i)

typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;

template <class L, class R> ostream &operator <<(ostream &os, pair<L, R> P){
    return os << "(" << P.fi << "," << P.se << ")";
}
template <class T> ostream &operator <<(ostream &os, vector<T> V){
    os << "["; for (auto vv: V) os << vv << ","; return os << "]";
}

struct Matrix {
    vvi data;
    int r, c;
    Matrix(int row, int col, bool identity = false) : r(row), c(col) {
        data.assign(row, vector<int>(col, 0));
        if (identity) {
            for (int i = 0; i < r; ++i) {
                data[i][i] = 1;
            }
        }
    }
    Matrix operator * (Matrix& other) {
        int m = r, n = c, p = other.c;
        Matrix res(m, p);
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < p; ++j) {
                for (int k = 0; k < n; ++k) {
                    res.data[i][j] += data[i][k] * other.data[k][j];
                }
            }
        }
        return res;
    }
};

// 扩展欧几里得算法 res[1] * a + res[2] * b = res[0] = gcd(a, b)
vector<int> extendGcd(int a, int b) {
    if (b == 0) {
        return {a, 1, 0};
    } else {
        vector<int> tmp = extendGcd(b, a % b);
        return {tmp[0], tmp[2], tmp[1] - (a / b) * tmp[2]};
    }
}

// 矩阵快速幂
Matrix matrix_power(Matrix base, ll exp) {
    int n = base.r;
    Matrix res(n, n, true);
    while (exp) {
        if (exp & 1) {
            res = res * base;
        }
        base = base * base;
        exp >>= 1;
    }
    return res;
}

/******************************** template ********************************/
#define N 100010
int n, m;
int cnt[N];
//key:xi value:positions of corresponding points
unordered_map<int, vi> mm;
//amount of smaller element in the left/right
int l[N], r[N];
vi cur;
int dp[N];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifdef ZYC_LOCAL
    freopen("input.txt", "rt", stdin);
#endif
    cin >> n >> m;
    int L, R;
    go(i, 1, n) {
        cin >> L >> R;
        cnt[L]++;
        cnt[R + 1]--;
    }
    go(i, 1, m) {
        cnt[i] += cnt[i - 1];
        mm[cnt[i]].pb(i);
    }

    go(i, 1, m) {
        auto it = upper_bound(all(cur), cnt[i]);
        l[i] = it - cur.begin();
        cur.insert(it, cnt[i]);
    }

    cur.clear();

    gorev(i, m, 1) {
        auto it = upper_bound(all(cur), cnt[i]);
        r[i] = it - cur.begin();
        cur.insert(it, cnt[i]);
    }

    go(i, 0, n) {
        if (mm.find(i) == mm.end()) continue;
        dp[i] = l[*mm[i].begin()];
    }

    int res = 0;
    go(i, 1, m) {
        res = max(res, dp[cnt[i]] + r[i] + (int)(lower_bound(all(mm[cnt[i]]), i) - mm[cnt[i]].begin()) + 1);
        dp[cnt[i]] = max(dp[cnt[i]], l[i] - (int)(lower_bound(all(mm[cnt[i]]), i) - mm[cnt[i]].begin()));
    }
    cout << res;

#ifdef ZYC_LOCAL
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
    return 0;
}


#include <iostream>
#include <algorithm>
#include <climits>
using namespace std;
const int MAXN = 100000 + 10;
int nums[MAXN], dp[2][MAXN], ret[2][MAXN];
int main() {
//    freopen("test.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n, m, a, b;
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        cin >> a >> b;
        nums[a]++;
        nums[b+1]--;
    }
    for (int i = 1; i <= m; i++) {
        nums[i] += nums[i-1];
        dp[0][i] = dp[1][i] = INT_MAX;
    }
    for (int i = 1; i <= m; i++) {
        *upper_bound(dp[0]+1, dp[0]+m+1, nums[i]) = nums[i];
        ret[0][i] = lower_bound(dp[0]+1, dp[0]+i+1, INT_MAX) - dp[0] - 1;
    }
    reverse(nums+1, nums+m+1);
    for (int i = 1; i <= m; i++) {
        *upper_bound(dp[1]+1, dp[1]+m+1, nums[i]) = nums[i];
        ret[1][i] = lower_bound(dp[1]+1, dp[1]+i+1, INT_MAX) - dp[1] - 1;
    }
    int ma = 0;
    for (int i = 1; i <= m; i++) {
        ma = max(ma, ret[0][i]+ret[1][m-i+1] - 1);
    }
    cout << ma << endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <bitset>
#include <sstream>
#include <cstdlib>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <climits>
#include <cstdio>
#include <ctype.h>
#include <time.h>
using namespace std;

#define PI acos(-1.0)
#define EPS 1e-8
#define MOD 1000000007
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
#define go(i, a, b) for(int i = a; i <= b; ++i)
#define gorev(i, a, b) for(int i = a; i >= b; --i)

typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;

template <class L, class R> ostream &operator <<(ostream &os, pair<L, R> P){
    return os << "(" << P.fi << "," << P.se << ")";
}
template <class T> ostream &operator <<(ostream &os, vector<T> V){
    os << "["; for (auto vv: V) os << vv << ","; return os << "]";
}

struct Matrix {
    vvi data;
    int r, c;
    Matrix(int row, int col, bool identity = false) : r(row), c(col) {
        data.assign(row, vector<int>(col, 0));
        if (identity) {
            for (int i = 0; i < r; ++i) {
                data[i][i] = 1;
            }
        }
    }
    Matrix operator * (Matrix& other) {
        int m = r, n = c, p = other.c;
        Matrix res(m, p);
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < p; ++j) {
                for (int k = 0; k < n; ++k) {
                    res.data[i][j] += data[i][k] * other.data[k][j];
                }
            }
        }
        return res;
    }
};

// 扩展欧几里得算法 res[1] * a + res[2] * b = res[0] = gcd(a, b)
vector<int> extendGcd(int a, int b) {
    if (b == 0) {
        return {a, 1, 0};
    } else {
        vector<int> tmp = extendGcd(b, a % b);
        return {tmp[0], tmp[2], tmp[1] - (a / b) * tmp[2]};
    }
}

// 矩阵快速幂
Matrix matrix_power(Matrix base, ll exp) {
    int n = base.r;
    Matrix res(n, n, true);
    while (exp) {
        if (exp & 1) {
            res = res * base;
        }
        base = base * base;
        exp >>= 1;
    }
    return res;
}

/******************************** template ********************************/
#define N 100010
int n, m;
int cnt[N];
//key:xi value:positions of corresponding points
unordered_map<int, vi> mm;
//amount of smaller element in the left/right
int l[N], r[N];
vi cur;
int dp[N];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
//    freopen("test.txt", "r", stdin);
#ifdef ZYC_LOCAL
    freopen("input.txt", "rt", stdin);
#endif
    cin >> n >> m;
    int L, R;
    go(i, 1, n) {
        cin >> L >> R;
        cnt[L]++;
        cnt[R + 1]--;
    }
    go(i, 1, m) {
        cnt[i] += cnt[i - 1];
        mm[cnt[i]].pb(i);
    }

    go(i, 1, m) {
        auto it = upper_bound(all(cur), cnt[i]);
        l[i] = it - cur.begin();
        if (it == cur.end()) cur.insert(it, cnt[i]);
        else *it = cnt[i];
    }

    cur.clear();

    gorev(i, m, 1) {
        auto it = upper_bound(all(cur), cnt[i]);
        r[i] = it - cur.begin();
        if (it == cur.end()) cur.insert(it, cnt[i]);
        else *it = cnt[i];
    }

    go(i, 0, n) {
        if (mm.find(i) == mm.end()) continue;
        dp[i] = l[*mm[i].begin()];
    }

    int res = 0;
    go(i, 1, m) {
        res = max(res, dp[cnt[i]] + r[i] + (int)(lower_bound(all(mm[cnt[i]]), i) - mm[cnt[i]].begin()) + 1);
        dp[cnt[i]] = max(dp[cnt[i]], l[i] - (int)(lower_bound(all(mm[cnt[i]]), i) - mm[cnt[i]].begin()));
    }
    cout << res;

#ifdef ZYC_LOCAL
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
    return 0;
}


/*input
4 6
1 3
2 3
4 6
4 6
*/
#include <bits/stdc++.h>
using namespace std;

int bs(int freq[],int low, int high, int x){

	if(low>high) return -1;
	int mid;
	while(low<high){
		mid = low + (high-1)/2;
		if(freq[mid]==x) break;
		if(x<freq[mid]) high=mid-1;
		return low=mid+1;
	}
	return freq[mid]==x?mid:0;
}

int freq[100005],n,enter[100005],m;
pair<int,int> a[100005];

// Refernce from https://www.geeksforgeeks.org/longest-bitonic-subsequence-onlogn/
long long solve(int n){
	int inc[n]={freq[0]}, tail1[n]={0},dec[n],tail2[n]={0},in=1;
	for(int i=1;i<n;++i)
		if(freq[i]<inc[0])
			inc[tail1[i]=0] = freq[i];
		else if(freq[i]>=inc[in-1])
			inc[tail1[i]=in++]=freq[i];
		else
			inc[tail1[i]=bs(inc,-1,in-1,freq[i])] = freq[i];

	in = 1;
	reverse(freq,freq+n);
	dec[0] = freq[0];

	for(int i=1;i<n;++i)
		if(freq[i]<dec[0])
			dec[tail2[i]=0] = freq[i];
		else if(freq[i]>=dec[in-1])
			dec[tail2[i]=in++]=freq[i];
		else
			dec[tail2[i]=bs(dec,-1,in-1,freq[i])] = freq[i];

	reverse(freq,freq+n);
	reverse(tail2,tail2+n);
	int ans = 0;
	for(int i=0;i<n;++i)
		ans = max(ans,tail1[i]+tail2[i]+1);
	return ans;
}
int main(){

	ios_base::sync_with_stdio(0);

	int ans = 0;
	cin>>n>>m;
	for(int i=0;i<n;++i){
		cin>>a[i].first>>a[i].second;
		++enter[a[i].first];
		--enter[a[i].second+1];
	}
	for(int i=0,run=0;i<=m;++i){
		freq[i] = (run+=enter[i]);
	}
	cout<<solve(m);
}


/*input
4 6
1 3
2 3
4 6
4 6
*/
#include <bits/stdc++.h>
using namespace std;
int freq[100005],n,enter[100005],m;
template <typename T>
class SegmentTree{
	vector<T> segtree;
	public:
	SegmentTree(int size){
		segtree.resize(4*size,0);
	}
	void update(int u, int a, int b, int i, int j, T x){
		if(j<a || i>b || a>b) return;
		if(j>=b && i<=a){
			segtree[u]=x;
			return;
		}
		update(u*2,a,(a+b)/2,i,j,x); update(u*2+1,(a+b)/2+1,b,i,j,x);
		segtree[u]=max(segtree[u*2],segtree[u*2+1]);
	}
	void update(int i, T x){
		update(1, 0, segtree.size()/4-1, i, i, x);
	}
	void update(int i, int j, T x){
		update(1, 0, segtree.size()/4-1, i, j, x);
	}
	T query(int u, int a, int b, int i, int j){
		if(j<a || i>b || a>b) return 0;
		if(j>=b && i<=a) return segtree[u];
		return max(query(u*2,a,(a+b)/2,i,j),query(u*2+1,1+(a+b)/2,b,i,j));
	}
	T query(int i, int j){
		return query(1,0,segtree.size()/4-1,i,j);
	}
};
int dpi[100005],dpd[100005];
int solve(int n){
	fill_n(dpi,n,0);
	fill_n(dpd,n,0);
	SegmentTree<int> inc(100005), dec(100005);

	for(int i=0;i<n;++i){
		inc.update(freq[i],inc.query(0,freq[i])+1);
		dpi[i] = inc.query(0,100004);
	}
	reverse(freq,freq+n);
	for(int i=0;i<n;++i){
		dec.update(freq[i],dec.query(0,freq[i])+1);
		dpd[i] = dec.query(0,100004);
	}
	reverse(dpd,dpd+n);
	int ans = 0;
	for(int i=0;i<n;++i){
		ans=max(ans,dpi[i]+dpd[i]-1);
	}
	return ans;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin>>n>>m;
	for(int i=n,x,y;i--;++enter[x],--enter[y+1])
		cin>>x>>y;
	for(int i=1,run=0;i<=m;++i)
		freq[i-1] = (run+=enter[i]);
	cout<<solve(m);
}


/*input
4 6
1 3
2 3
4 6
4 6
*/
#include <bits/stdc++.h>
using namespace std;
int freq[100005],n,enter[100005],m;
int solve(int n){
	int inc[n]={freq[0]}, tail1[n]={0},dec[n],tail2[n]={0},in=1;
	for(int i=1;i<n;++i)
		if(freq[i]<inc[0])
			inc[tail1[i]=0] = freq[i];
		else if(freq[i]>=inc[in-1])
			inc[tail1[i]=in++]=freq[i];
		else
			inc[tail1[i]=lower_bound(inc,inc+in,freq[i])-inc] = freq[i];

	in = 1;
	reverse(freq,freq+n);
	dec[0] = freq[0];

	for(int i=1;i<n;++i)
		if(freq[i]<dec[0])
			dec[tail2[i]=0] = freq[i];
		else if(freq[i]>=dec[in-1])
			dec[tail2[i]=in++]=freq[i];
		else
			dec[tail2[i]=lower_bound(dec,dec+in,freq[i])-dec] = freq[i];

	reverse(freq,freq+n);
	reverse(tail2,tail2+n);
	int ans = 0;
	for(int i=0;i<n;++i)
		ans = max(ans,tail1[i]+tail2[i]+1);
	return ans;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin>>n>>m;
	for(int i=n,x,y;i--;++enter[x],--enter[y+1])
		cin>>x>>y;
	for(int i=0,run=0;i<=m;++i)
		freq[i] = (run+=enter[i]);
	cout<<solve(m);
}


/*input
4 6
1 3
2 3
4 6
5 6
*/
#include <bits/stdc++.h>
using namespace std;

long long freq[100005],n,enter[100005];
pair<long long,long long> a[100005];
map<int,int> mp;
int main(){
	#ifdef ONLINE_JUDGE
	ios_base::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	#endif
	int m,ans = 0;
	cin>>n>>m;
	for(int i=0;i<n;++i){
		cin>>a[i].first>>a[i].second;
		++enter[a[i].first];
		--enter[a[i].second+1];
	}
	for(int i=0,run=0;i<=m;++i)
		freq[i] = (run+=enter[i]);
	for(int i=1;i<=m;++i) {
		++mp[freq[i]];
	}
	for(auto p:mp)
		ans = max(ans,1+p.second);
	cout<<min(m,ans);
}


/*input
4 6
1 3
2 3
4 6
4 6
*/
#include <bits/stdc++.h>
using namespace std;
int freq[100005],n,enter[100005],m;
int solve(int n){
	int inc[n]={freq[0]}, tail1[n]={0},dec[n],tail2[n]={0};
	for(int i=1,in=1;i<n;++i)
		if(freq[i]<inc[0])
			inc[tail1[i]=0] = freq[i];
		else if(freq[i]>=inc[in-1])
			inc[tail1[i]=in++]=freq[i];
		else
			inc[tail1[i]=upper_bound(inc,inc+in,freq[i])-inc-1] = freq[i];

	reverse(freq,freq+n);
	dec[0] = freq[0];

	for(int i=1,in=1;i<n;++i)
		if(freq[i]>dec[0])
			dec[tail2[i]=0] = freq[i];
		else if(freq[i]<=dec[in-1])
			dec[tail2[i]=in++]=freq[i];
		else
			dec[tail2[i]=upper_bound(dec,dec+in,freq[i])-dec-1] = freq[i];

	reverse(freq,freq+n);
	reverse(tail2,tail2+n);
	int ans = 0;
	for(int i=0;i<n;++i)
		ans = max(ans,tail1[i]+tail2[i]+1);
	return ans;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin>>n>>m;
	for(int i=n,x,y;i--;++enter[x],--enter[y+1])
		cin>>x>>y;
	for(int i=0,run=0;i<=m;++i)
		freq[i] = (run+=enter[i]);
	cout<<solve(m);
}


/*input
4 6
1 3
2 3
4 6
4 6
*/
#include <bits/stdc++.h>
using namespace std;
int freq[100005],n,enter[100005],m;
int solve(int n){
	int inc[n]={freq[0]}, tail1[n]={0},dec[n],tail2[n]={0};

	for(int i=1,in=1;i<n;++i)
		if(freq[i]<inc[0])
			inc[tail1[i]=0] = freq[i];
		else if(freq[i]>=inc[in-1])
			inc[tail1[i]=in++]=freq[i];
		else
			inc[tail1[i]=lower_bound(inc,inc+in,freq[i])-inc] = freq[i];

	reverse(freq,freq+n);
	dec[0] = freq[0];

	for(int i=1,in=1;i<n;++i)
		if(freq[i]<dec[0])
			dec[tail2[i]=0] = freq[i];
		else if(freq[i]>=dec[in-1])
			dec[tail2[i]=in++]=freq[i];
		else
			dec[tail2[i]=lower_bound(dec,dec+in,freq[i])-dec] = freq[i];

	reverse(freq,freq+n);
	reverse(tail2,tail2+n);
	int ans = 0;
	for(int i=0;i<n;++i)
		ans = max(ans,tail1[i]+tail2[i]+1);
	return ans;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin>>n>>m;
	for(int i=n,x,y;i--;++enter[x],--enter[y+1])
		cin>>x>>y;
	for(int i=1,run=0;i<=m;++i)
		freq[i-1] = (run+=enter[i]);
	cout<<solve(m);
}


/*input
4 6
1 3
2 3
4 6
4 6
*/
#include <bits/stdc++.h>
using namespace std;

int bs(int freq[],int low, int high, int x){

	if(low>high) return -1;
	int mid;
	while(low<high){
		mid = low + (high-1)/2;
		if(freq[mid]==x) break;
		if(x<freq[mid]) high=mid-1;
		return low=mid+1;
	}
	return freq[mid]==x?mid:-1;
}

int freq[100005],n,enter[100005],m;
pair<int,int> a[100005];

// Refernce from https://www.geeksforgeeks.org/longest-bitonic-subsequence-onlogn/
long long solve(int n){
	int inc[n]={freq[0]}, tail1[n]={0},dec[n],tail2[n]={0},in=1;
	for(int i=1;i<n;++i)
		if(freq[i]<inc[0])
			inc[tail1[i]=0] = freq[i];
		else if(freq[i]>=inc[in-1])
			inc[tail1[i]=in++]=freq[i];
		else
			inc[tail1[i]=bs(inc,-1,in-1,freq[i])] = freq[i];

	in = 1;
	reverse(freq,freq+n);
	dec[0] = freq[0];

	for(int i=1;i<n;++i)
		if(freq[i]<dec[0])
			dec[tail2[i]=0] = freq[i];
		else if(freq[i]>=dec[in-1])
			dec[tail2[i]=in++]=freq[i];
		else
			dec[tail2[i]=bs(dec,-1,in-1,freq[i])] = freq[i];

	reverse(freq,freq+n);
	reverse(tail2,tail2+n);
	int ans = 0;
	for(int i=0;i<n;++i)
		ans = max(ans,tail1[i]+tail2[i]+1);
	return ans;
}
int main(){

	ios_base::sync_with_stdio(0);

	int ans = 0;
	cin>>n>>m;
	for(int i=0;i<n;++i){
		cin>>a[i].first>>a[i].second;
		++enter[a[i].first];
		--enter[a[i].second+1];
	}
	for(int i=0,run=0;i<=m;++i){
		freq[i] = (run+=enter[i]);
	}
	cout<<solve(m);
}


/*input
4 6
1 3
2 3
4 6
4 6
*/
#include <bits/stdc++.h>
using namespace std;

int bs(int freq[],int low, int high, int x){

	if(low>high) return -1;
	int mid;
	while(low<high){
		mid = low + (high-1)/2;
		if(freq[mid]==x) break;
		if(x<freq[mid]) high=mid-1;
		return low=mid+1;
	}
	return mid;
}

int freq[100005],n,enter[100005],m;
pair<int,int> a[100005];

// Refernce from https://www.geeksforgeeks.org/longest-bitonic-subsequence-onlogn/
long long solve(int n){
	int inc[n]={freq[0]}, tail1[n]={0},dec[n],tail2[n]={0},in=1;
	for(int i=1;i<n;++i)
		if(freq[i]<inc[0])
			inc[tail1[i]=0] = freq[i];
		else if(freq[i]>=inc[in-1])
			inc[tail1[i]=in++]=freq[i];
		else
			inc[tail1[i]=bs(inc,-1,in-1,freq[i])] = freq[i];

	in = 1;
	reverse(freq,freq+n);
	dec[0] = freq[0];

	for(int i=1;i<n;++i)
		if(freq[i]<dec[0])
			dec[tail2[i]=0] = freq[i];
		else if(freq[i]>=dec[in-1])
			dec[tail2[i]=in++]=freq[i];
		else
			dec[tail2[i]=bs(dec,-1,in-1,freq[i])] = freq[i];

	reverse(freq,freq+n);
	reverse(tail2,tail2+n);
	int ans = 0;
	for(int i=0;i<n;++i)
		ans = max(ans,tail1[i]+tail2[i]+1);
	return ans;
}
int main(){

	ios_base::sync_with_stdio(0);

	int ans = 0;
	cin>>n>>m;
	for(int i=0;i<n;++i){
		cin>>a[i].first>>a[i].second;
		++enter[a[i].first];
		--enter[a[i].second+1];
	}
	for(int i=0,run=0;i<=m;++i){
		freq[i] = (run+=enter[i]);
	}
	cout<<solve(m);
}


/*input
4 6
1 3
2 3
4 6
4 6
*/
#include <bits/stdc++.h>
using namespace std;
int freq[100005],n,enter[100005],m;
int solve(int n){
	int inc[n]={freq[0]}, tail1[n]={0},dec[n],tail2[n]={0};

	for(int i=1,in=1;i<n;++i)
		if(freq[i]<inc[0])
			inc[tail1[i]=0] = freq[i];
		else if(freq[i]>=inc[in-1])
			inc[tail1[i]=in++]=freq[i];
		else
			inc[tail1[i]=upper_bound(inc,inc+in,freq[i])-inc-1] = freq[i];

	reverse(freq,freq+n);
	dec[0] = freq[0];

	for(int i=1,in=1;i<n;++i)
		if(freq[i]<dec[0])
			dec[tail2[i]=0] = freq[i];
		else if(freq[i]>=dec[in-1])
			dec[tail2[i]=in++]=freq[i];
		else
			dec[tail2[i]=upper_bound(dec,dec+in,freq[i])-dec-1] = freq[i];

	reverse(freq,freq+n);
	reverse(tail2,tail2+n);
	int ans = 0;
	for(int i=0;i<n;++i)
		ans = max(ans,tail1[i]+tail2[i]+1);
	return ans;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin>>n>>m;
	for(int i=n,x,y;i--;++enter[x],--enter[y+1])
		cin>>x>>y;
	for(int i=1,run=0;i<=m;++i)
		freq[i-1] = (run+=enter[i]);
	cout<<solve(m);
}


/*input
4 6
1 3
2 3
4 6
4 6
*/
#include <bits/stdc++.h>
using namespace std;
int freq[100005],n,enter[100005],m;
int solve(int n){
	int inc[n]={freq[0]}, tail1[n]={0},dec[n],tail2[n]={0},in=1;
	for(int i=1;i<n;++i)
		if(freq[i]<inc[0])
			inc[tail1[i]=0] = freq[i];
		else if(freq[i]>=inc[in-1])
			inc[tail1[i]=in++]=freq[i];
		else
			inc[tail1[i]=upper_bound(inc,inc+in,freq[i])-inc-1] = freq[i];

	in = 1;
	reverse(freq,freq+n);
	dec[0] = freq[0];

	for(int i=1;i<n;++i)
		if(freq[i]<dec[0])
			dec[tail2[i]=0] = freq[i];
		else if(freq[i]>=dec[in-1])
			dec[tail2[i]=in++]=freq[i];
		else
			dec[tail2[i]=upper_bound(dec,dec+in,freq[i])-dec-1] = freq[i];

	reverse(freq,freq+n);
	reverse(tail2,tail2+n);
	int ans = 0;
	for(int i=0;i<n;++i)
		ans = max(ans,tail1[i]+tail2[i]+1);
	return ans;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin>>n>>m;
	for(int i=n,x,y;i--;++enter[x],--enter[y+1])
		cin>>x>>y;
	for(int i=0,run=0;i<=m;++i)
		freq[i] = (run+=enter[i]);
	cout<<solve(m);
}


/*input
4 6
1 3
2 3
4 6
4 6
*/
#include <bits/stdc++.h>
using namespace std;

int bs(int freq[],int low, int high, int x){

	if(low>high) return -1;
	int mid = low + (high-1)/2;
	if(freq[mid]==x) return mid;
	if(x<freq[mid]) return bs(freq,low,mid-1,x);
	return bs(freq,mid+1,high,x);
}

int freq[100005],n,enter[100005],m;
pair<int,int> a[100005];

// Refernce from https://www.geeksforgeeks.org/longest-bitonic-subsequence-onlogn/
long long solve(int n){
	int inc[n]={freq[0]}, tail1[n]={0},dec[n],tail2[n]={0},in=1;
	for(int i=1;i<n;++i)
		if(freq[i]<inc[0])
			inc[tail1[i]=0] = freq[i];
		else if(freq[i]>=inc[in-1])
			inc[tail1[i]=in++]=freq[i];
		else
			inc[tail1[i]=bs(inc,-1,in-1,freq[i])] = freq[i];

	in = 1;
	reverse(freq,freq+n);
	dec[0] = freq[0];

	for(int i=1;i<n;++i)
		if(freq[i]<dec[0])
			dec[tail2[i]=0] = freq[i];
		else if(freq[i]>=dec[in-1])
			dec[tail2[i]=in++]=freq[i];
		else
			dec[tail2[i]=bs(dec,-1,in-1,freq[i])] = freq[i];

	reverse(freq,freq+n);
	reverse(tail2,tail2+n);
	int ans = 0;
	for(int i=0;i<n;++i)
		ans = max(ans,tail1[i]+tail2[i]+1);
	return ans;
}
int main(){

	ios_base::sync_with_stdio(0);

	int ans = 0;
	cin>>n>>m;
	for(int i=0;i<n;++i){
		cin>>a[i].first>>a[i].second;
		++enter[a[i].first];
		--enter[a[i].second+1];
	}
	for(int i=0,run=0;i<=m;++i){
		freq[i] = (run+=enter[i]);
	}
	cout<<solve(m);
}


#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <utility>
#include <string>

using namespace std;

const int INF=1000000000;

const int MAXN=101010;

int t[4*MAXN]={0};

int a[MAXN];

void upd(int v,int tl,int tr,int l,int r)
{
    if(l>r)
    {
        return;
    }
    if((tl==l)&&(tr==r))
    {
        t[v]+=1;
        return;
    }
    int mid=(tl+tr)/2;

    upd(2*v,tl,mid,l,min(mid,r));
    upd(2*v+1,mid+1,tr,max(mid+1,l),r);
    return;
}

void fin(int v,int tl,int tr,int val=0)
{
    val+=t[v];

    if(tl==tr)
    {
        a[tl]=val;
        return;
    }

    int mid=(tl+tr)/2;

    fin(2*v,tl,mid,val);
    fin(2*v+1,mid+1,tr,val);
    return;
}

int pref[101010];
int suff[101010];
int d[101010];

int main()
{
    cin.tie(NULL);
    ios::sync_with_stdio(false);

    int n,m,l,r;
    cin>>n>>m;

    for(int i=0;i<n;i++)
    {
        cin>>l>>r;
        upd(1,1,m,l,r);
    }

    fin(1,1,m);

    //end of tree

    d[0]=0;

    for(int i=1;i<=m;i++)
    {
        d[i]=INF;
    }

    for(int i=1;i<=m;i++)
    {
        int j=0;
        int q=m+1;

        while(q-j>1)
        {
            int mid=(q+j)/2;

            if(a[i]>=d[mid])
            {
                j=mid;
            }
            else
            {
                q=mid;
            }
        }

        pref[i]=j;

        if(a[i]>=d[j] && a[i]<d[j+1])
        {
            d[j+1]=a[i];
        }
    }

    d[0]=0;

    for(int i=1;i<=m;i++)
    {
        d[i]=INF;
    }

    int maxx=0;

    for(int i=m;i>0;i--)
    {
        int j=0;
        int q=m+1;

        while(q-j>1)
        {
            int mid=(q+j)/2;

            if(a[i]>=d[mid])
            {
                j=mid;
            }
            else
            {
                q=mid;
            }
            //cout<<j<<' '<<q<<endl;
        }

        //cout<<i<<' '<<pref[i]<<' '<<j+1<<endl;

        maxx=max(maxx,j+1+pref[i]);

        if(a[i]>=d[j] && a[i]<d[j+1])
        {
            d[j+1]=a[i];
        }
    }

    cout<<maxx<<'\n';

    return 0;
}


#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <utility>
#include <string>

using namespace std;

const int INF=1000000000;

const int MAXN=101010;

int t[4*MAXN]={0};

int a[MAXN];

void upd(int v,int tl,int tr,int l,int r)
{
    if(l>r)
    {
        return;
    }
    if((tl==l)&&(tr==r))
    {
        t[v]+=1;
        return;
    }
    int mid=(tl+tr)/2;

    upd(2*v,tl,mid,l,min(mid,r));
    upd(2*v+1,mid+1,tr,max(mid+1,l),r);
    return;
}

void fin(int v,int tl,int tr,int val=0)
{
    val+=t[v];

    if(tl==tr)
    {
        a[tl]=val;
        return;
    }

    int mid=(tl+tr)/2;

    fin(2*v,tl,mid,val);
    fin(2*v+1,mid+1,tr,val);
    return;
}

int pref[101010];
int suff[101010];
int d[101010];

int main()
{
    cin.tie(NULL);
    ios::sync_with_stdio(false);

    int n,m,l,r;
    cin>>n>>m;

    for(int i=0;i<n;i++)
    {
        cin>>l>>r;
        upd(1,1,m,l,r);
    }

    fin(1,1,m);

    //end of tree

    d[0]=0;

    for(int i=1;i<=m;i++)
    {
        d[i]=INF;
    }

    for(int i=1;i<=m;i++)
    {
        int j=0;
        int q=m+1;

        while(q-j>1)
        {
            int mid=(q+j)/2;

            if(a[i]>=d[mid])
            {
                j=mid;
            }
            else
            {
                q=mid;
            }
        }

        pref[i]=j+1;

        if(a[i]>=d[j] && a[i]<d[j+1])
        {
            d[j+1]=a[i];
        }
    }

    d[0]=0;

    for(int i=0;i<=m;i++)
    {
        d[i]=INF;
    }

    int maxx=0;

    for(int i=m;i>0;i--)
    {
        int j=0;
        int q=m+1;

        while(q-j>1)
        {
            int mid=(q+j)/2;
            if(a[i]>=d[mid])
            {
                j=mid;
            }
            else
            {
                q=mid;
            }
        }

        //cout<<i<<' '<<pref[i]<<' '<<j+1<<endl;

        maxx=max(maxx,j+1+pref[i]-1);

        if(a[i]>=d[j] && a[i]<d[j+1])
        {
            d[j+1]=a[i];
        }
    }

    cout<<maxx<<'\n';

    return 0;
}


#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <utility>
#include <string>

using namespace std;

const int INF=1000000000;

const int MAXN=101010;

int t[4*MAXN]={0};

int a[MAXN];

void upd(int v,int tl,int tr,int l,int r)
{
    if(l>r)
    {
        return;
    }
    if((tl==l)&&(tr==r))
    {
        t[v]+=1;
        return;
    }
    int mid=(tl+tr)/2;

    upd(2*v,tl,mid,l,min(mid,r));
    upd(2*v+1,mid+1,tr,max(mid+1,l),r);
    return;
}

void fin(int v,int tl,int tr,int val=0)
{
    val+=t[v];

    if(tl==tr)
    {
        a[tl]=val;
        return;
    }

    int mid=(tl+tr)/2;

    fin(2*v,tl,mid,val);
    fin(2*v+1,mid+1,tr,val);
    return;
}

int pref[101010];
int suff[101010];

int main()
{
    cin.tie(NULL);
    ios::sync_with_stdio(false);

    int n,m,l,r;
    cin>>n>>m;

    for(int i=0;i<n;i++)
    {
        cin>>l>>r;
        upd(1,1,m,l,r);
    }

    fin(1,1,m);

    //end of tree

    for(int i=1;i<=m;i++)
    {
        for(int j=0;j<i;j++)
        {
            if(a[j]<=a[i])
            {
                pref[i]=max(pref[i],pref[j]+1);
            }
        }
    }

    for(int i=m;i>0;i--)
    {
        for(int j=m+1;j>i;j--)
        {
            if(a[j]<=a[i])
            {
                suff[i]=max(suff[i],suff[j]+1);
            }
        }
    }

    int maxx=0;

    for(int i=1;i<=m;i++)
    {
        maxx=max(maxx,suff[i]+pref[i]-1);
        //cout<<pref[i]<<' '<<suff[i]<<endl;
    }

    cout<<maxx<<'\n';

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define int long long
#define pb push_back
#define sz size()

const int inf = 1e9;
const int M = 1e6 +1;
const int mod = 1e9 +7;
const int N = 2*1e5 +1;

int n, m, l, r, a[N], tree[4*N], _sz, cur;

int get(int v, int lv, int rv, int r)
{
    if(lv > r) return 0;
    if(r >= rv) return tree[v];

    int mid = (lv+rv)/2;

    return max(get(v+v, lv, mid, r), get(v+v+1, mid+1, rv, r));
}

inline void upd(int pos, int val)
{
    pos += _sz-1;

    while(pos)
    {
        tree[pos] = max(tree[pos], val);

        pos /= 2;
    }
}

main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cout << setprecision(10) << fixed;

    cin >> n >> m;

    for(int i = 1; i <= n; i++)
    {
        cin >> l >> r;

        a[l]++;
        a[r+1]--;
    }

    for(int i = 1; i <= m; i++)
    {
        a[i] += a[i-1];
    }

    for(_sz = 1; _sz < n; _sz += _sz){}

    for(int i = 1; i <= m; i++)
    {
        upd(a[i], get(1, 1, _sz, a[i]) +1);
    }

    cur = get(1, 1, _sz, _sz);

    memset(tree, 0, sizeof(tree));

    for(int i = m; i; i--)
    {
        upd(a[i], get(1, 1, _sz, a[i]) +1);
    }

    cur = max(cur, get(1, 1, _sz, _sz));

    cout << cur;
}


#include<bits/stdc++.h>
using namespace std;

#define int long long
#define pb push_back
#define sz size()

const int inf = 1e9;
const int M = 1e6 +1;
const int mod = 1e9 +7;
const int N = 2*1e5 +1;

int n, m, l, r, a[N], tree[4*N], _sz, cur;

int get(int v, int lv, int rv, int r)
{
    if(lv > r) return 0;
    if(r >= rv) return tree[v];

    int mid = (lv+rv)/2;

    return max(get(v+v, lv, mid, r), get(v+v+1, mid+1, rv, r));
}

inline void upd(int pos, int val)
{
    pos += _sz-1;

    while(pos)
    {
        tree[pos] = max(tree[pos], val);

        pos /= 2;
    }
}

main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cout << setprecision(10) << fixed;

    cin >> n >> m;

    for(int i = 1; i <= n; i++)
    {
        cin >> l >> r;

        a[l]++;
        a[r+1]--;
    }

    for(int i = 1; i <= m; i++)
    {
        a[i] += a[i-1];
    }

    for(_sz = 1; _sz < n; _sz += _sz){}

    for(int i = 1; i <= m; i++)
    {
        if(a[i] == 0) continue;

        upd(a[i], get(1, 1, _sz, a[i]) +1);
    }

    cur = get(1, 1, _sz, _sz);

    memset(tree, 0, sizeof(tree));

    for(int i = m; i; i--)
    {
        if(a[i] == 0) continue;

        upd(a[i], get(1, 1, _sz, a[i]) +1);
    }

    cur = max(cur, get(1, 1, _sz, _sz));

    cout << cur;
}


#include<bits/stdc++.h>
using namespace std;

#define int long long
#define pb push_back
#define sz size()

const int inf = 1e9;
const int M = 1e6 +1;
const int mod = 1e9 +7;
const int N = 2*1e5 +1;

int n, m, l, r, a[N], tree[4*N], dp1[N], dp2[N], _sz, cur, ans;

int get(int v, int lv, int rv, int r)
{
    if(lv > r) return 0;
    if(r >= rv) return tree[v];

    int mid = (lv+rv)/2;

    return max(get(v+v, lv, mid, r), get(v+v+1, mid+1, rv, r));
}

inline void upd(int pos, int val)
{
    pos += _sz-1;

    while(pos)
    {
        tree[pos] = max(tree[pos], val);

        pos /= 2;
    }
}

main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cout << setprecision(10) << fixed;

    cin >> n >> m;

    for(int i = 1; i <= n; i++)
    {
        cin >> l >> r;

        a[l]++;
        a[r+1]--;
    }

    for(int i = 1; i <= m; i++)
    {
        a[i] += a[i-1];
    }

    for(_sz = 1; _sz < n; _sz += _sz){}

    for(int i = 1; i <= m; i++)
    {
        if(a[i] == 0)
        {
            cur ++;
            continue;
        }

        dp1[i] = get(1, 1, _sz, a[i]) +1;

        upd(a[i], dp1[i]);
    }

    memset(tree, 0, sizeof(tree));

    for(int i = m; i; i--)
    {
        if(a[i] == 0) continue;

        dp2[i] = get(1, 1, _sz, a[i]) +1;

        upd(a[i], dp2[i]);
    }

    for(int i = 1; i <= m ;i++)
    {
        ans = max(ans, dp1[i] + dp2[i]-1);
    }

    cout << cur+ans;
}


#include<bits/stdc++.h>
using namespace std;

#define int long long
#define pb push_back
#define sz size()

const int inf = 1e9;
const int M = 1e6 +1;
const int mod = 1e9 +7;
const int N = 2*1e5 +1;

int n, m, l, r, a[N], tree[4*N], _sz;

int get(int v, int lv, int rv, int r)
{
    if(lv > r) return 0;
    if(r >= rv) return tree[v];

    int mid = (lv+rv)/2;

    return max(get(v+v, lv, mid, r), get(v+v+1, mid+1, rv, r));
}

inline void upd(int pos, int val)
{
    pos += _sz-1;

    while(pos)
    {
        tree[pos] = max(tree[pos], val);

        pos /= 2;
    }
}

main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cout << setprecision(10) << fixed;

    cin >> n >> m;

    for(int i = 1; i <= n; i++)
    {
        cin >> l >> r;

        a[l]++;
        a[r+1]--;
    }

    for(int i = 1; i <= m; i++)
    {
        a[i] += a[i-1];
    }

    for(_sz = 1; _sz < n; _sz += _sz){}

    for(int i = 1; i <= m; i++)
    {
        upd(a[i], get(1, 1, _sz, a[i]) +1);
    }

    cout << get(1, 1, _sz, _sz);
}


#include<bits/stdc++.h>
using namespace std;

#define int long long
#define pb push_back
#define sz size()

const int inf = 1e9;
const int M = 1e6 +1;
const int mod = 1e9 +7;
const int N = 2*1e5 +1;

int n, m, l, r, a[N], tree[4*N], _sz, cur;

int get(int v, int lv, int rv, int r)
{
    if(lv > r) return 0;
    if(r >= rv) return tree[v];

    int mid = (lv+rv)/2;

    return max(get(v+v, lv, mid, r), get(v+v+1, mid+1, rv, r));
}

inline void upd(int pos, int val)
{
    pos += _sz-1;

    while(pos)
    {
        tree[pos] = max(tree[pos], val);

        pos /= 2;
    }
}

main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cout << setprecision(10) << fixed;

    cin >> n >> m;

    for(int i = 1; i <= n; i++)
    {
        cin >> l >> r;

        a[l]++;
        a[r+1]--;
    }

    for(int i = 1; i <= m; i++)
    {
        a[i] += a[i-1];
    }

    for(_sz = 1; _sz < n; _sz += _sz){}

    for(int i = 1; i <= m; i++)
    {
        if(a[i] == 0) continue;

        upd(a[i], get(1, 1, _sz, a[i]) +1);
    }

    cur = get(1, 1, _sz, _sz);

    memset(tree, 0, sizeof(tree));

    for(int i = m; i; i--)
    {
        if(a[i] == 0) continue;

        upd(a[i], get(1, 1, _sz, a[i]) +1);
    }

    cur = max(cur, get(1, 1, _sz, _sz));

    if(cur == m) cout << cur-1;
    else cout << cur;
}


#include <bits/stdc++.h>
using namespace std;
int arr[100004],tree[400004],ans[100004];
void update(int node,int start,int en,int idx,int val){
	if(start==en){
		tree[node]=val;
		return;
	}
	if(idx<start || idx>en)
		return;
	int mid=(start+en)/2;
	update(2*node,start,mid,idx,val);
	update(2*node+1,mid+1,en,idx,val);
	tree[node]=max(tree[2*node],tree[2*node+1]);
}
int query(int node,int start,int en,int l,int r){
	if(start>=l && en<=r)
		return tree[node];
	if(start>r || en<l)
		return 0;
	int mid=(start+en)/2;
	return max(query(2*node,start,mid,l,r),query(2*node+1,mid+1,en,l,r));
}
int main(){
	int i,l,r,n,m;
	scanf("%d %d",&n,&m);
	for(i=0;i<n;++i){
		scanf("%d %d",&l,&r);
		arr[l]++;
		arr[r+1]--;
	}
	l=0;
	for(i=1;i<=m;++i){
		l+=arr[i];
		arr[i]=l;
	}
	for(i=1;i<=m;++i){
		l=query(1,0,n-1,0,arr[i]);
		ans[i]=l;
		update(1,0,n-1,arr[i],l+1);
	}
	memset(tree,0,sizeof(tree));
	for(i=m;i>0;--i){
		l=query(1,0,n-1,0,arr[i]);
		ans[i]+=(l+1);
		update(1,0,n-1,arr[i],l+1);
	}
	l=0;
	for(i=1;i<=m;++i)
		l=max(l,ans[i]);
	printf("%d\n",l);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int arr[100004],tree[400004],ans[100004];
void update(int node,int start,int en,int idx,int val){
	if(idx<start || idx>en)
		return;
	if(start==en){
		tree[node]=val;
		return;
	}
	int mid=(start+en)/2;
	update(2*node,start,mid,idx,val);
	update(2*node+1,mid+1,en,idx,val);
	tree[node]=max(tree[2*node],tree[2*node+1]);
}
int query(int node,int start,int en,int l,int r){
	if(start>=l && en<=r)
		return tree[node];
	if(start>r || en<l)
		return 0;
	int mid=(start+en)/2;
	return max(query(2*node,start,mid,l,r),query(2*node+1,mid+1,en,l,r));
}
int main(){
	int i,l,r,n,m;
	scanf("%d %d",&n,&m);
	for(i=0;i<n;++i){
		scanf("%d %d",&l,&r);
		arr[l]++;
		arr[r+1]--;
	}
	l=0;
	for(i=1;i<=m;++i){
		l+=arr[i];
		arr[i]=l;
		//cout<<arr[i]<<endl;
	}
	for(i=1;i<=m;++i){
		l=query(1,0,n-1,0,arr[i]);
		ans[i]=l;
		update(1,0,n-1,arr[i],l+1);
	}
	memset(tree,0,sizeof(tree));
	for(i=m;i>0;--i){
		l=query(1,0,n-1,0,arr[i]);
		ans[i]+=(l+1);
		update(1,0,n-1,arr[i],l+1);
	}
	l=0;
	for(i=1;i<=m;++i)
		l=max(l,ans[i]);
	printf("%d\n",l);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <set>
#include <stack>
#include <sstream>
#include <queue>
#include <map>
#include <functional>
#include <bitset>
//#include <unordered_map>
//#include <unordered_set>

using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define ALL(A) A.begin(), A.end()
#define rep(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define repr(i,n) for(int (i)=(int)(n);(i)>=0;(i)--)
#define repab(i,a,b) for(int (i)=(int)(a);(i)<=(int)(b);(i)++)
#define reprab(i,a,b) for(int (i)=(int)(a);(i)>=(int)(b);(i)--)
#define sc(x) scanf("%d", &x)
#define pr(x) printf("x:%dn", x)
#define fastio ios::sync_with_stdio(false),cin.tie(false)
#define frein freopen("in.txt", "r", stdin)
#define freout freopen("out.txt", "w", stdout)
#define freout1 freopen("out1.txt", "w", stdout)
#define lson(rt) (rt*2+1)
#define rson(rt) (rt*2+2)
#define mid (((l)+(r))/2)
#define br puts("")
#define ABS(x) (((x)<0)?(-(x)):(x))
#define sqr(x) ((x)*(x))
#define debug cout<<"???"<<endl
const int mod = 1e9+7;
const int INF = 0x3f3f3f3f;
const ll INF64 = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-8;
const double PI = acos(-1);
template<class T> T gcd(T a, T b){if(!b)return a;return gcd(b,a%b);}

const int maxn = 1e5+10;

int a[maxn], n, m, pre[maxn],
    suf[maxn], w[maxn], add[maxn];

int main(){
    sc(n); sc(m);
    for(int i = 0; i < n; i++){
        int l,r; sc(l); sc(r);
        add[l]++; add[r+1]--;
    }
    for(int i = 1; i <= m; i++){
        a[i] = a[i-1] + add[i];
    }
    memset(w, INF, sizeof(w));
    w[0] = 0;
    for(int i = 1; i <= m; i++){
        int len = upper_bound(w, w+m, a[i]) - w;
        pre[i] = len;
        w[len] = a[i];
    }
    memset(w, INF, sizeof(w));
    w[0] = 0;
    for(int i = m; i >= 1; i--){
        int len = upper_bound(w, w+m, a[i]) - w;
        suf[i] = len;
        w[len] = a[i];
    }
    int ans = -1;
    for(int i = 1; i <= m; i++)
        ans = max(ans, pre[i]+suf[i]-1);
    printf("%d\n", ans);
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int dat[100005], n, m;
vector <int> inf;
int dat1[100005];
int dat2[100005];

int f(int l, int r, int v)
{
	if (l == r)
	{
		return l;
	}else if (l + 1 == r)
	{
		if (v <= inf[r])
		{
			return r;
		}
		else
		{
			return l;
		}
	}
	else
	{
		int mid = (l + r) / 2;

		if (inf[mid] > v)
		{
			return f(l, mid, v);
		}
		else
		{
			return f(mid, r, v);
		}
	}
}

void f1()
{
	dat1[1] = 1;
	inf.push_back(dat[1]);

	for (int i = 2; i <= m; i++)
	{
		if (dat[i] >= inf.back())
		{
			inf.push_back(dat[i]);
			dat1[i] = inf.size();
		}
		else
		{
			int t = f(0, inf.size()-1, dat[i]);
			inf[t] = dat[i];
			dat1[i] = t + 1;
		}
	}
}

void f2()
{
	dat2[1] = 1;
	inf.push_back(dat[1]);

	for (int i = 2; i <= m; i++)
	{
		if (dat[i] >= inf.back())
		{
			inf.push_back(dat[i]);
			dat2[i] = inf.size();
		}
		else
		{
			int t = f(0, inf.size() - 1, dat[i]);
			inf[t] = dat[i];
			dat2[i] = t + 1;
		}
	}
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	int i, a, b, res=0;

	cin >> n >> m;
	for (i = 0; i < n; i++)
	{
		cin >> a >> b;
		dat[a]++;
		dat[b + 1]--;
	}

	if (n == 1)
	{
		cout << 1;
		return 0;
	}

	for (i = 1; i <= m + 1; i++)
	{
		dat[i] += dat[i - 1];
	}

	f1();
	inf.clear();
	reverse(dat + 1, dat + m + 1);
	f2();
	reverse(dat2 + 1, dat2 + m + 1);
	for (i = 1; i <=m; i++)
	{
		res = max(res, dat1[i]+dat2[i]-1);
	}

	cout << res;

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int dat[100005], n, m;
vector <int> inf;
int dat1[100005];
int dat2[100005];

int f(int l, int r, int v)
{
	if (l + 1 == r)
	{
		if (v <= inf[r])
		{
			return r;
		}
		else
		{
			return l;
		}
	}
	else
	{
		int mid = (l + r) / 2;

		if (inf[mid] > v)
		{
			return f(l, mid, v);
		}
		else
		{
			return f(mid, r, v);
		}
	}
}

void f1()
{
	dat1[1] = 1;
	inf.push_back(dat[1]);

	for (int i = 2; i <= m; i++)
	{
		if (dat[i] >= inf.back())
		{
			inf.push_back(dat[i]);
			dat1[i] = inf.size();
		}
		else
		{
			int t = f(0, inf.size()-1, dat[i]);
			inf[t] = dat[i];
			dat1[i] = t + 1;
		}
	}
}

void f2()
{
	dat2[1] = 1;
	inf.push_back(dat[1]);

	for (int i = 2; i <= m; i++)
	{
		if (dat[i] >= inf.back())
		{
			inf.push_back(dat[i]);
			dat2[i] = inf.size();
		}
		else
		{
			int t = f(0, inf.size() - 1, dat[i]);
			inf[t] = dat[i];
			dat2[i] = t + 1;
		}
	}
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	int i, a, b, res=0;

	cin >> n >> m;
	for (i = 0; i < n; i++)
	{
		cin >> a >> b;
		dat[a]++;
		dat[b + 1]--;
	}

	if (n == 1)
	{
		cout << 1;
		return 0;
	}

	for (i = 1; i <= m + 1; i++)
	{
		dat[i] += dat[i - 1];
	}

	f1();
	inf.clear();
	reverse(dat + 1, dat + m + 1);
	f2();
	reverse(dat2 + 1, dat2 + m + 1);
	for (i = 1; i <=m; i++)
	{
		res = max(res, dat1[i]+dat2[i]-1);
	}

	cout << res;

	return 0;
}


#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cctype>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstdio>
#include <functional>
using namespace std;
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define FORD(i,a,b) for(int i=(a);i>=(b);i--)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
const int INF = (((1<<30)-1)<<1)+1;
const int nINF = 1<<31;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<char, int> pci;

/////////////////////////////////////////////////////////////////////

int n, m;

int lis(vector<int>& a, vector<int>& rec) {
	// REP(i, m) cout << a[i] << ' ';
	// cout << endl;

    int ans = 1, high, low;
    vector<int> dp(m+1, INF);
    dp[1] = a[0];
    rec[0] = 1;
    FOR(i, 1, m-1) {
        high = ans+1;
        low = 1;
        int idx = upper_bound(dp.begin()+low, dp.begin()+high, a[i])-dp.begin();
        dp[idx] = a[i];
        // DEBUG(i);
        // DEBUG(a[i]);
        // DEBUG(idx);
        // DEBUG("");
        ans = max(ans, idx);
        rec[i] = ans;
    }
    return ans;
}

int main() {
    ios::sync_with_stdio(false);
    //cout << fixed << setprecision(7);
    cin >> n >> m;
    vector<int> s(m, 0);
    REP(i, n) {
    	int l, r;
    	cin >> l >> r;
    	--l, --r;
    	s[l]++;
    	if(r < m-1) s[r+1]--;
    }
    // REP(i, m) cout << s[i] << ' ';
    // cout << endl;

    FOR(i, 1, m-1) s[i] += s[i-1];
    vector<int> dpu(m, INF), dpd(m, INF);
    lis(s, dpu);
    reverse(s.begin(), s.end());
    lis(s, dpd);
    reverse(dpd.begin(), dpd.end());
    reverse(s.begin(), s.end());

	// REP(i, m) cout << s[i] << ' ';
 //    cout << endl;

 //    REP(i, m) cout << dpu[i] << ' ';
 //    cout << endl;

 //    REP(i, m) cout << dpd[i] << ' ';
 //    cout << endl;

    int ans = 0;
    REP(i, m-1) ans = max(ans, dpu[i]+dpd[i+1]);
    ans = max(ans, max(dpu.back(), dpd[0]));
    cout << ans << endl;

    return 0;
}


#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cctype>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstdio>
#include <functional>
using namespace std;
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define FORD(i,a,b) for(int i=(a);i>=(b);i--)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
const int INF = (((1<<30)-1)<<1)+1;
const int nINF = 1<<31;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<char, int> pci;

/////////////////////////////////////////////////////////////////////

int n, m;

int lis(vector<int>& a, vector<int>& rec) {
    int ans = 1;
    vector<int> dp(m+1, INF);
    dp[1] = a[0];
    rec[0] = 1;
    FOR(i, 1, m-1) {
        int idx = upper_bound(dp.begin()+1, dp.begin()+ans+1, a[i])-dp.begin();
        dp[idx] = a[i];
        ans = max(ans, idx);
        rec[i] = ans;
    }
    return ans;
}

int main() {
    ios::sync_with_stdio(false);
    //cout << fixed << setprecision(7);
    cin >> n >> m;
    vector<int> s(m, 0);
    int l, r, ans = 0;
    REP(i, n) {
    	cin >> l >> r;
    	--l, --r;
    	s[l]++;
    	if(r < m-1) s[r+1]--;
    }

    FOR(i, 1, m-1) s[i] += s[i-1];
    vector<int> lisv(m), ldsv(m);
    lis(s, lisv);
    reverse(s.begin(), s.end());
    lis(s, ldsv);
    reverse(ldsv.begin(), ldsv.end());

    REP(i, m-1) ans = max(ans, lisv[i]+ldsv[i+1]);
    ans = max(ans, max(lisv.back(), ldsv[0]));
    cout << ans << endl;

    return 0;
}


#include<bits/stdc++.h>
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<string.h>
#include<iostream>
#include<math.h>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<iomanip>
#include<bitset>
using namespace std;         //

#define ll long long
#define ull unsigned long long
#define pb push_back
#define FOR(a) for(int i=1;i<=a;i++)
#define sqr(a) (a)*(a)
#define dis(a,b) sqrt(sqr(a.x-b.x)+sqr(a.y-b.y))
ll qp(ll a,ll b,ll mod){
	ll t=1;while(b){if(b&1)t=t*a%mod;b>>=1;a=a*a%mod;}return t;
}
struct DOT{ll x;ll y;};
inline void read(int &x){int k=0;char f=1;char c=getchar();for(;!isdigit(c);c=getchar())if(c=='-')f=-1;for(;isdigit(c);c=getchar())k=k*10+c-'0';x=k*f;}
const int dx[4]={0,0,-1,1};
const int dy[4]={1,-1,0,0};
const int inf=0x3f3f3f3f;
const ll Linf=0x3f3f3f3f3f3f3f3f;
const ll mod=1e9+7;;

const int maxn=1e5+43;

int n,m;
int a[maxn];
int dp1[maxn],dp2[maxn];
int tmp[maxn];

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1,x,y;i<=n;i++){
		scanf("%d%d",&x,&y);
		a[x]++;a[y+1]--;
	}
	for(int i=1;i<=m;i++){
		a[i]+=a[i-1];
	}

	int len;

	tmp[1]=a[1];len=1;dp1[1]=1;
	for(int i=2;i<=m;i++){
		if(a[i]>=tmp[len]){
			tmp[++len]=a[i];
		}else{
			int pos=upper_bound(tmp+1,tmp+1+len,a[i])-tmp;
			tmp[pos]=a[i];
		}
		dp1[i]=len;
	}

	reverse(a+1,a+1+m);
	memset(tmp,0,sizeof tmp);
	len=1;dp2[1]=1;tmp[1]=a[1];

	for(int i=2;i<=m;i++){
		if(a[i]>=tmp[len]){
			tmp[++len]=a[i];
		}else{
			int pos=upper_bound(tmp+1,tmp+1+len,a[i])-tmp;
			tmp[pos]=a[i];
		}
		dp2[i]=len;
	}

	int ans=0;
	for(int i=1;i<=m;i++){
		ans=max(ans,dp1[i]+dp2[m-i+1]-1);
	}
	printf("%d\n",ans);
}


#include<bits/stdc++.h>
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<string.h>
#include<iostream>
#include<math.h>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<iomanip>
#include<bitset>
using namespace std;         //

#define ll long long
#define ull unsigned long long
#define pb push_back
#define FOR(a) for(int i=1;i<=a;i++)
#define sqr(a) (a)*(a)
#define dis(a,b) sqrt(sqr(a.x-b.x)+sqr(a.y-b.y))
ll qp(ll a,ll b,ll mod){
	ll t=1;while(b){if(b&1)t=t*a%mod;b>>=1;a=a*a%mod;}return t;
}
struct DOT{ll x;ll y;};
inline void read(int &x){int k=0;char f=1;char c=getchar();for(;!isdigit(c);c=getchar())if(c=='-')f=-1;for(;isdigit(c);c=getchar())k=k*10+c-'0';x=k*f;}
const int dx[4]={0,0,-1,1};
const int dy[4]={1,-1,0,0};
const int inf=0x3f3f3f3f;
const ll Linf=0x3f3f3f3f3f3f3f3f;
const ll mod=1e9+7;;

const int maxn=1e5+43;

int n,m;
int a[maxn];
int dp1[maxn],dp2[maxn];
int tmp[maxn];

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1,x,y;i<=n;i++){
		scanf("%d%d",&x,&y);
		a[x]++;a[y+1]--;
	}
	for(int i=1;i<=m;i++){
		a[i]+=a[i-1];
	}

	int len;

	tmp[1]=a[1];len=1;dp1[1]=1;
	for(int i=2;i<=m;i++){
		if(a[i]>=a[len]){
			tmp[++len]=a[i];
		}else{
			int pos=lower_bound(tmp+1,tmp+1+len,a[i])-tmp;
			tmp[pos]=a[i];
		}
		dp1[i]=len;
	}

	reverse(a+1,a+1+m);
	memset(tmp,0,sizeof tmp);len=1;dp2[1]=1;
	tmp[1]=a[1];
	for(int i=2;i<=m;i++){
		if(a[i]>=a[len]){
			tmp[++len]=a[i];
		}else{
			int pos=lower_bound(tmp+1,tmp+1+len,a[i])-tmp;
			tmp[pos]=a[i];
		}
		dp2[i]=len;
	}

	int ans=0;
	for(int i=1;i<=m;i++){
		ans=max(ans,dp1[i]+dp2[m-i+1]-1);
	}
	printf("%d\n",ans);
}


#include<bits/stdc++.h>
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<string.h>
#include<iostream>
#include<math.h>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<iomanip>
#include<bitset>
using namespace std;         //

#define ll long long
#define ull unsigned long long
#define pb push_back
#define FOR(a) for(int i=1;i<=a;i++)
#define sqr(a) (a)*(a)
#define dis(a,b) sqrt(sqr(a.x-b.x)+sqr(a.y-b.y))
ll qp(ll a,ll b,ll mod){
	ll t=1;while(b){if(b&1)t=t*a%mod;b>>=1;a=a*a%mod;}return t;
}
struct DOT{ll x;ll y;};
inline void read(int &x){int k=0;char f=1;char c=getchar();for(;!isdigit(c);c=getchar())if(c=='-')f=-1;for(;isdigit(c);c=getchar())k=k*10+c-'0';x=k*f;}
const int dx[4]={0,0,-1,1};
const int dy[4]={1,-1,0,0};
const int inf=0x3f3f3f3f;
const ll Linf=0x3f3f3f3f3f3f3f3f;
const ll mod=1e9+7;;

const int maxn=1e5+43;

int n,m;
int a[maxn];
int dp1[maxn],dp2[maxn];
int tmp[maxn];

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1,x,y;i<=n;i++){
		scanf("%d%d",&x,&y);
		a[x]++;a[y+1]--;
	}
	for(int i=1;i<=m;i++){
		a[i]+=a[i-1];
	}

	int len;

	tmp[1]=a[1];len=1;dp1[1]=1;
	for(int i=2;i<=m;i++){
		if(a[i]>=tmp[len]){
			tmp[++len]=a[i];
		}else{
			int pos=lower_bound(tmp+1,tmp+1+len,a[i])-tmp;
			tmp[pos]=a[i];
		}
		dp1[i]=len;
	}

	reverse(a+1,a+1+m);
	memset(tmp,0,sizeof tmp);len=1;dp2[1]=1;
	tmp[1]=a[1];
	for(int i=2;i<=m;i++){
		if(a[i]>=tmp[len]){
			tmp[++len]=a[i];
		}else{
			int pos=lower_bound(tmp+1,tmp+1+len,a[i])-tmp;
			tmp[pos]=a[i];
		}
		dp2[i]=len;
	}

	int ans=0;
	for(int i=1;i<=m;i++){
		ans=max(ans,dp1[i]+dp2[m-i+1]-1);
	}
	printf("%d\n",ans);
}


#include <bits/stdc++.h>
using namespace std;

int n,m,l,r,f,bit[100069],p1[100069],p2[100069],ans,s[100069],x,y;

void update(int x,int f){
    while (x<=n){
        bit[x]=max(bit[x],f);
        x+=x&-x;
    }
}
int query(int x){
    int k=0;
    while (x>0){
        k=max(k,bit[x]);
        x-=x&-x;
    }
    return k;
}
int main(){
    scanf("%d%d",&m,&n);
    for (int i=1;i<=m;i++){
        scanf("%d%d",&l,&r);
        s[l]++;
        s[r+1]--;
    }
    s[0]=1;
    for (int i=1;i<=n;i++) s[i]=s[i-1]+s[i];
    int cnt=0;
    for (int i=1;i<=n;i++){
        f=query(s[i]);
        update(s[i],f+1);
        p1[i]=max(f+1,p1[i-1]);
    }
    memset(bit,0,sizeof(bit));
    for (int i=n;i>=1;i--){
        f=query(s[i]);
        update(s[i],f+1);
        p2[i]=max(f+1,p2[i+1]);
    }
    for (int i=1;i<=n;i++) ans=max(ans,p1[i]+p2[i+1]);
    cout<<ans;
}


#include <bits/stdc++.h>
using namespace std;

int n,m,l,r,s[100069],x,y;

int main(){
    scanf("%d%d",&m,&n);
    for (int i=1;i<=m;i++){
        scanf("%d%d",&l,&r);
        s[l]++;
        s[r+1]--;
    }
    for (int i=1;i<=n;i++) s[i]=s[i-1]+s[i];
    x=0;
    while (x<=n){
        if (s[x+1]>=s[x]) x++;
        else break;
    }
    y=n+1;
    while (y>x+1){
        if (s[y-1]>=s[y]) y--;
        else break;
    }
    cout<<(n-(y-x-1));
}


#include <bits/stdc++.h>
using namespace std;

int n,m,l,r,f,bit[100069],p1[100069],p2[100069],ans,s[100069],x,y;

void update(int x,int f){
    while (x<=n){
        bit[x]=max(bit[x],f);
        x+=x&-x;
    }
}
int query(int x){
    int k=0;
    while (x>0){
        k=max(k,bit[x]);
        x-=x&-x;
    }
    return k;
}
int main(){
    scanf("%d%d",&m,&n);
    for (int i=1;i<=m;i++){
        scanf("%d%d",&l,&r);
        s[l]++;
        s[r+1]--;
    }
    for (int i=1;i<=n;i++) s[i]=s[i-1]+s[i];
    int cnt=0;
    for (int i=1;i<=n;i++){
        f=query(s[i]);
        update(s[i],f+1);
        p1[i]=max(f+1,p1[i-1]);
    }
    memset(bit,0,sizeof(bit));
    for (int i=n;i>=1;i--){
        f=query(s[i]);
        update(s[i],f+1);
        p2[i]=max(f+1,p2[i+1]);
    }
    for (int i=1;i<=n;i++) ans=max(ans,p1[i]+p2[i+1]);
    cout<<ans;
}


#include <cstdio>
#define MAXN 100100
#define INF (1<<30)
int a[MAXN];
int diff[MAXN];
int solve(int m) {
	int ans = 0;
	for(int i = 1; i <= m; i++)
		if((a[i - 1] > a[i] && a[i] < a[i + 1]) || a[i] == 0) ans++;
	return m - ans;
}
void add(int l, int r) {
	diff[l]++;
	diff[r + 1]--;
}
void process(int m) {
	for(int i = 1; i <= m; i++) diff[i] += diff[i - 1];
	for(int i = 1; i <= m; i++) a[i] = diff[i];
	a[0] = a[m + 1] = -INF;
}
int main(void) {
	int n, m;
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= n; i++) {
		int l, r;
		scanf("%d%d", &l, &r);
		add(l, r);
	}
	process(m);
	printf("%d\n", solve(m));
	return 0;
}


#include <cstdio>
#define MAXN 100100
#define INF (1<<30)
int a[MAXN];
int diff[MAXN];
int solve(int m) {
	int ans = 0;
	for(int i = 1; i <= m; i++)
		if((a[i - 1] > a[i] && a[i] < a[i + 1]) || a[i] == 0) ans++;
	return m - ans;
}
void add(int l, int r) {
	diff[l]++;
	diff[r + 1]--;
}
void process(int m) {
	for(int i = 1; i <= m; i++) diff[i] += diff[i - 1];
	for(int i = 1; i <= m; i++) a[i] = diff[i];
	a[0] = a[m + 1] = INF;
}
int main(void) {
	int n, m;
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= n; i++) {
		int l, r;
		scanf("%d%d", &l, &r);
		add(l, r);
	}
	process(m);
	printf("%d\n", solve(m));
	return 0;
}


#include <cstdio>
#include <set>
#define MAXN 100100
#define INF (1<<30)
int a[MAXN];
int ar[MAXN];
int lisp[MAXN];
int liss[MAXN];
int diff[MAXN];
void lis_pref(int m) {
	std::multiset<int> s;
	std::multiset<int>::iterator it;
	for(int i = 1; i <= m; i++) {
		s.insert(a[i]);
		it = s.upper_bound(a[i]);
		if(it != s.end()) s.erase(it);
		lisp[i] = s.size();
	}
}
void lis_suf(int m) {
	std::multiset<int> s;
	std::multiset<int>::iterator it;
	for(int i = m; i > 0; i--) {
		s.insert(a[i]);
		it = s.upper_bound(a[i]);
		if(it != s.end()) s.erase(it);
		liss[i] = s.size();
	}
}
void add(int l, int r) {
	diff[l]++;
	diff[r + 1]--;
}
int solve(int m) {
	int mans = 0;
	for(int i = 1; i <= m; i++) mans = std::max(mans, lisp[i] + liss[i] - 1);
	return mans;
}
void process(int m) {
	for(int i = 1; i <= m; i++) diff[i] += diff[i - 1];
	for(int i = 1; i <= m; i++) a[i] = diff[i];
	a[0] = a[m + 1] = -INF;
}
int main(void) {
	int n, m;
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= n; i++) {
		int l, r;
		scanf("%d%d", &l, &r);
		add(l, r);
	}
	process(m);
	lis_pref(m);
	lis_suf(m);
	printf("%d\n", solve(m));
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = 100010;
int Pre[maxn], Suf[maxn];//Pre[i]表示以第i个数结尾的最长非严格递增子序列的长度
int N[maxn];
int Stk[maxn], top;
const int INF = 0x3f3f3f3f;
int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	memset(N, 0, sizeof(N));
	for (int i = 1; i <= n; i++)
	{
		int l, r;
		scanf("%d%d", &l, &r);
		N[l]++, N[r + 1]--;
	}
	for (int i = 2; i <= m; i++) N[i] += N[i - 1];//得到所有端点被覆盖的线段数
	top = -1;
	for (int i = 1; i <= m; i++)
	{
		if (top == -1)
		{
			Stk[++top] = N[i];
			Pre[i] = top + 1;
		}
		else
		{
			if (Stk[top] <= N[i])
			{
				Stk[++top] = N[i];
				Pre[i] = top + 1;
			}
			else
			{
				int index = upper_bound(Stk, Stk + top + 1, N[i]) - Stk;
				Stk[index] = N[i];
				Pre[i] = index + 1;
			}
		}
	}
	top = -1;
	for (int i = m; i>= 1; i--)
	{
		if (top == -1)
		{
			Stk[++top] = N[i];
			Suf[i] = top + 1;
		}
		else
		{
			if (Stk[top] <= N[i])
			{
				Stk[++top] = N[i];
				Suf[i] = top + 1;
			}
			else
			{
				int index = upper_bound(Stk, Stk + top + 1, N[i]) - Stk;
				Stk[index] = N[i];
				Suf[i] = index + 1;
			}
		}
	}
	int ans = 0;
	for (int i = 1; i <= n; i++) ans = max(ans, Pre[i] + Suf[i] - 1);
	printf("%d\n", ans);
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = 100010;
int Pre[maxn], Suf[maxn];//Pre[i]表示[0,i]的最长非严格递增子序列的长度
int N[maxn];
int Stk[maxn], top;
const int INF = 0x3f3f3f3f;
int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	memset(N, 0, sizeof(N));
	for (int i = 1; i <= n; i++)
	{
		int l, r;
		scanf("%d%d", &l, &r);
		N[l]++, N[r + 1]--;
	}
	for (int i = 2; i <= m; i++) N[i] += N[i - 1];//得到所有端点被覆盖的线段数
	top = -1;
	for (int i = 1; i <= m; i++)
	{
		if (top == -1)
		{
			Stk[++top] = N[i];
			Pre[i] = top + 1;
		}
		else
		{
			if (Stk[top] <= N[i])
			{
				Stk[++top] = N[i];
				Pre[i] = top + 1;
			}
			else
			{
				int index = upper_bound(Stk, Stk + top + 1, N[i]) - Stk;
				Stk[index] = N[i];
				Pre[i] = top + 1;
			}
		}
	}
	top = -1;
	for (int i = m; i >= 1; i--)
	{
		if (top == -1)
		{
			Stk[++top] = N[i];
			Suf[i] = top + 1;
		}
		else
		{
			if (Stk[top] <= N[i])
			{
				Stk[++top] = N[i];
				Suf[i] = top + 1;
			}
			else
			{
				int index = upper_bound(Stk, Stk + top + 1, N[i]) - Stk;
				Stk[index] = N[i];
				Suf[i] = top + 1;
			}
		}
	}
	int ans = 0;
	for (int i = 1; i <= m; i++) ans = max(ans, Pre[i] + Suf[i + 1]);
	printf("%d\n", ans);
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = 100010;
int Pre[maxn], Suf[maxn];//Pre[i]表示以第i个数结尾的最长非严格递增子序列的长度
int N[maxn];
int Stk[maxn], top;
const int INF = 0x3f3f3f3f;
int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	memset(N, 0, sizeof(N));
	for (int i = 1; i <= n; i++)
	{
		int l, r;
		scanf("%d%d", &l, &r);
		N[l]++, N[r + 1]--;
	}
	for (int i = 2; i <= m; i++) N[i] += N[i - 1];//得到所有端点被覆盖的线段数
	top = -1;
	for (int i = 1; i <= m; i++)
	{
		if (top == -1)
		{
			Stk[++top] = N[i];
			Pre[i] = top + 1;
		}
		else
		{
			if (Stk[top] <= N[i])
			{
				Stk[++top] = N[i];
				Pre[i] = top + 1;
			}
			else
			{
				int index = upper_bound(Stk, Stk + top + 1, N[i]) - Stk;
				Stk[index] = N[i];
				Pre[i] = top + 1;
			}
		}
	}
	top = -1;
	for (int i = m; i>= 1; i--)
	{
		if (top == -1)
		{
			Stk[++top] = N[i];
			Suf[i] = top + 1;
		}
		else
		{
			if (Stk[top] <= N[i])
			{
				Stk[++top] = N[i];
				Suf[i] = top + 1;
			}
			else
			{
				int index = upper_bound(Stk, Stk + top + 1, N[i]) - Stk;
				Stk[index] = N[i];
				Suf[i] = top + 1;
			}
		}
	}
	int ans = 0;
	for (int i = 1; i <= n; i++) ans = max(ans, Pre[i] + Suf[i] - 1);
	printf("%d\n", ans);
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = 100010;
int Pre[maxn], Suf[maxn];//Pre[i]表示以第i个数结尾的最长非严格递增子序列的长度
int N[maxn];
int Stk[maxn], top;
const int INF = 0x3f3f3f3f;
int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	memset(N, 0, sizeof(N));
	for (int i = 1; i <= n; i++)
	{
		int l, r;
		scanf("%d%d", &l, &r);
		N[l]++, N[r + 1]--;
	}
	for (int i = 2; i <= m; i++) N[i] += N[i - 1];//得到所有端点被覆盖的线段数
	top = -1;
	for (int i = 1; i <= m; i++)
	{
		if (top == -1)
		{
			Stk[++top] = N[i];
			Pre[i] = top + 1;
		}
		else
		{
			if (Stk[top] <= N[i])
			{
				Stk[++top] = N[i];
				Pre[i] = top + 1;
			}
			else
			{
				int index = upper_bound(Stk, Stk + top + 1, N[i]) - Stk;
				Stk[index] = N[i];
				Pre[i] = top + 1;
			}
		}
	}
	top = -1;
	for (int i = m; i>= 1; i--)
	{
		if (top == -1)
		{
			Stk[++top] = N[i];
			Suf[i] = top + 1;
		}
		else
		{
			if (Stk[top] <= N[i])
			{
				Stk[++top] = N[i];
				Suf[i] = top + 1;
			}
			else
			{
				int index = upper_bound(Stk, Stk + top + 1, N[i]) - Stk;
				Stk[index] = N[i];
				Suf[i] = top + 1;
			}
		}
	}
	int ans = 0;
	for (int i = 1; i <= n; i++) ans = max(ans, Pre[i] + Suf[i+1]);
	printf("%d\n", ans);
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = 100010;
int Pre[maxn], Suf[maxn];//Pre[i]表示[0,i]的最长非严格递增子序列的长度
int N[maxn];
int Stk[maxn], top;
const int INF = 0x3f3f3f3f;
int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	memset(N, 0, sizeof(N));
	for (int i = 1; i <= n; i++)
	{
		int l, r;
		scanf("%d%d", &l, &r);
		N[l]++, N[r + 1]--;
	}
	for (int i = 2; i <= m; i++) N[i] += N[i - 1];//得到所有端点被覆盖的线段数
	memset(Stk, INF, sizeof(Stk));
	int top = 0;
	for (int i = 1; i <= m; i++)
	{
		int index = upper_bound(Stk, Stk + m, N[i])-Stk;
		Stk[index] = N[i];
		top = max(top, index);
		Pre[i] = top + 1;
	}
	memset(Stk, INF, sizeof(Stk));
	top = 0;
	for (int i = m; i>= 1; i--)
	{
		int index = upper_bound(Stk, Stk + m, N[i]) - Stk;
		Stk[index] = N[i];
		top = max(top, index);
		Suf[i] = top + 1;
	}
	int ans = 0;
	for (int i = 1; i <= n; i++) ans = max(ans, Pre[i] + Suf[i+1]);
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <class T> int size(const T &x) { return x.size(); }
#define rep(i,a,b) for(int i = (a); i < (b); i++)
#define iter(it,c) for(auto it = (c).begin(); it != (c).end();++it)
#define pb push_back
#define fs first
#define sc second
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

const int INF = ~(1<<31);
const double pi = acos(-1);
const double EPS = 1e-9;

vi lis(vi arr) {
  vi seq, back(size(arr)), ans;
  rep(i,0,size(arr)) {
    int res = 0, lo = 1, hi = size(seq);
    while (lo <= hi) {
      int mid = (lo+hi)/2;
      if (arr[seq[mid-1]] <= arr[i]) res = mid, lo = mid + 1;
      else hi = mid - 1; }
    if (res < size(seq)) seq[res] = i;
    else seq.push_back(i);
    back[i] = res == 0 ? -1 : seq[res-1]; }
  int at = seq.back();
  while (at != -1) ans.push_back(at), at = back[at];
  reverse(ans.begin(), ans.end());
  return ans; }

int ans[100050];
int main() {
    cin.sync_with_stdio(false);
	memset(ans,0,sizeof(ans));
	int n,k;
	cin >> n >> k;
	rep(i,0,n) {
		int a,b;
		cin >> a >> b;
		a--;b--;
		ans[a] += 1;
		ans[b+1] -= 1;
	}
	vi A(k);
	int sum = 0;
	rep(i,0,k) {
		sum += ans[i];
		A[i] = sum;
	}
	cout << lis(A).size() << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <class T> int size(const T &x) { return x.size(); }
#define rep(i,a,b) for(int i = (a); i < (b); i++)
#define iter(it,c) for(auto it = (c).begin(); it != (c).end();++it)
#define pb push_back
#define fs first
#define sc second
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

const int INF = ~(1<<31);
const double pi = acos(-1);
const double EPS = 1e-9;

vi lis(vi a) {
	int n = a.size(), l = 0, lo, hi, mid;
	vi dp(n + 1, INF), rtn;
	dp[0] = -1;
	for (const auto &i : a) {
		lo = 0, hi = l;
		while (lo < hi) {
			mid = (lo + hi + 1) / 2;
			if (dp[mid] > i) hi = mid - 1;
			else lo = mid;
		}
		l = max(l, ++lo);
		dp[lo] = i;
		rtn.push_back(lo);
	}
	return rtn;
}

int main() {
    cin.sync_with_stdio(false);
	int n,m;
	cin >> n >> m;
	vi P(m+1,0);
	rep(i,0,n) {
		int a,b;
		cin >> a >> b;
		a--;
		P[b]--;
		P[a]++;
	}
	rep(i,0,m) P[i+1]+=P[i];
	P.pop_back();
	vi L1 = lis(P);
	reverse(P.begin(), P.end());
	vi L2 = lis(P);
	reverse(L2.begin(), L2.end());
	int mx = 0;
	rep(i,0,m) {
		mx = max(L1[i]+L2[i]-1, mx);
	}
	cout << mx << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <class T> int size(const T &x) { return x.size(); }
#define rep(i,a,b) for(int i = (a); i < (b); i++)
#define iter(it,c) for(auto it = (c).begin(); it != (c).end();++it)
#define pb push_back
#define fs first
#define sc second
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

const int INF = ~(1<<31);
const double pi = acos(-1);
const double EPS = 1e-9;

vi lis(vi arr) {
  vi seq, back(size(arr)), ans;
  rep(i,0,size(arr)) {
    int res = 0, lo = 1, hi = size(seq);
    while (lo <= hi) {
      int mid = (lo+hi)/2;
      if (arr[seq[mid-1]] <= arr[i]) res = mid, lo = mid + 1;
      else hi = mid - 1; }
    if (res < size(seq)) seq[res] = i;
    else seq.push_back(i);
    back[i] = res == 0 ? -1 : seq[res-1]; }
  int at = seq.back();
  while (at != -1) ans.push_back(at), at = back[at];
  reverse(ans.begin(), ans.end());
  return ans; }

int ans[100050];
int main() {
    cin.sync_with_stdio(false);
	memset(ans,0,sizeof(ans));
	int n,k;
	cin >> n >> k;
	rep(i,0,n) {
		int a,b;
		cin >> a >> b;
		a--;b--;
		ans[a] += 1;
		ans[b+1] -= 1;
	}
	vi A(k);
	int sum = 0;
	rep(i,0,k) {
		sum += ans[i];
		A[i] = sum;
	}
	int first = lis(A).size();
	reverse(A.begin(), A.end());
	int second = lis(A).size();
	cout << max(first,second) << endl;

    return 0;
}


//{
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double lf;
typedef pair<ll,ll> ii;
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define FILL(i,n) memset(i,n,sizeof i)
#define X first
#define Y second
#define SZ(_a) (int)_a.size()
#define ALL(_a) _a.begin(),_a.end()
#define pb push_back
#ifdef brian
#define debug(...) do{\
    fprintf(stderr,"%s - %d (%s) = ",__PRETTY_FUNCTION__,__LINE__,#__VA_ARGS__);\
    _do(__VA_ARGS__);\
}while(0)
template<typename T>void _do(T &&_x){cerr<<_x<<endl;}
template<typename T,typename ...S> void _do(T &&_x,S &&..._t){cerr<<_x<<" ,";_do(_t...);}
template<typename _a,typename _b> ostream& operator << (ostream &_s,const pair<_a,_b> &_p){return _s<<"("<<_p.X<<","<<_p.Y<<")";}
template<typename It> ostream& _OUTC(ostream &_s,It _ita,It _itb)
{
    _s<<"{";
    for(It _it=_ita;_it!=_itb;_it++)
    {
        _s<<(_it==_ita?"":",")<<*_it;
    }
    _s<<"}";
    return _s;
}
template<typename _a> ostream &operator << (ostream &_s,vector<_a> &_c){return _OUTC(_s,ALL(_c));}
template<typename _a> ostream &operator << (ostream &_s,set<_a> &_c){return _OUTC(_s,ALL(_c));}
template<typename _a,typename _b> ostream &operator << (ostream &_s,map<_a,_b> &_c){return _OUTC(_s,ALL(_c));}
template<typename _t> void pary(_t _a,_t _b){_OUTC(cerr,_a,_b);cerr<<endl;}
#define IOS()
#else
#define debug(...)
#define pary(...)
#define endl '\n'
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#endif // brian
//}

const ll MAXn=1e5+5,MAXlg=__lg(MAXn)+2;
const ll MOD=1000000007;
const ll INF=ll(1e15);

ll d[MAXn];
ll mxinc[MAXn];
vector<ll> dt;

int main()
{
    IOS();
    ll n,m;
    cin>>n>>m;
    REP(i,n)
    {
      ll l,r;
      cin>>l>>r;
      d[r+1]--;
      d[l]++;
    }
    REP1(i,n+1)
    {
      d[i]+=d[i-1];
    }
    REP1(i,m)
    {
      if(!SZ(dt)||d[i]>=dt.back())dt.pb(d[i]);
      else *upper_bound(ALL(dt),d[i])=d[i];
      mxinc[i]=SZ(dt);
    }
    dt.clear();
    ll ans=mxinc[m];
    for(int i=m;i>0;i--)
    {
      if(!SZ(dt)||d[i]>=dt.back())dt.pb(d[i]);
      else *upper_bound(ALL(dt),d[i])=d[i];
      ans=max(ans,mxinc[i-1]+SZ(dt));
    }
    dt.clear();
    cout<<ans<<endl;
}


//{
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double lf;
typedef pair<ll,ll> ii;
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define FILL(i,n) memset(i,n,sizeof i)
#define X first
#define Y second
#define SZ(_a) (int)_a.size()
#define ALL(_a) _a.begin(),_a.end()
#define pb push_back
#ifdef brian
#define debug(...) do{\
    fprintf(stderr,"%s - %d (%s) = ",__PRETTY_FUNCTION__,__LINE__,#__VA_ARGS__);\
    _do(__VA_ARGS__);\
}while(0)
template<typename T>void _do(T &&_x){cerr<<_x<<endl;}
template<typename T,typename ...S> void _do(T &&_x,S &&..._t){cerr<<_x<<" ,";_do(_t...);}
template<typename _a,typename _b> ostream& operator << (ostream &_s,const pair<_a,_b> &_p){return _s<<"("<<_p.X<<","<<_p.Y<<")";}
template<typename It> ostream& _OUTC(ostream &_s,It _ita,It _itb)
{
    _s<<"{";
    for(It _it=_ita;_it!=_itb;_it++)
    {
        _s<<(_it==_ita?"":",")<<*_it;
    }
    _s<<"}";
    return _s;
}
template<typename _a> ostream &operator << (ostream &_s,vector<_a> &_c){return _OUTC(_s,ALL(_c));}
template<typename _a> ostream &operator << (ostream &_s,set<_a> &_c){return _OUTC(_s,ALL(_c));}
template<typename _a,typename _b> ostream &operator << (ostream &_s,map<_a,_b> &_c){return _OUTC(_s,ALL(_c));}
template<typename _t> void pary(_t _a,_t _b){_OUTC(cerr,_a,_b);cerr<<endl;}
#define IOS()
#else
#define debug(...)
#define pary(...)
#define endl '\n'
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#endif // brian
//}

const ll MAXn=1e5+5,MAXlg=__lg(MAXn)+2;
const ll MOD=1000000007;
const ll INF=ll(1e15);

ll d[MAXn];
ll mxinc[MAXn];
vector<ll> dt;

int main()
{
    IOS();
    ll n,m;
    cin>>n>>m;
    REP(i,n)
    {
      ll l,r;
      cin>>l>>r;
      d[r+1]--;
      d[l]++;
    }
    REP1(i,m+1)
    {
      d[i]+=d[i-1];
    }
    REP1(i,m)
    {
      if(!SZ(dt)||d[i]>=dt.back())dt.pb(d[i]);
      else *upper_bound(ALL(dt),d[i])=d[i];
      mxinc[i]=SZ(dt);
    }
    dt.clear();
    ll ans=mxinc[m];
    for(int i=m;i>0;i--)
    {
      if(!SZ(dt)||d[i]>=dt.back())dt.pb(d[i]);
      else *upper_bound(ALL(dt),d[i])=d[i];
      ans=max(ans,mxinc[i-1]+SZ(dt));
    }
    dt.clear();
    cout<<ans<<endl;
}


//{{{By oToToT
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <bitset>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <random>
#include <utility>
#include <stack>
#include <sstream>
#include <functional>
#include <deque>
#include <cassert>
using namespace std;
/* include everything for Kotori~ <3 */

typedef long long lld;
typedef unsigned long long llu;
typedef long double llf;
typedef pair<int,int> PII;
typedef pair<int,lld> PIL;
typedef pair<lld,int> PLI;
typedef pair<lld,lld> PLL;
template<typename T>
using maxHeap = priority_queue<T,vector<T>,less<T>>;
template<typename T>
using minHeap = priority_queue<T,vector<T>,greater<T>>;
/* define some types for Ruby! */

#define FF first
#define SS second
#define SZ(x) (int)(x.size())
#define ALL(x) begin(x), end(x)
#define PB push_back
#define WC(x) while(x--)
/* make code shorter for Di~a~ */

template<typename Iter>
ostream& _out(ostream &s, Iter b, Iter e) {
	s<<"[";
	for ( auto it=b; it!=e; it++ ) s<<(it==b?"":" ")<<*it;
	s<<"]";
	return s;
}
template<typename A, typename B>
ostream& operator <<( ostream &s, const pair<A,B> &p ) { return s<<"("<<p.FF<<","<<p.SS<<")"; }
template<typename T>
ostream& operator <<( ostream &s, const vector<T> &c ) { return _out(s,ALL(c)); }
/* make output easier for Ainyan~n~ */

bool debug = 0;
#define DUMP(x) if(debug) cerr<<__PRETTY_FUNCTION__<<":"<<__LINE__<<" - "<<#x<<"="<<x<<'\n'
template<typename T>
void DEBUG(const T& x){if(debug) cerr<<x;}
template<typename T, typename... Args>
void DEBUG(const T& head,const Args& ...tail){
	if(debug){cerr<<head; DEBUG(tail...);}
}
/* Let's debug with Nico~Nico~Ni */
//}}}
const int N = 100000 + 5;
const int INF = 1<<30;

class BIT{
	private:
		int n;
		vector<int> arr;
		inline int lowbit(int x){return x&(-x);}
	public:
		void init(int n_){
			n = n_;
			arr.clear(); arr.resize(n);
			fill(ALL(arr), 0);
		}
		int query(int p){
			int ret = 0;
			while(p){
				ret = max(ret, arr[p]);
				p -= lowbit(p);
			}
			return ret;
		}
		void modify(int p, int x){
			while(p < n){
				arr[p] = max(arr[p], x);
				p += lowbit(p);
			}
		}
} bit;

class LiSan{
	private:
		vector<int> vv;
	public:
		void init(){vv.clear();}
		void insert(int x){vv.PB(x);}
		void done(){
			sort(ALL(vv));
			vv.resize(distance(vv.begin(), unique(ALL(vv))));
		}
		int size(){return SZ(vv);}
		int get(int x){
			return distance(vv.begin(), lower_bound(ALL(vv), x))+1;
		}
} lisan;

int arr[N], lis[N], rlis[N];

int main(int argc, char* argv[]){
	if(argc>1 and string(argv[1])=="-D") debug=1;
	if(!debug){ios_base::sync_with_stdio(0);cin.tie(0);}
	int n, m; cin >> n >> m;
	for(int i=0;i<n;i++){
		int l, r; cin >> l >> r;
		arr[l]++; arr[r+1]--;
	}
	lisan.insert(-1);
	for(int i=1;i<=m;i++){
		arr[i] += arr[i-1];
		lisan.insert(arr[i]);
	}
	lisan.insert(INF);
	lisan.done();
	bit.init(lisan.size()+5);
	for(int i=1;i<=m;i++){
		int pre = bit.query(lisan.get(arr[i]))+1;
		bit.modify(lisan.get(arr[i]), pre);
		lis[i] = bit.query(lisan.get(INF));
	}
	bit.init(lisan.size()+5);
	for(int i=m;i>=1;i--){
		int pre = bit.query(lisan.get(arr[i]))+1;
		bit.modify(lisan.get(arr[i]), pre);
		rlis[i] = bit.query(lisan.get(INF));
	}
	int ans = 0;
	for(int i=0;i<=m;i++) ans = max(ans, lis[i]+rlis[i+1]);
	cout << ans << '\n';
	return 0;
}


//{{{By oToToT
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <bitset>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <random>
#include <utility>
#include <stack>
#include <sstream>
#include <functional>
#include <deque>
#include <cassert>
using namespace std;
/* include everything for Kotori~ <3 */

typedef long long lld;
typedef unsigned long long llu;
typedef long double llf;
typedef pair<int,int> PII;
typedef pair<int,lld> PIL;
typedef pair<lld,int> PLI;
typedef pair<lld,lld> PLL;
template<typename T>
using maxHeap = priority_queue<T,vector<T>,less<T>>;
template<typename T>
using minHeap = priority_queue<T,vector<T>,greater<T>>;
/* define some types for Ruby! */

#define FF first
#define SS second
#define SZ(x) (int)(x.size())
#define ALL(x) begin(x), end(x)
#define PB push_back
#define WC(x) while(x--)
/* make code shorter for Di~a~ */

template<typename Iter>
ostream& _out(ostream &s, Iter b, Iter e) {
	s<<"[";
	for ( auto it=b; it!=e; it++ ) s<<(it==b?"":" ")<<*it;
	s<<"]";
	return s;
}
template<typename A, typename B>
ostream& operator <<( ostream &s, const pair<A,B> &p ) { return s<<"("<<p.FF<<","<<p.SS<<")"; }
template<typename T>
ostream& operator <<( ostream &s, const vector<T> &c ) { return _out(s,ALL(c)); }
/* make output easier for Ainyan~n~ */

bool debug = 0;
#define DUMP(x) if(debug) cerr<<__PRETTY_FUNCTION__<<":"<<__LINE__<<" - "<<#x<<"="<<x<<'\n'
template<typename T>
void DEBUG(const T& x){if(debug) cerr<<x;}
template<typename T, typename... Args>
void DEBUG(const T& head,const Args& ...tail){
	if(debug){cerr<<head; DEBUG(tail...);}
}
/* Let's debug with Nico~Nico~Ni */
//}}}
const int N = 100000 + 5;
const int INF = 1<<30;

class BIT{
	private:
		int n;
		vector<int> arr;
		inline int lowbit(int x){return x&(-x);}
	public:
		void init(int n_){
			n = n_;
			arr.clear(); arr.resize(n);
		}
		int query(int p){
			int ret = 0;
			while(p){
				ret = max(ret, arr[p]);
				p -= lowbit(p);
			}
			return ret;
		}
		void modify(int p, int x){
			while(p < n){
				arr[p] = max(arr[p], x);
				p += lowbit(p);
			}
		}
} bit;

class LiSan{
	private:
		vector<int> vv;
	public:
		void init(){vv.clear();}
		void insert(int x){vv.PB(x);}
		void done(){
			sort(ALL(vv));
			vv.resize(distance(vv.begin(), unique(ALL(vv))));
		}
		int size(){return SZ(vv);}
		int get(int x){
			return distance(vv.begin(), lower_bound(ALL(vv), x))+1;
		}
} lisan;

int arr[N], lis[N], rlis[N];

int main(int argc, char* argv[]){
	if(argc>1 and string(argv[1])=="-D") debug=1;
	if(!debug){ios_base::sync_with_stdio(0);cin.tie(0);}
	int n, m; cin >> n >> m;
	for(int i=0;i<n;i++){
		int l, r; cin >> l >> r;
		arr[l]++; arr[r+1]--;
	}
	lisan.insert(-1);
	for(int i=1;i<=m;i++){
		arr[i] += arr[i-1];
		lisan.insert(arr[i]);
	}
	lisan.insert(INF);
	lisan.done();
	bit.init(lisan.size()+5);
	for(int i=1;i<=m;i++){
		int pre = bit.query(lisan.get(arr[i]))+1;
		bit.modify(lisan.get(arr[i]), pre);
		lis[i] = lisan.get(INF);
	}
	bit.init(lisan.size()+5);
	for(int i=m;i>=1;i--){
		int pre = bit.query(lisan.get(arr[i]))+1;
		bit.modify(lisan.get(arr[i]), pre);
		rlis[i] = lisan.get(INF);
	}
	int ans = 0;
	for(int i=0;i<=m;i++) ans = max(ans, lis[i]+rlis[i+1]);
	cout << ans << '\n';
	return 0;
}


//{{{By oToToT
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <bitset>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <random>
#include <utility>
#include <stack>
#include <sstream>
#include <functional>
#include <deque>
#include <cassert>
using namespace std;
/* include everything for Kotori~ <3 */

typedef long long lld;
typedef unsigned long long llu;
typedef long double llf;
typedef pair<int,int> PII;
typedef pair<int,lld> PIL;
typedef pair<lld,int> PLI;
typedef pair<lld,lld> PLL;
template<typename T>
using maxHeap = priority_queue<T,vector<T>,less<T>>;
template<typename T>
using minHeap = priority_queue<T,vector<T>,greater<T>>;
/* define some types for Ruby! */

#define FF first
#define SS second
#define SZ(x) (int)(x.size())
#define ALL(x) begin(x), end(x)
#define PB push_back
#define WC(x) while(x--)
/* make code shorter for Di~a~ */

template<typename Iter>
ostream& _out(ostream &s, Iter b, Iter e) {
	s<<"[";
	for ( auto it=b; it!=e; it++ ) s<<(it==b?"":" ")<<*it;
	s<<"]";
	return s;
}
template<typename A, typename B>
ostream& operator <<( ostream &s, const pair<A,B> &p ) { return s<<"("<<p.FF<<","<<p.SS<<")"; }
template<typename T>
ostream& operator <<( ostream &s, const vector<T> &c ) { return _out(s,ALL(c)); }
/* make output easier for Ainyan~n~ */

bool debug = 0;
#define DUMP(x) if(debug) cerr<<__PRETTY_FUNCTION__<<":"<<__LINE__<<" - "<<#x<<"="<<x<<'\n'
template<typename T>
void DEBUG(const T& x){if(debug) cerr<<x;}
template<typename T, typename... Args>
void DEBUG(const T& head,const Args& ...tail){
	if(debug){cerr<<head; DEBUG(tail...);}
}
/* Let's debug with Nico~Nico~Ni */
//}}}
const int N = 100000 + 5;

class BIT{
	private:
		int n;
		vector<int> arr;
		inline int lowbit(int x){return x&(-x);}
		int query(int p){
			int ret = 0;
			while(p){
				ret += arr[p];
				p -= lowbit(p);
			}
			return ret;
		}
	public:
		void init(int n_){
			n = n_;
			arr.clear(); arr.resize(n);
		}
		int query(int l, int r){
			return query(r) - query(l);
		}
		void modify(int p, int x){
			while(p < n){
				arr[p] = max(arr[p], x);
				p += lowbit(p);
			}
		}
} bit;

class LiSan{
	private:
		vector<int> vv;
	public:
		void init(){vv.clear();}
		void insert(int x){vv.PB(x);}
		void done(){
			sort(ALL(vv));
			vv.resize(distance(vv.begin(), unique(ALL(vv))));
		}
		int size(){return SZ(vv);}
		int get(int x){
			return distance(vv.begin(), lower_bound(ALL(vv), x))+1;
		}
} lisan;

int arr[N];

int main(int argc, char* argv[]){
	if(argc>1 and string(argv[1])=="-D") debug=1;
	if(!debug){ios_base::sync_with_stdio(0);cin.tie(0);}
	int n, m; cin >> n >> m;
	for(int i=0;i<n;i++){
		int l, r; cin >> l >> r;
		arr[l]++; arr[r+1]--;
	}
	lisan.insert(-1);
	for(int i=1;i<=m;i++){
		arr[i] += arr[i-1];
		lisan.insert(arr[i]);
	}
	lisan.done();
	int ans0 = 0, ans1 = 0;
	bit.init(lisan.size()+5);
	for(int i=1;i<=m;i++){
		int pre = bit.query(lisan.get(-1), lisan.get(arr[i]))+1;
		ans0 = max(pre, ans0);
		bit.modify(lisan.get(arr[i]), pre);
	}
	bit.init(lisan.size()+5);
	for(int i=m;i>=1;i--){
		int pre = bit.query(lisan.get(-1), lisan.get(arr[i]))+1;
		ans1 = max(pre, ans1);
		bit.modify(lisan.get(arr[i]), pre);
	}
	cout << max(ans0, ans1) << '\n';
	return 0;
}


//{{{By oToToT
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <bitset>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <random>
#include <utility>
#include <stack>
#include <sstream>
#include <functional>
#include <deque>
#include <cassert>
using namespace std;
/* include everything for Kotori~ <3 */

typedef long long lld;
typedef unsigned long long llu;
typedef long double llf;
typedef pair<int,int> PII;
typedef pair<int,lld> PIL;
typedef pair<lld,int> PLI;
typedef pair<lld,lld> PLL;
template<typename T>
using maxHeap = priority_queue<T,vector<T>,less<T>>;
template<typename T>
using minHeap = priority_queue<T,vector<T>,greater<T>>;
/* define some types for Ruby! */

#define FF first
#define SS second
#define SZ(x) (int)(x.size())
#define ALL(x) begin(x), end(x)
#define PB push_back
#define WC(x) while(x--)
/* make code shorter for Di~a~ */

template<typename Iter>
ostream& _out(ostream &s, Iter b, Iter e) {
	s<<"[";
	for ( auto it=b; it!=e; it++ ) s<<(it==b?"":" ")<<*it;
	s<<"]";
	return s;
}
template<typename A, typename B>
ostream& operator <<( ostream &s, const pair<A,B> &p ) { return s<<"("<<p.FF<<","<<p.SS<<")"; }
template<typename T>
ostream& operator <<( ostream &s, const vector<T> &c ) { return _out(s,ALL(c)); }
/* make output easier for Ainyan~n~ */

bool debug = 0;
#define DUMP(x) if(debug) cerr<<__PRETTY_FUNCTION__<<":"<<__LINE__<<" - "<<#x<<"="<<x<<'\n'
template<typename T>
void DEBUG(const T& x){if(debug) cerr<<x;}
template<typename T, typename... Args>
void DEBUG(const T& head,const Args& ...tail){
	if(debug){cerr<<head; DEBUG(tail...);}
}
/* Let's debug with Nico~Nico~Ni */
//}}}
const int N = 100000 + 5;

class BIT{
	private:
		int n;
		vector<int> arr;
		inline int lowbit(int x){return x&(-x);}
	public:
		void init(int n_){
			n = n_;
			arr.clear(); arr.resize(n);
		}
		int query(int p){
			int ret = 0;
			while(p){
				ret = max(ret, arr[p]);
				p -= lowbit(p);
			}
			return ret;
		}
		void modify(int p, int x){
			while(p < n){
				arr[p] = max(arr[p], x);
				p += lowbit(p);
			}
		}
} bit;

class LiSan{
	private:
		vector<int> vv;
	public:
		void init(){vv.clear();}
		void insert(int x){vv.PB(x);}
		void done(){
			sort(ALL(vv));
			vv.resize(distance(vv.begin(), unique(ALL(vv))));
		}
		int size(){return SZ(vv);}
		int get(int x){
			return distance(vv.begin(), lower_bound(ALL(vv), x))+1;
		}
} lisan;

int arr[N], lis[N], rlis[N];

int main(int argc, char* argv[]){
	if(argc>1 and string(argv[1])=="-D") debug=1;
	if(!debug){ios_base::sync_with_stdio(0);cin.tie(0);}
	int n, m; cin >> n >> m;
	for(int i=0;i<n;i++){
		int l, r; cin >> l >> r;
		arr[l]++; arr[r+1]--;
	}
	lisan.insert(-1);
	for(int i=1;i<=m;i++){
		arr[i] += arr[i-1];
		lisan.insert(arr[i]);
	}
	lisan.done();
	bit.init(lisan.size()+5);
	for(int i=1;i<=m;i++){
		int pre = bit.query(lisan.get(arr[i]))+1;
		lis[i] = pre;
		bit.modify(lisan.get(arr[i]), pre);
	}
	bit.init(lisan.size()+5);
	for(int i=m;i>=1;i--){
		int pre = bit.query(lisan.get(arr[i]))+1;
		rlis[i] = pre;
		bit.modify(lisan.get(arr[i]), pre);
	}
	int ans = 0;
	for(int i=0;i<=m;i++) ans = max(ans, lis[i]+rlis[i+1]);
	cout << ans << '\n';
	return 0;
}


//{{{By oToToT
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <bitset>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <random>
#include <utility>
#include <stack>
#include <sstream>
#include <functional>
#include <deque>
#include <cassert>
using namespace std;
/* include everything for Kotori~ <3 */

typedef long long lld;
typedef unsigned long long llu;
typedef long double llf;
typedef pair<int,int> PII;
typedef pair<int,lld> PIL;
typedef pair<lld,int> PLI;
typedef pair<lld,lld> PLL;
template<typename T>
using maxHeap = priority_queue<T,vector<T>,less<T>>;
template<typename T>
using minHeap = priority_queue<T,vector<T>,greater<T>>;
/* define some types for Ruby! */

#define FF first
#define SS second
#define SZ(x) (int)(x.size())
#define ALL(x) begin(x), end(x)
#define PB push_back
#define WC(x) while(x--)
/* make code shorter for Di~a~ */

template<typename Iter>
ostream& _out(ostream &s, Iter b, Iter e) {
	s<<"[";
	for ( auto it=b; it!=e; it++ ) s<<(it==b?"":" ")<<*it;
	s<<"]";
	return s;
}
template<typename A, typename B>
ostream& operator <<( ostream &s, const pair<A,B> &p ) { return s<<"("<<p.FF<<","<<p.SS<<")"; }
template<typename T>
ostream& operator <<( ostream &s, const vector<T> &c ) { return _out(s,ALL(c)); }
/* make output easier for Ainyan~n~ */

bool debug = 0;
#define DUMP(x) if(debug) cerr<<__PRETTY_FUNCTION__<<":"<<__LINE__<<" - "<<#x<<"="<<x<<'\n'
template<typename T>
void DEBUG(const T& x){if(debug) cerr<<x;}
template<typename T, typename... Args>
void DEBUG(const T& head,const Args& ...tail){
	if(debug){cerr<<head; DEBUG(tail...);}
}
/* Let's debug with Nico~Nico~Ni */
//}}}
const int N = 100000 + 5;

class BIT{
	private:
		int n;
		vector<int> arr;
		inline int lowbit(int x){return x&(-x);}
		int query(int p){
			int ret = 0;
			while(p){
				ret = max(ret, arr[p]);
				p -= lowbit(p);
			}
			return ret;
		}
	public:
		void init(int n_){
			n = n_;
			arr.clear(); arr.resize(n);
		}
		int query(int l, int r){
			return query(r) - query(l);
		}
		void modify(int p, int x){
			while(p < n){
				arr[p] = max(arr[p], x);
				p += lowbit(p);
			}
		}
} bit;

class LiSan{
	private:
		vector<int> vv;
	public:
		void init(){vv.clear();}
		void insert(int x){vv.PB(x);}
		void done(){
			sort(ALL(vv));
			vv.resize(distance(vv.begin(), unique(ALL(vv))));
		}
		int size(){return SZ(vv);}
		int get(int x){
			return distance(vv.begin(), lower_bound(ALL(vv), x))+1;
		}
} lisan;

int arr[N];

int main(int argc, char* argv[]){
	if(argc>1 and string(argv[1])=="-D") debug=1;
	if(!debug){ios_base::sync_with_stdio(0);cin.tie(0);}
	int n, m; cin >> n >> m;
	for(int i=0;i<n;i++){
		int l, r; cin >> l >> r;
		arr[l]++; arr[r+1]--;
	}
	lisan.insert(-1);
	for(int i=1;i<=m;i++){
		arr[i] += arr[i-1];
		lisan.insert(arr[i]);
	}
	lisan.done();
	int ans0 = 0, ans1 = 0;
	bit.init(lisan.size()+5);
	for(int i=1;i<=m;i++){
		int pre = bit.query(lisan.get(-1), lisan.get(arr[i]))+1;
		ans0 = max(pre, ans0);
		bit.modify(lisan.get(arr[i]), pre);
	}
	bit.init(lisan.size()+5);
	for(int i=m;i>=1;i--){
		int pre = bit.query(lisan.get(-1), lisan.get(arr[i]))+1;
		ans1 = max(pre, ans1);
		bit.modify(lisan.get(arr[i]), pre);
	}
	cout << max(ans0, ans1) << '\n';
	return 0;
}


//{{{By oToToT
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <bitset>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <random>
#include <utility>
#include <stack>
#include <sstream>
#include <functional>
#include <deque>
#include <cassert>
using namespace std;
/* include everything for Kotori~ <3 */

typedef long long lld;
typedef unsigned long long llu;
typedef long double llf;
typedef pair<int,int> PII;
typedef pair<int,lld> PIL;
typedef pair<lld,int> PLI;
typedef pair<lld,lld> PLL;
template<typename T>
using maxHeap = priority_queue<T,vector<T>,less<T>>;
template<typename T>
using minHeap = priority_queue<T,vector<T>,greater<T>>;
/* define some types for Ruby! */

#define FF first
#define SS second
#define SZ(x) (int)(x.size())
#define ALL(x) begin(x), end(x)
#define PB push_back
#define WC(x) while(x--)
/* make code shorter for Di~a~ */

template<typename Iter>
ostream& _out(ostream &s, Iter b, Iter e) {
	s<<"[";
	for ( auto it=b; it!=e; it++ ) s<<(it==b?"":" ")<<*it;
	s<<"]";
	return s;
}
template<typename A, typename B>
ostream& operator <<( ostream &s, const pair<A,B> &p ) { return s<<"("<<p.FF<<","<<p.SS<<")"; }
template<typename T>
ostream& operator <<( ostream &s, const vector<T> &c ) { return _out(s,ALL(c)); }
/* make output easier for Ainyan~n~ */

bool debug = 0;
#define DUMP(x) if(debug) cerr<<__PRETTY_FUNCTION__<<":"<<__LINE__<<" - "<<#x<<"="<<x<<'\n'
template<typename T>
void DEBUG(const T& x){if(debug) cerr<<x;}
template<typename T, typename... Args>
void DEBUG(const T& head,const Args& ...tail){
	if(debug){cerr<<head; DEBUG(tail...);}
}
/* Let's debug with Nico~Nico~Ni */
//}}}
const int N = 100000 + 5;

class BIT{
	private:
		int n;
		vector<int> arr;
		inline int lowbit(int x){return x&(-x);}
		int query(int p){
			int ret = 0;
			while(p){
				ret += arr[p];
				p -= lowbit(p);
			}
			return ret;
		}
	public:
		void init(int n_){
			n = n_;
			arr.clear(); arr.resize(n);
		}
		int query(int l, int r){
			return query(r) - query(l);
		}
		void modify(int p, int x){
			while(p < n){
				arr[p] = max(arr[p], x);
				p += lowbit(p);
			}
		}
} bit;

class LiSan{
	private:
		vector<int> vv;
	public:
		void init(){vv.clear();}
		void insert(int x){vv.PB(x);}
		void done(){
			sort(ALL(vv));
			vv.resize(distance(vv.begin(), unique(ALL(vv))));
		}
		int get(int x){
			return distance(vv.begin(), lower_bound(ALL(vv), x));
		}
} lisan;

int arr[N];

int main(int argc, char* argv[]){
	if(argc>1 and string(argv[1])=="-D") debug=1;
	if(!debug){ios_base::sync_with_stdio(0);cin.tie(0);}
	int n, m; cin >> n >> m;
	for(int i=0;i<n;i++){
		int l, r; cin >> l >> r;
		arr[l]++; arr[r+1]--;
	}
	lisan.insert(-1);
	for(int i=1;i<=m;i++){
		arr[i] += arr[i-1];
		lisan.insert(arr[i]);
	}
	lisan.done();
	int ans0 = 0, ans1 = 0;
	bit.init(m+1);
	for(int i=1;i<=m;i++){
		int pre = bit.query(lisan.get(-1), lisan.get(arr[i]))+1;
		ans0 = max(pre, ans0);
		bit.modify(lisan.get(arr[i]), pre);
	}
	bit.init(m+1);
	for(int i=m;i>=1;i--){
		int pre = bit.query(lisan.get(-1), lisan.get(arr[i]))+1;
		ans1 = max(pre, ans1);
		bit.modify(lisan.get(arr[i]), pre);
	}
	cout << max(ans0, ans1) << '\n';
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int a[111111], ls[111111], rs[111111];
vector<int> lis;

int main() {
	int n, m;
	cin >> n >> m;
	for (int i = 0, q, w; i < n; i++) {
		cin >> q >> w;
		a[q]++;
		a[w+1]--;
	}
	for (int i = 1; i <= m; i++)
		a[i] += a[i-1];
	lis.push_back(a[1]);
	ls[1] = 1;

	for (int i = 2; i <= m; i++) {
		if (a[i] >= lis.back()) {
			lis.push_back(a[i]);
		}
		else {
			int ind = upper_bound(lis.begin(), lis.end(), a[i])-lis.begin();
			lis[ind] = a[i];
		}
		ls[i] = lis.size();
	}
	lis.clear();
	lis.push_back(a[m]);
	rs[m] = 1;
	for (int i = m-1; i > 0; i--) {
		if (a[i] >= lis.back()) {
			lis.push_back(a[i]);
		}
		else {
			int ind = upper_bound(lis.begin(), lis.end(), a[i])-lis.begin();
			lis[ind] = a[i];
		}
		rs[i] = lis.size();
	}
	int res = 0;
	for (int i = 1; i <= m; i++) {
		res = max(res, rs[i]+ls[i]-1);
	}
	cout << res << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int a[111111], ls[111111], rs[111111];
vector<int> lis;

int main() {
	int n, m;
	cin >> n >> m;
	for (int i = 0, q, w; i < n; i++) {
		cin >> q >> w;
		a[q]++;
		a[w+1]--;
	}
	for (int i = 1; i <= m; i++)
		a[i] += a[i-1];
	lis.push_back(a[1]);
	ls[1] = 1;

	for (int i = 2; i <= m; i++) {
		if (a[i] >= lis.back()) {
			lis.push_back(a[i]);
		}
		else {
			int ind = upper_bound(lis.begin(), lis.end(), a[i])-lis.begin();
			lis[ind] = a[i];
		}
		ls[i] = lis.size();
	}
	lis.clear();
	lis.push_back(a[m]);
	rs[m] = 1;
	for (int i = m-1; i > 0; i--) {
		if (a[i] >= lis.back()) {
			lis.push_back(a[i]);
		}
		else {
			int ind = upper_bound(lis.begin(), lis.end(), a[i])-lis.begin();
			lis[ind] = a[i];
		}
		rs[i] = lis.size();
	}
	int res = 0;
	for (int i = 1; i <= n; i++) {
		res = max(res, rs[i]+ls[i]-1);
	}
	cout << res << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int a[111111], ls[111111], rs[111111];
vector<int> lis;

int main() {
	int n, m;
	cin >> n >> m;
	for (int i = 0, q, w; i < n; i++) {
		cin >> q >> w;
		a[q]++;
		a[w+1]--;
	}
	for (int i = 1; i <= m; i++)
		a[i] += a[i-1];
	lis.push_back(a[1]);
	ls[1] = 1;

	for (int i = 2; i <= m; i++) {
		if (a[i] >= lis.back()) {
			lis.push_back(a[i]);
		}
		else {
			int ind = upper_bound(lis.begin(), lis.end(), a[i])-lis.begin();
			lis[ind] = a[i];
		}
		ls[i] = lis.size();
	}
	lis.clear();
	lis.push_back(a[m]);
	rs[m] = 1;
	for (int i = m-1; i > 0; i--) {
		if (a[i] >= lis.back()) {
			lis.push_back(a[i]);
		}
		else {
			int ind = upper_bound(lis.begin(), lis.end(), a[i])-lis.begin();
			lis[ind] = a[i];
		}
		rs[i] = lis.size();
	}
	int res = 0;
	for (int i = 0; i < n; i++) {
		res = max(res, rs[i]+ls[i]-1);
	}
	cout << res << endl;
	return 0;
}


#include <stdio.h>
#pragma warning(disable:4996)
//FILE *in = fopen("input.txt", "r"), *out = fopen("output.txt", "w");
FILE *in = stdin, *out = stdout;
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;
#define INF 0x7fffffff
#define NM 100005
#define MOD 1000000007
#define FOR(i,n,m) for (int i=(n);i<=(m);i++)
#define si(n) fscanf(in,"%d",&n)
#define sd(n) fscanf(in,"%lld",&n)
typedef long long int ll;

int n, m, cnt[NM], B;
int tree[NM * 2], L[NM], R[NM];
void input() {
	si(n); si(m); B = n - 1;
	FOR(i, 1, n) {
		int x, y; si(x), si(y);
		cnt[x]++; cnt[y + 1]--;
	}
	FOR(i, 1, m) cnt[i] += cnt[i - 1];
	FOR(i, 1, m) cnt[i]++;
}
void update(int idx, int cnt) {
	idx += B; tree[idx] = cnt; idx >>= 1;
	while (idx) tree[idx] = max(tree[idx * 2], tree[idx * 2 + 1]), idx >>= 1;
}
int find(int L, int R) {
	L += B, R += B;
	int res = 0;
	while (L <= R) {
		if (L & 1) res = max(res, tree[L++]);
		if (!(R & 1)) res = max(res, tree[R--]);
		L >>= 1, R >>= 1;
	}
	return res;
}
void pro() {
	FOR(i, 1, m) {
		L[i] = find(1, cnt[i]) + 1;
		update(cnt[i], L[i]);
	}
	FOR(i, 1, B * 2 + 1) tree[i] = 0;
	for (int i = m; i >= 1; i--) {
		R[i] = find(1, cnt[i]) + 1;
		update(cnt[i], R[i]);
	}
	int ans = 0;
	FOR(i, 1, m) {
		ans = max(ans, L[i] + R[i] - 1);
	}
	fprintf(out, "%d", ans);
}
int main() {
	input();
	pro();
	return 0;
}


#include <stdio.h>
#pragma warning(disable:4996)
//FILE *in = fopen("input.txt", "r"), *out = fopen("output.txt", "w");
FILE *in = stdin, *out = stdout;
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;
#define INF 0x7fffffff
#define NM 100005
#define MOD 1000000007
#define FOR(i,n,m) for (int i=(n);i<=(m);i++)
#define si(n) fscanf(in,"%d",&n)
#define sd(n) fscanf(in,"%lld",&n)
typedef long long int ll;

int n, m, cnt[NM], B;
int tree[NM * 2], L[NM], R[NM];
void input() {
	si(n); si(m); B = m - 1;
	FOR(i, 1, n) {
		int x, y; si(x), si(y);
		cnt[x]++; cnt[y + 1]--;
	}
	FOR(i, 1, m) cnt[i] += cnt[i - 1];
	FOR(i, 1, m) cnt[i]++;
}
void update(int idx, int cnt) {
	idx += B; tree[idx] = cnt; idx >>= 1;
	while (idx) tree[idx] = max(tree[idx * 2], tree[idx * 2 + 1]), idx >>= 1;
}
int find(int L, int R) {
	L += B, R += B;
	int res = 0;
	while (L <= R) {
		if (L & 1) res = max(res, tree[L++]);
		if (!(R & 1)) res = max(res, tree[R--]);
		L >>= 1, R >>= 1;
	}
	return res;
}
void pro() {
	FOR(i, 1, m) {
		L[i] = find(1, cnt[i]) + 1;
		update(cnt[i], L[i]);
	}
	FOR(i, 1, B * 2 + 1) tree[i] = 0;
	for (int i = m; i >= 1; i--) {
		R[i] = find(1, cnt[i]) + 1;
		update(cnt[i], R[i]);
	}
	int ans = 0;
	FOR(i, 1, m) {
		ans = max(ans, L[i] + R[i] - 1);
	}
	fprintf(out, "%d", ans);
}
int main() {
	input();
	pro();
	return 0;
}