#include<bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<pair<int,int> > vp;
#define pb push_back
#define mp make_pair
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define MOD 1000000007
#define N 100010
#define INF 2*1e6
int start[N], val[N], endi[N], tree[4*N], dp1[N], dp2[N];
vector<pair<int,int> > pts;
void update(int low,int high,int ind,int val, int node){
	if(low == high){
		tree[node] = max(val,tree[node]);
		return;
	}
	int mid = (low + high)/2;
	if(ind <= mid)
		update(low,mid,ind,val,2*node);
	else
		update(mid+1,high,ind,val,2*node+1);
	tree[node] = max(tree[2*node], tree[2*node+1]);
}
int query(int low,int high,int begin,int endi,int node){
	if(low > endi || high < begin || begin > endi)
		return -1*INF;
	if(low >= begin && high <= endi)
		return tree[node];
	int mid = (low + high)/2;
	return max(query(low,mid,begin,endi,2*node),query(mid+1,high,begin,endi,2*node+1));
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	int n,m;
	cin>>n>>m;
	for(int i=0;i<n;i++){
		int x,y;
		cin>>x>>y;
		start[x]++;
		endi[y]++;
	}
	int cur = 0;
	for(int i=1;i<=m;i++){
		cur += start[i];
		val[i] = cur;
		cur -= endi[i];
	}
	int maxi = 0;
	for(int i=1;i<=m;i++){
		dp1[i] = max(query(0,m,1,val[i],1),0) + 1;
		update(0,m,val[i],dp1[i],1);
		maxi = max(maxi, dp1[i]);
	}
	memset(tree,0,sizeof(tree));
	for(int i=1;i<=m;i++){
		dp2[i] = max(query(0,m,val[i],m,1),0) + 1;
		update(0,m,val[i],dp2[i],1);
		maxi = max(maxi,dp2[i]);
	}
	cout<<maxi<<'\n';
}


#include<bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<pair<int,int> > vp;
#define pb push_back
#define mp make_pair
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define MOD 1000000007
#define N 100010
#define INF 2*1e6
int start[N], val[N], endi[N], tree[4*N], dp1[N], dp2[N];
vector<pair<int,int> > pts;
void update(int low,int high,int ind,int val, int node){
	if(low == high){
		tree[node] = max(val,tree[node]);
		return;
	}
	int mid = (low + high)/2;
	if(ind <= mid)
		update(low,mid,ind,val,2*node);
	else
		update(mid+1,high,ind,val,2*node+1);
	tree[node] = max(tree[2*node], tree[2*node+1]);
}
int query(int low,int high,int begin,int end,int node){
	if(low > end || high < begin || begin > end)
		return -1*INF;
	if(low >= begin && high <= end)
		return tree[node];
	int mid = (low + high)/2;
	return max(query(low,mid,begin,end,2*node),query(mid+1,high,begin,end,2*node+1));
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	int n,m;
	cin>>n>>m;
	for(int i=0;i<n;i++){
		int x,y;
		cin>>x>>y;
		start[x]++;
		endi[y]++;
	}
	int cur = 0;
	for(int i=1;i<=m;i++){
		cur += start[i];
		val[i] = cur;
		cur -= endi[i];
	}
	int maxi = 0;
	for(int i=1;i<=m;i++){
		dp1[i] = max(query(0,n,0,val[i],1),0) + 1;
		update(0,n,val[i],dp1[i],1);
	}
	memset(tree,0,sizeof(tree));
	for(int i=m;i>=1;i--){
		dp2[i] = max(query(0,n,0,val[i],1),0) + 1;
		update(0,n,val[i],dp2[i],1);
	}
	for(int i=1;i<=m;i++){
		maxi = max(maxi,dp1[i] + dp2[i] - 1);

	}
	cout<<maxi<<'\n';
}


#include<bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<pair<int,int> > vp;
#define pb push_back
#define mp make_pair
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define MOD 1000000007
#define N 100010
#define INF 2*1e6
int start[N], val[N], endi[N], tree[4*N], dp1[N], dp2[N];
vector<pair<int,int> > pts;
void update(int low,int high,int ind,int val, int node){
	if(low == high){
		tree[node] = max(val,tree[node]);
		return;
	}
	int mid = (low + high)/2;
	if(ind <= mid)
		update(low,mid,ind,val,2*node);
	else
		update(mid+1,high,ind,val,2*node+1);
	tree[node] = max(tree[2*node], tree[2*node+1]);
}
int query(int low,int high,int begin,int end,int node){
	if(low > end || high < begin || begin > end)
		return -1*INF;
	if(low >= begin && high <= end)
		return tree[node];
	int mid = (low + high)/2;
	return max(query(low,mid,begin,end,2*node),query(mid+1,high,begin,end,2*node+1));
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	int n,m;
	cin>>n>>m;
	for(int i=0;i<n;i++){
		int x,y;
		cin>>x>>y;
		start[x]++;
		endi[y]++;
	}
	int cur = 0;
	for(int i=1;i<=m;i++){
		cur += start[i];
		val[i] = cur;
		cur -= endi[i];
	}
	int maxi = 0;
	/*
	for(int i=1;i<=m;i++){
		dp1[i] = max(query(0,m,1,val[i],1),0) + 1;
		update(0,m,val[i],dp1[i],1);
		maxi = max(maxi, dp1[i]);
	}
	*/
	memset(dp1,0,sizeof(dp1));
	dp1[1] = 1;
	for(int i=2;i<=m;i++){
		int mxi = 0;
		for(int j=1;j<i;j++){
			if(val[j] <= val[i])
				mxi = max(mxi,dp1[j]);
		}
		dp1[i] = mxi + 1;
		maxi = max(maxi,dp1[i]);
	}
	dp2[1] = 1;
	for(int i=2;i<=m;i++){
		int mxi = 0;
		for(int j=1;j<i;j++){
			if(val[j] >= val[i])
				mxi = max(mxi,dp2[j]);
		}
		dp2[i] = mxi + 1;
		maxi = max(maxi,dp2[i]);
	}
	cout<<maxi<<'\n';
	/*
	memset(tree,0,sizeof(tree));

	for(int i=1;i<=m;i++){
		dp2[i] = max(query(0,m,val[i],m,1),0) + 1;
		update(0,m,val[i],dp2[i],1);
		maxi = max(maxi, dp2[i]);
	}
	*/
}


#include<bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<pair<int,int> > vp;
#define pb push_back
#define mp make_pair
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define MOD 1000000007
#define N 100010
#define INF 2*1e6
int start[N], val[N], endi[N], tree[4*N], dp1[N], dp2[N];
vector<pair<int,int> > pts;
void update(int low,int high,int ind,int val, int node){
	if(low == high){
		tree[node] = max(val,tree[node]);
		return;
	}
	int mid = (low + high)/2;
	if(ind <= mid)
		update(low,mid,ind,val,2*node);
	else
		update(mid+1,high,ind,val,2*node+1);
	tree[node] = max(tree[2*node], tree[2*node+1]);
}
int query(int low,int high,int begin,int end,int node){
	if(low > end || high < begin || begin > end)
		return -1*INF;
	if(low >= begin && high <= end)
		return tree[node];
	int mid = (low + high)/2;
	return max(query(low,mid,begin,end,2*node),query(mid+1,high,begin,end,2*node+1));
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	int n,m;
	cin>>n>>m;
	for(int i=0;i<n;i++){
		int x,y;
		cin>>x>>y;
		start[x]++;
		endi[y]++;
	}
	int cur = 0;
	for(int i=1;i<=m;i++){
		cur += start[i];
		val[i] = cur;
		cur -= endi[i];
	}
	int maxi = 0;
	for(int i=1;i<=m;i++){
		dp1[i] = max(query(0,m,0,val[i],1),0) + 1;
		update(0,m,val[i],dp1[i],1);
	}
	memset(tree,0,sizeof(tree));
	for(int i=m;i>=1;i--){
		dp2[i] = max(query(0,m,0,val[i],1),0) + 1;
		update(0,m,val[i],dp2[i],1);
	}
	for(int i=1;i<=m;i++){
		maxi = max(maxi,dp1[i] + dp2[i] - 1);
	}
	cout<<maxi<<'\n';
}


#include<bits/stdc++.h>
using namespace std;
int n;
int te[100011];
int ke[100011];
int b[100011];
int re[100011];
int se[100011];
int BIT[100011];
int lowbit(int x)
{
	return x&(-x);
}
void add(int x,int k)
{
	while(x<=n)
	{
		BIT[x]+=k;
		x+=lowbit(x);
	}
}
int get(int x)
{
	int ans=0;
	while(x>0)
	{
		ans+=BIT[x];
		x-=lowbit(x);
	}
	return ans;
}
int main()
{
	int q;
	scanf("%d %d",&q,&n);
	for(int i=0;i<q;i++)
	{
		int x,y;
		scanf("%d %d",&x,&y);
		add(y+1,-1);
		add(x,1);
	}
	for(int i = 1;i<=n;i++)
	{
		te[i]=get(i);
		re[n+1-i] = te[i];
	}
	int cc = 0;
	for (int i = 1;i<=n;i++)
	{
		if (i==1)
		{
			b[cc]=te[1];
		}
		else if (te[i]>=b[cc])
		{
			b[cc+1]=te[i];
			cc++;
		}
		else if (te[i]<b[cc])
		{
			if (te[i]>=b[0])
			{
				int cur = 1, d = log2(cc+1);
  				while (d>=0)
  				{
  					if (cur + (1<<d)<=cc&&te[i]>=b[cur + (1<<d)])
  					{
  	  					cur += 1<<d;
  					}
  					d--;
  				}
  				b[cur+1]=te[i];
			}
			else if (te[i]<b[1])
			{
				b[0]=te[i];
			}
 		}
 		ke[i]=cc;
	}
	cc = 0;
	memset(b,0,sizeof(b));
	for (int i = 1;i<=n;i++)
	{
		if (i==1)
		{
			b[cc]=re[1];
		}
		else if (re[i]>=b[cc])
		{
			b[cc+1]=re[i];
			cc++;
		}
		else if (re[i]<b[cc])
		{
			if (re[i]>=b[0])
			{
				int cur = 1, d = log2(cc+1);
  				while (d>=0)
  				{
  					if (cur + (1<<d)<=cc&&re[i]>=b[cur + (1<<d)])
  					{
  	  					cur += 1<<d;
  					}
  					d--;
  				}
  				b[cur+1]=re[i];
			}
			else if (re[i]<b[0])
			{
				b[1]=re[i];
			}
 		}
 		se[n+1-i]=cc;
	}
	int ans = 0;
	for(int i = 1;i<=n;i++)
	{
		ans = max(ans,se[i]+ke[i]+1);
	}
	printf("%d\n",ans);
  	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n;
int te[100011];
int ke[100011];
int b[100011];
int re[100011];
int se[100011];
int BIT[100011];
int lowbit(int x)
{
	return x&(-x);
}
void add(int x,int k)
{
	while(x<=n)
	{
		BIT[x]+=k;
		x+=lowbit(x);
	}
}
int get(int x)
{
	int ans=0;
	while(x>0)
	{
		ans+=BIT[x];
		x-=lowbit(x);
	}
	return ans;
}
int main()
{
	int q;
	scanf("%d %d",&q,&n);
	for(int i=0;i<q;i++)
	{
		int x,y;
		scanf("%d %d",&x,&y);
		add(y+1,-1);
		add(x,1);
	}
	for(int i = 1;i<=n;i++)
	{
		te[i]=get(i);
		re[n+1-i] = te[i];
	}
	int cc = 0;
	for (int i = 1;i<=n;i++)
	{
		if (i==1)
		{
			b[cc]=te[1];
		}
		else if (te[i]>=b[cc])
		{
			b[cc+1]=te[i];
			cc++;
		}
		else if (te[i]<b[cc])
		{
			if (te[i]>=b[1])
			{
				int cur = 1, d = log2(cc+1);
  				while (d>=0)
  				{
  					if (cur + (1<<d)<=cc&&te[i]>=b[cur + (1<<d)])
  					{
  	  					cur += 1<<d;
  					}
  					d--;
  				}
  				b[cur+1]=te[i];
			}
			else if (te[i]<b[1])
			{
				b[1]=te[i];
			}
 		}
 		ke[i]=cc;
	}
	cc = 0;
	memset(b,0,sizeof(b));
	for (int i = 1;i<=n;i++)
	{
		if (i==1)
		{
			b[cc]=re[1];
		}
		else if (re[i]>=b[cc])
		{
			b[cc+1]=re[i];
			cc++;
		}
		else if (re[i]<b[cc])
		{
			if (re[i]>=b[1])
			{
				int cur = 1, d = log2(cc+1);
  				while (d>=0)
  				{
  					if (cur + (1<<d)<=cc&&re[i]>=b[cur + (1<<d)])
  					{
  	  					cur += 1<<d;
  					}
  					d--;
  				}
  				b[cur+1]=re[i];
			}
			else if (re[i]<b[1])
			{
				b[1]=re[i];
			}
 		}
 		se[n+1-i]=cc;
	}
	int ans = 0;
	for(int i = 1;i<=n;i++)
	{
		ans = max(ans,se[i]+ke[i]+1);
	}
	printf("%d\n",ans);
  	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <iomanip>
#include <deque>
#include <cassert>
#include <ctime>
using namespace std;
#define INF 2001001001
#define MOD 1000000007
#define endl '\n'
typedef long long ll;
typedef long double ld;

int N,M;
int dp[100001][2];
int cnt[100001];
int L[100001],R[100001];
int tree[2][200001];

void update(int mode, int pos, int val){
  pos+=M+1;
  tree[mode][pos]=max(tree[mode][pos],val);
  for (pos/=2;pos>=1;pos/=2){
    tree[mode][pos]=max(tree[mode][pos*2],tree[mode][pos*2+1]);
  }
}

int query(int mode, int l, int r){
  l+=M+1;
  r+=M+1;
  int s=0;
  while (l<=r){
    if (l%2==1)//right node
      s=max(s,tree[mode][l++]);
    if (r%2==0)//left node
      s=max(s,tree[mode][r--]);
    l/=2;
    r/=2;
  }
  return s;
}

int main()
{
  //ios_base::sync_with_stdio(0);cin.tie(0);
  cin>>N>>M;
  for (int i=0;i<N;i++){
    cin>>L[i]>>R[i];
    cnt[L[i]]++;
    cnt[R[i]+1]--;
  }
  for (int i=1;i<=M;i++)
    cnt[i]+=cnt[i-1];
  for (int i=1;i<=M;i++){
    dp[i][0]=1;
    dp[i][1]=1;
  }
  int ans=0;
  for (int i=1;i<=M;i++){
    dp[i][0]=max(dp[i][0],query(0,1,cnt[i])+1);
    dp[i][1]=max(dp[i][1],query(1,cnt[i],M)+1);
    dp[i][1]=max(dp[i][1],query(0,cnt[i],M)+1);
    update(0,cnt[i],dp[i][0]);
    update(1,cnt[i],dp[i][1]);
    update(1,cnt[i],dp[i][0]);
    ans=max(ans,max(dp[i][0],dp[i][1]));
  }
  cout<<ans<<endl;
  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <iomanip>
#include <deque>
#include <cassert>
#include <ctime>
using namespace std;
#define INF 2001001001
#define MOD 1000000007
#define endl '\n'
typedef long long ll;
typedef long double ld;

int N,M;
int dp[100001][2];
int cnt[100001];
int L[100001],R[100001];
int tree[2][200101];

void update(int mode, int pos, int val){
  pos+=M+2;
  tree[mode][pos]=max(tree[mode][pos],val);
  for (pos/=2;pos>=1;pos/=2){
    tree[mode][pos]=max(tree[mode][pos*2],tree[mode][pos*2+1]);
  }
}

int query(int mode, int l, int r){
  l+=M+2;
  r+=M+2;
  int s=0;
  while (l<=r){
    if (l%2==1)//right node
      s=max(s,tree[mode][l++]);
    if (r%2==0)//left node
      s=max(s,tree[mode][r--]);
    l/=2;
    r/=2;
  }
  return s;
}

int main()
{
  //ios_base::sync_with_stdio(0);cin.tie(0);
  cin>>N>>M;
  for (int i=0;i<N;i++){
    cin>>L[i]>>R[i];
    cnt[L[i]]++;
    cnt[R[i]+1]--;
  }
  for (int i=1;i<=M;i++)
    cnt[i]+=cnt[i-1];
  for (int i=1;i<=M;i++){
    dp[i][0]=1;
    dp[i][1]=1;
  }
  int ans=0;
  for (int i=1;i<=M;i++){
    dp[i][0]=max(dp[i][0],query(0,0,cnt[i])+1);
    dp[i][1]=max(dp[i][1],query(1,cnt[i],M)+1);
    dp[i][1]=max(dp[i][1],query(0,cnt[i],M)+1);
    update(0,cnt[i],dp[i][0]);
    update(1,cnt[i],dp[i][1]);
    update(1,cnt[i],dp[i][0]);
    ans=max(ans,max(dp[i][0],dp[i][1]));
  }
  cout<<ans<<endl;
  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <iomanip>
#include <deque>
#include <cassert>
#include <ctime>
using namespace std;
#define INF 2001001001
#define MOD 1000000007
#define endl '\n'
typedef long long ll;
typedef long double ld;

int N,M;
int dp[100001][2];
int cnt[100001];
int L[100001],R[100001];
int tree[2][200101];

void update(int mode, int pos, int val){
  pos+=M+2;
  tree[mode][pos]=max(tree[mode][pos],val);
  for (pos/=2;pos>=1;pos/=2){
    tree[mode][pos]=max(tree[mode][pos*2],tree[mode][pos*2+1]);
  }
}

int query(int mode, int l, int r){
  l+=M+2;
  r+=M+2;
  int s=0;
  while (l<=r){
    if (l%2==1)//right node
      s=max(s,tree[mode][l++]);
    if (r%2==0)//left node
      s=max(s,tree[mode][r--]);
    l/=2;
    r/=2;
  }
  return s;
}

int main()
{
  //ios_base::sync_with_stdio(0);cin.tie(0);
  cin>>N>>M;
  for (int i=0;i<N;i++){
    cin>>L[i]>>R[i];
    cnt[L[i]]++;
    cnt[R[i]+1]--;
  }
  for (int i=1;i<=M;i++)
    cnt[i]+=cnt[i-1];
  for (int i=1;i<=M;i++){
    if (cnt[i]!=0){
      dp[i][0]=1;
      dp[i][1]=1;
    }
  }
  int ans=0;
  for (int i=1;i<=M;i++){
    if (cnt[i]!=0){
      dp[i][0]=max(dp[i][0],query(0,1,cnt[i])+1);
      dp[i][1]=max(dp[i][1],query(1,cnt[i],M)+1);
      dp[i][1]=max(dp[i][1],query(0,cnt[i],M)+1);
      update(0,cnt[i],dp[i][0]);
      update(1,cnt[i],dp[i][1]);
      update(1,cnt[i],dp[i][0]);
      ans=max(ans,max(dp[i][0],dp[i][1]));
    }
  }
  cout<<ans<<endl;
  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <iomanip>
#include <deque>
#include <cassert>
#include <ctime>
using namespace std;
#define INF 2001001001
#define MOD 1000000007
#define endl '\n'
typedef long long ll;
typedef long double ld;

int N,M;
int dp[100001][2];
int cnt[100001];
int L[100001],R[100001];
int tree[2][200101];

void update(int mode, int pos, int val){
  pos+=M+1;
  tree[mode][pos]=max(tree[mode][pos],val);
  for (pos/=2;pos>=1;pos/=2){
    tree[mode][pos]=max(tree[mode][pos*2],tree[mode][pos*2+1]);
  }
}

int query(int mode, int l, int r){
  l+=M+1;
  r+=M+1;
  int s=0;
  while (l<=r){
    if (l%2==1)//right node
      s=max(s,tree[mode][l++]);
    if (r%2==0)//left node
      s=max(s,tree[mode][r--]);
    l/=2;
    r/=2;
  }
  return s;
}

int main()
{
  //ios_base::sync_with_stdio(0);cin.tie(0);
  cin>>N>>M;
  for (int i=0;i<N;i++){
    cin>>L[i]>>R[i];
    cnt[L[i]]++;
    cnt[R[i]+1]--;
  }
  for (int i=1;i<=M;i++)
    cnt[i]+=cnt[i-1];
  for (int i=1;i<=M;i++){
    if (cnt[i]!=0){
      dp[i][0]=1;
      dp[i][1]=1;
    }
  }
  int ans=0;
  for (int i=1;i<=M;i++){
    if (cnt[i]!=0){
      dp[i][0]=max(dp[i][0],query(0,1,cnt[i])+1);
      dp[i][1]=max(dp[i][1],query(1,cnt[i],M)+1);
      dp[i][1]=max(dp[i][1],query(0,cnt[i],M)+1);
      update(0,cnt[i],dp[i][0]);
      update(1,cnt[i],dp[i][1]);
      update(1,cnt[i],dp[i][0]);
      ans=max(ans,max(dp[i][0],dp[i][1]));
    }
  }
  cout<<ans<<endl;
  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <iomanip>
#include <deque>
#include <cassert>
#include <ctime>
using namespace std;
#define INF 2001001001
#define MOD 1000000007
#define endl '\n'
typedef long long ll;
typedef long double ld;

int N,M;
int dp[100001][2];
int cnt[100001];
int L[100001],R[100001];
int tree[2][200001];

void update(int mode, int pos, int val){
  pos+=M+1;
  tree[mode][pos]=max(tree[mode][pos],val);
  for (pos/=2;pos>=1;pos/=2){
    tree[mode][pos]=max(tree[mode][pos*2],tree[mode][pos*2+1]);
  }
}

int query(int mode, int l, int r){
  l+=M+1;
  r+=M+1;
  int s=0;
  while (l<=r){
    if (l%2==1)//right node
      s=max(s,tree[mode][l++]);
    if (r%2==0)//left node
      s=max(s,tree[mode][r--]);
    l/=2;
    r/=2;
  }
  return s;
}

int main()
{
  //ios_base::sync_with_stdio(0);cin.tie(0);
  cin>>N>>M;
  for (int i=0;i<N;i++){
    cin>>L[i]>>R[i];
    cnt[L[i]]++;
    cnt[R[i]+1]--;
  }
  for (int i=1;i<=M;i++)
    cnt[i]+=cnt[i-1];
  for (int i=1;i<=M;i++){
    dp[i][0]=1;
    dp[i][1]=1;
  }
  int ans=0;
  for (int i=1;i<=M;i++){
    dp[i][0]=max(dp[i][0],query(0,1,cnt[i])+1);
    dp[i][1]=max(dp[i][1],query(1,cnt[i],M)+1);
    update(0,cnt[i],dp[i][0]);
    update(1,cnt[i],dp[i][1]);
    update(1,cnt[i],dp[i][0]);
    ans=max(ans,max(dp[i][0],dp[i][1]));
  }
  cout<<ans<<endl;
  return 0;
}


#pragma GCC optimize("omit-frame-pointer")
#pragma GCC optimize("unroll-loops")

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
#include <cstring>
#include <iomanip>
#include <set>
#include <sstream>
#include <ctime>

#define rust(a, b, c, d) sqrt(sqr(a - c) + sqr(b - d))
#define sqr(a) (a)*(a)
#define m_p make_pair
#define fi first
#define se second
#define pb push_back
#define fast_io ios_base::sync_with_stdio(0);cin.tie(0)

typedef long long ll;
const int MAXINT=2147483640;
const ll MAXLL=9223372036854775800LL;
const ll MAXN=1000000;
const double pi=3.1415926535897932384626433832795;
using namespace std;

vector<pair<int,int> > v;
int ans[300000];
int forw[300000];
int bck[300000];

int main()
{
// freopen("1.in", "r", stdin);
// freopen("1.out", "w", stdout);

    fast_io;
    srand(time(0));

    int n, m, a, b;

    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> a >> b;
//        v.push_back({a, 1});
//        v.push_back({b + 1, -1});
        ans[a]++;
        ans[b + 1]--;
    }

    int val = 0;
    for (int i = 1; i <= m; i++) {
        val += ans[i];
        ans[i] = val;
    }

    multiset<int> st;
    multiset<int> :: iterator t;

    for (int i = 1; i <= m; i++) {
        t = st.upper_bound(ans[i]);

        if (t == st.end()) st.insert(ans[i]);
        else {
            st.erase(t);
            st.insert(ans[i]);
        }

        forw[i] = st.size();
    }
    st.clear();

//    for (int i = 1; i <= m; i++) cout << "i=" << i << " forw=" << forw[i] << endl;

     for (int i = m; i >= 1; i--) {
        t = st.upper_bound(ans[i]);

        if (t == st.end()) st.insert(ans[i]);
        else {
            st.erase(t);
            st.insert(ans[i]);
        }

        bck[i] = st.size();
    }

    int res = 0;
    for (int i = 0; i <= m; i++) res = max(res, forw[i] + bck[i + 1]);
    cout << res << endl;

    return 0;
}


#include <bits/stdc++.h>
typedef long long LL;
using namespace std;
#define ALL(x) (x.begin(), x.end())
#define mset(a,i) memset(a,i,sizeof(a))
#define rep(i,n) for(int i = 0;i < n;i ++)

const int N = 1e5 + 5;
const LL inf = 1LL << 60;
LL a[N], dp[N];
int l[N], r[N];
int main() {
    ios :: sync_with_stdio(false);
    cout << fixed << setprecision(16);
    int n, m, x, y;
    scanf("%d%d", &n, &m);
    rep(i, n) {
        scanf("%d%d", &x, &y);
        a[x] ++;
        a[y + 1] --;
    }
    for (int i = 1; i <= m; i ++) {
        a[i] += a[i-1];
    }
    rep(i, N) dp[i] = inf;
    for (int i = 1; i <= m; i ++) {
        int k = upper_bound(dp, dp + m, a[i]) - dp;
        l[i] = k;
        dp[k] = a[i];
    }
    rep(i, N) dp[i] = inf;
    reverse(a + 1, a + 1 + m);
    for (int i = 1; i <= m; i ++) {
        int k = upper_bound(dp, dp + m, a[i]) - dp;
        r[n - i + 1] = k;
        dp[k] = a[i];
    }
    int answer = 1;
    for (int i = 1; i <= m; i ++) {
        answer = max(answer, l[i] + r[i] + 1);
    }
    printf("%d\n", answer);
    return 0;
}


#include <bits/stdc++.h>
typedef long long LL;
using namespace std;
#define ALL(x) (x.begin(), x.end())
#define mset(a,i) memset(a,i,sizeof(a))
#define rep(i,n) for(int i = 0;i < n;i ++)

const int N = 1e5 + 5;
const int inf = 1e9 + 7;
int a[N], l[N], r[N], dp[N];
int main() {
    ios :: sync_with_stdio(false);
    cout << fixed << setprecision(16);
    int n, m;
    scanf("%d%d", &n, &m);
    rep(i, n) {
        scanf("%d%d", &l[i], &r[i]);
        a[l[i]] ++;
        a[r[i] + 1] --;
    }
    for (int i = 1; i <= m; i ++) {
        a[i] += a[i-1];
    }
    rep(i, N) dp[i] = inf;
    for (int i = 1; i <= m; i ++) {
        *upper_bound(dp, dp + m, a[i]) = a[i];
    }
    int k = lower_bound(dp, dp + m, inf) - dp;
    printf("%d\n", k);
    return 0;
}


#include <bits/stdc++.h>
typedef long long LL;
using namespace std;
#define ALL(x) (x.begin(), x.end())
#define mset(a,i) memset(a,i,sizeof(a))
#define rep(i,n) for(int i = 0;i < n;i ++)

const int N = 1e5 + 5;
const LL inf = 1LL << 60;
LL a[N], dp[N];
int l[N], r[N];
int main() {
    ios :: sync_with_stdio(false);
    cout << fixed << setprecision(16);
    int n, m;
    scanf("%d%d", &n, &m);
    rep(i, n) {
        scanf("%d%d", &l[i], &r[i]);
        a[l[i]] ++;
        a[r[i] + 1] --;
    }
    for (int i = 1; i <= m; i ++) {
        a[i] += a[i-1];
    }
    rep(i, N) dp[i] = inf;
    for (int i = 1; i <= m; i ++) {
        *upper_bound(dp, dp + m, a[i]) = a[i];
    }
    int answer = lower_bound(dp, dp + m, inf) - dp;
    rep(i, N) dp[i] = inf;
    reverse(a + 1, a + 1 + m);
    for (int i = 1; i <= m; i ++) {
        *upper_bound(dp, dp + m, a[i]) = a[i];
    }
    answer = max(answer, int(lower_bound(dp, dp + m, inf) - dp));
    printf("%d\n", answer);
    return 0;
}


#include <bits/stdc++.h>
typedef long long LL;
using namespace std;
#define ALL(x) (x.begin(), x.end())
#define mset(a,i) memset(a,i,sizeof(a))
#define rep(i,n) for(int i = 0;i < n;i ++)

const int N = 1e5 + 5;
const LL inf = 1LL << 60;
LL a[N], dp[N];
int l[N], r[N];
int main() {
    ios :: sync_with_stdio(false);
    cout << fixed << setprecision(16);
    int n, m;
    scanf("%d%d", &n, &m);
    rep(i, n) {
        scanf("%d%d", &l[i], &r[i]);
        a[l[i]] ++;
        a[r[i] + 1] --;
    }
    for (int i = 1; i <= m; i ++) {
        a[i] += a[i-1];
    }
    rep(i, N) dp[i] = inf;
    for (int i = 1; i <= m; i ++) {
        *upper_bound(dp, dp + m, a[i]) = a[i];
    }
    int k = lower_bound(dp, dp + m, inf) - dp;
    printf("%d\n", k);
    return 0;
}


#include <bits/stdc++.h>
typedef long long LL;
using namespace std;
#define ALL(x) (x.begin(), x.end())
#define mset(a,i) memset(a,i,sizeof(a))
#define rep(i,n) for(int i = 0;i < n;i ++)

const int N = 1e5 + 5;
const LL inf = 1LL << 60;
LL a[N], dp[N];
int l[N], r[N];
int main() {
    ios :: sync_with_stdio(false);
    cout << fixed << setprecision(16);
    int n, m, x, y;
    scanf("%d%d", &n, &m);
    rep(i, n) {
        scanf("%d%d", &x, &y);
        a[x] ++;
        a[y + 1] --;
    }
    for (int i = 1; i <= m; i ++) {
        a[i] += a[i-1];
    }
    rep(i, N) dp[i] = inf;
    for (int i = 1; i <= m; i ++) {
        int k = upper_bound(dp, dp + m, a[i]) - dp;
        l[i] = k;
        dp[k] = a[i];
    }
    rep(i, N) dp[i] = inf;
    reverse(a + 1, a + 1 + m);
    for (int i = 1; i <= m; i ++) {
        int k = upper_bound(dp, dp + m, a[i]) - dp;
        r[m - i + 1] = k;
        dp[k] = a[i];
    }
    int answer = 1;
    for (int i = 1; i <= m; i ++) {
        answer = max(answer, l[i] + r[i] + 1);
    }
    printf("%d\n", answer);
    return 0;
}


#include <bits/stdc++.h>
typedef long long LL;
using namespace std;
#define ALL(x) (x.begin(), x.end())
#define mset(a,i) memset(a,i,sizeof(a))
#define rep(i,n) for(int i = 0;i < n;i ++)

const int N = 1e5 + 1;
int l[N], r[N];
int cnt[N];
int main() {
    ios :: sync_with_stdio(false);
    cout << fixed << setprecision(16);
	int n, m;
	scanf("%d%d", &n, &m);
	rep(i, n) {
		scanf("%d%d", l+i, r+i);
		cnt[l[i]] ++;
		cnt[r[i]+1] --;
	}
	int maxValue = -1;
	for (int i = 1; i <= m; i ++) {
		cnt[i] += cnt[i-1];
		maxValue = max(maxValue, cnt[i]);
	}
	int total = 0;
	for (int i = 1; i <= m; i ++) if (cnt[i] == maxValue) {
		int j = i + 1;
		while(j <= m && cnt[j] == maxValue) {
			j ++;
		}
		total ++;
		i = j - 1;
	}
	if (total == 1) {
		printf("%d\n", m);
	} else {
		printf("%d\n", m - 1);
	}
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m;
int a[100005];
int num[100005];
int d1[100005];
int d2[100005];
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		a[x]++;
		a[y+1]--;
	}
	for(int i=1;i<=m;i++) a[i]+=a[i-1];
	num[0]=-1e9;
	for(int i=1;i<=m;i++) num[i]=1e9;
    for(int i=1;i<=m;i++)
	{
		int pos=upper_bound(num,num+m,a[i])-num;
		d1[i]=pos;
		num[pos]=min(num[pos],a[i]);
	}
	num[0]=-1e9;
	for(int i=1;i<=m;i++) num[i]=1e9;
	for(int i=m;i>=1;i--)
	{
		int pos=upper_bound(num,num+m,a[i])-num;
		d2[i]=pos;
		num[pos]=min(num[pos],a[i]);
	}
	int ans=0;
	for(int i=1;i<=m;i++) ans=max(ans,d1[i]+d2[i]-1);
	printf("%d\n",ans);
	return 0;
}


#include <bits/stdc++.h>

#define fi first
#define se second
#define p_b push_back
#define pll pair<ll,ll>
#define endl "\n"
#define m_p make_pair
#define all(x) x.begin(),x.end()

using namespace std;
typedef long long ll;
const ll MAXN = 1123456;

template <typename T>
T sqr(T x){
    return x * x;
}

template <typename T>
void vout(T s){
    cout << s << endl;
    exit(0);
}

ll bp(ll a,ll n){
    ll res = 1;
    while(n){
        if(n % 2)res *= a;
        a *= a;
        n >>= 1;
    }
    return res;
}

vector <ll> v[MAXN],va;
string s1,s2,s3;
map <ll, ll> mp;
set <ll> s[MAXN];
char cc;
ll a, b, d, c, a1, b1, c1, i, j, n, m, k, sc, x, ans;
ll p[MAXN], N;

void update(ll x, ll val){
    for(int i = x; i <= N; i += i & -i)p[i] += val;
}

ll get(ll x){
    ll res = 0;
    for(int i = x; i > 0; i -= i & -i)res += p[i];
    return res;
}

int main(){
    ios_base :: sync_with_stdio(0);
    cin.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    cin >> m >> n;
    N = n;
    for(i = 1; i <= m; i++){
        cin >> a1 >> b1;
        update(a1, 1);
        update(b1 + 1, -1);
    }
    vector <ll> v;
    vector <ll> dp(n + 1);
    v.p_b(1e18);
    for(i = 1; i <= n; i++){
        x = get(i);
        ll l = upper_bound(all(v), x) - v.begin();
        v[l] = x;
        if(l + 1 == v.size())v.p_b(1e18);
        dp[i] = v.size() - 1;
    }
    v.clear();
    v.p_b(-1e18);
    ll ans = 0;
    for(int i = n; i > 0; i--){
        x = - get(i);
        ll l = upper_bound(all(v), x) - v.begin();
        v[l] = x;
        if(l + 1 == v.size())v.p_b(1e18);
        ans = max(ans, v.size() + dp[i] - 1);
    }
    cout << ans << "\n";
    return 0;
}


#include <bits/stdc++.h>

#define fi first
#define se second
#define p_b push_back
#define pll pair<ll,ll>
#define endl "\n"
#define m_p make_pair
#define all(x) x.begin(),x.end()

using namespace std;
typedef long long ll;
const ll MAXN = 1123456;

template <typename T>
T sqr(T x){
    return x * x;
}

template <typename T>
void vout(T s){
    cout << s << endl;
    exit(0);
}

ll bp(ll a,ll n){
    ll res = 1;
    while(n){
        if(n % 2)res *= a;
        a *= a;
        n >>= 1;
    }
    return res;
}

vector <ll> v[MAXN],va;
string s1,s2,s3;
map <ll, ll> mp;
set <ll> s[MAXN];
char cc;
ll a, b, d, c, a1, b1, c1, i, j, n, m, k, sc, x, ans;
ll p[MAXN], N;

void update(ll x, ll val){
    for(int i = x; i <= N; i += i & -i)p[i] += val;
}

ll get(ll x){
    ll res = 0;
    for(int i = x; i > 0; i -= i & -i)res += p[i];
    return res;
}

int main(){
    ios_base :: sync_with_stdio(0);
    cin.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    cin >> m >> n;
    N = n;
    for(i = 1; i <= m; i++){
        cin >> a1 >> b1;
        update(a1, 1);
        update(b1 + 1, -1);
    }
    vector <ll> v;
    v.p_b(1e18);
    for(i = 1; i <= n; i++){
        x = get(i);
        ll l = upper_bound(all(v), x) - v.begin();
        v[l] = x;
        if(l + 1 == v.size())v.p_b(1e18);
    }
    cout << v.size() - 1 << "\n";
    return 0;
}


#include <bits/stdc++.h>

#define fi first
#define se second
#define p_b push_back
#define pll pair<ll,ll>
#define m_p make_pair
#define all(x) x.begin(),x.end()

using namespace std;
typedef long long ll;
const ll MAXN = 1123456;

template <typename T>
T sqr(T x){
    return x * x;
}

template <typename T>
void vout(T s){
    cout << s << endl;
    exit(0);
}

ll bp(ll a,ll n){
    ll res = 1;
    while(n){
        if(n % 2)res *= a;
        a *= a;
        n >>= 1;
    }
    return res;
}

vector <ll> v[MAXN],va;
string s1,s2,s3;
map <ll, ll> mp;
set <ll> s[MAXN];
char cc;
ll  a1, b1, c1, i, j, n, m, k, sc, x, ans;
ll p[MAXN], N;

void update(ll x, ll val){
    for(int i = x; i <= N; i += i & -i)p[i] = max(p[i], val);
}

ll get(ll x){
    ll res = 0;
    for(int i = x; i > 0; i -= i & -i)res = max(res, p[i]);
    return res;
}

int main(){
    ios_base :: sync_with_stdio(0);
    cin.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    cin >> m >> n;
    N = m + 1;
    vector <ll> a(n + 1), b(n + 1);
    for(i = 1; i <= m; i++){
        cin >> a1 >> b1;
        a[a1]++;
        a[b1 + 1]--;
    }
    for(int i = 1; i <= n; i++){
        sc += a[i];
        b[i] = sc + 1;
    }
    vector <ll> c(n + 1);
    for(int i = 1; i <= n; i++){
        ll x = get(b[i]) + 1;
        c[i] = x;
        update(b[i], x);
    }
    //cout << 228 << endl;
    for(int i = 0; i <= N; i++)p[i] = 0;
    for(int i = n; i > 0; i--){
        ll x = get(b[i]);
        ans = max(ans, c[i] + x);
        update(b[i], x + 1);
    }
    cout << ans << "\n";
    return 0;
}


#include <bits/stdc++.h>

#define fi first
#define se second
#define p_b push_back
#define pll pair<ll,ll>
#define m_p make_pair
#define all(x) x.begin(),x.end()

using namespace std;
typedef long long ll;
const ll MAXN = 1123456;

template <typename T>
T sqr(T x){
    return x * x;
}

template <typename T>
void vout(T s){
    cout << s << endl;
    exit(0);
}

ll bp(ll a,ll n){
    ll res = 1;
    while(n){
        if(n % 2)res *= a;
        a *= a;
        n >>= 1;
    }
    return res;
}

vector <ll> v[MAXN],va;
string s1,s2,s3;
map <ll, ll> mp;
set <ll> s[MAXN];
char cc;
ll  a1, b1, c1, i, j, n, m, k, sc, x, ans;
ll p[MAXN], N;

void update(ll x, ll val){
    for(int i = x; i <= N; i += i & -i)p[i] = max(p[i], val);
}

ll get(ll x){
    ll res = 0;
    for(int i = x; i > 0; i -= i & -i)res = max(res, p[i]);
    return res;
}

int main(){
    ios_base :: sync_with_stdio(0);
    cin.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    cin >> m >> n;
    N = m + 1;
    vector <ll> a(n + 5), b(n + 1);
    for(i = 1; i <= m; i++){
        cin >> a1 >> b1;
        a[a1]++;
        a[b1 + 1]--;
    }
    for(int i = 1; i <= n; i++){
        sc += a[i];
        b[i] = sc + 1;
    }
    vector <ll> c(n + 1);
    for(int i = 1; i <= n; i++){
        //cout << i << " " << b[i] << " " << N << endl;
        ll x = get(b[i]) + 1;
        c[i] = x;
        update(b[i], x);
    }
    //cout << 228 << endl;
    for(int i = 0; i <= N; i++)p[i] = 0;
    for(int i = n; i > 0; i--){
        ll x = get(b[i]);
        ans = max(ans, c[i] + x);
        update(b[i], x + 1);
    }
    cout << ans << "\n";
    return 0;
}


#include <iostream>
#include <algorithm>
#include <utility>
#include <set>
#include <vector>

int main(int argc, const char * argv[])
{
    std::ios_base::sync_with_stdio(false);

    int n, m;
    std::cin >> n >> m;

    std::vector<int> v(m + 2);
    for (int i = 0; i < n; i ++)
    {
        int l, r;
        std::cin >> l >> r;

        v[l + 0] ++;
        v[r + 1] --;
    }

    for (int i = 1; i <= m; i ++)
        v[i] += v[i - 1];

    std::vector<int> last0(m + 1);
    for (int i = 0; i <= m; i ++)
        last0[i] = -1;

    std::vector<int> dp0(m + 1);
    for (int i = 1; i <= m; i ++)
    {
        for (int j = 0; j <= v[i]; j ++)
        {
            if (last0[j] != -1)
                dp0[i] = std::max(dp0[i], dp0[last0[j]] + 1);
            else dp0[i] = std::max(dp0[i], 1);
        }
        last0[v[i]] = i;
    }

    std::vector<int> last1(m + 1);
    for (int i = 0; i <= m; i ++)
        last1[i] = -1;

    std::vector<int> dp1(m + 1);
    for (int i = m; i >= 1; i --)
    {
        for (int j = 0; j <= v[i]; j ++)
        {
            if (last1[j] != -1)
                dp1[i] = std::max(dp1[i], dp1[last1[j]] + 1);
            else dp1[i] = std::max(dp1[i], 1);
        }
        last1[v[i]] = i;
    }

    int result = 0;
    for (int i = 1; i <= m; i ++)
        result = std::max(result, dp0[i] + dp1[i] - 1);

    std::cout << result << std::endl;

    return 0;
}


#include <iostream>
#include <algorithm>
#include <utility>
#include <set>
#include <vector>

static const int Infinity = 1e9;

static void LIS(std::vector<int> &v, std::vector<int> &result, int n)
{
    std::vector<int> dp(n + 1);

    dp[0] = -Infinity;
    for (int i = 1; i <= n; i ++)
        dp[i] = Infinity;

    for (int i = 1; i <= n; i ++)
    {
        int max_length = (int)(std::upper_bound(dp.begin(), dp.end(), v[i]) - dp.begin());
        dp[max_length] = std::min(dp[max_length], v[i]);

        result[i] = max_length;
    }
}

int main(int argc, const char * argv[])
{
    std::ios_base::sync_with_stdio(false);

    int n, m;
    std::cin >> n >> m;

    std::vector<int> v(m + 2);
    for (int i = 0; i < n; i ++)
    {
        int l, r;
        std::cin >> l >> r;

        v[l + 0] ++;
        v[r + 1] --;
    }

    for (int i = 1; i <= m; i ++)
        v[i] += v[i - 1];

    std::vector<int> result0(m + 1), result1(m + 1);
    LIS(v, result0, m);

    std::reverse(v.begin(), v.end());
    LIS(v, result1, m);

    int result = 0;
    for (int i = 1; i <= m; i ++)
        result = std::max(result, result0[i] + result1[m - i]);

    std::cout << result << std::endl;

    return 0;
}


#include <iostream>
#include <algorithm>
#include <utility>
#include <set>
#include <vector>

int main(int argc, const char * argv[])
{
    std::ios_base::sync_with_stdio(false);

    int n, m;
    std::cin >> n >> m;

    std::vector<int> v(m + 2);
    for (int i = 0; i < n; i ++)
    {
        int l, r;
        std::cin >> l >> r;

        v[l + 0] ++;
        v[r + 1] --;
    }

    for (int i = 1; i <= m; i ++)
        v[i] += v[i - 1];

    std::vector<int> last0(m + 1);
    for (int i = 0; i <= m; i ++)
        last0[i] = -1;

    std::vector<int> dp0(m + 1);
    for (int i = 1; i <= m; i ++)
    {
        for (int j = v[i]; j >= 0; j --)
        {
            if (last0[v[j]] != -1)
            {
                dp0[i] = std::max(dp0[i], dp0[last0[j]] + 1);
                break;
            }
            else dp0[i] = std::max(dp0[i], 1);
        }
        last0[v[i]] = i;
    }

    std::vector<int> last1(m + 1);
    for (int i = 0; i <= m; i ++)
        last1[i] = -1;

    std::vector<int> dp1(m + 1);
    for (int i = m; i >= 1; i --)
    {
        for (int j = v[i]; j >= 0; j --)
        {
            if (last1[j] != -1)
            {
                dp1[i] = std::max(dp1[i], dp1[last1[j]] + 1);
                break;
            }
            else dp1[i] = std::max(dp1[i], 1);
        }
        last1[v[i]] = i;
    }

    int result = 0;
    for (int i = 1; i <= m; i ++)
        result = std::max(result, dp0[i] + dp1[i] - 1);

    std::cout << result << std::endl;

    return 0;
}


#include <iostream>
#include <algorithm>
#include <utility>
#include <set>
#include <vector>

int main(int argc, const char * argv[])
{
    int n, m;
    std::cin >> n >> m;

    std::vector<int> v(m + 2);
    for (int i = 0; i < n; i ++)
    {
        int l, r;
        std::cin >> l >> r;

        v[l + 0] ++;
        v[r + 1] --;
    }

    for (int i = 1; i <= m; i ++)
        v[i] += v[i - 1];

    std::vector<int> last0(m + 1);
    for (int i = 0; i <= m; i ++)
        last0[i] = -1;

    std::vector<int> dp0(m + 1);
    for (int i = 1; i <= m; i ++)
    {
        for (int j = 0; j <= v[i]; j ++)
        {
            if (last0[j] != -1)
                dp0[i] = std::max(dp0[i], dp0[last0[j]] + 1);
            else dp0[i] = std::max(dp0[i], 1);
        }
        last0[v[i]] = i;
    }

    std::vector<int> last1(m + 1);
    for (int i = 0; i <= m; i ++)
        last1[i] = -1;

    std::vector<int> dp1(m + 1);
    for (int i = m; i >= 1; i --)
    {
        for (int j = 0; j <= v[i]; j ++)
        {
            if (last1[j] != -1)
                dp1[i] = std::max(dp1[i], dp1[last1[j]] + 1);
            else dp1[i] = std::max(dp1[i], 1);
        }
        last1[v[i]] = i;
    }

    int result = 0;
    for (int i = 1; i <= m; i ++)
        result = std::max(result, dp0[i] + dp1[i] - 1);

    std::cout << result << std::endl;

    return 0;
}


#include <iostream>
#include <algorithm>
#include <utility>
#include <set>
#include <vector>

static int solve(std::vector<int> &v, int n, int m)
{
    std::vector<int> last0(m + 1);
    for (int i = 0; i <= m; i ++)
        last0[i] = -1;

    std::vector<int> dp0(m + 1);
    for (int i = 1; i <= m; i ++)
    {
        for (int j = v[i]; j >= 0; j --)
        {
            if (last0[j] != -1)
                dp0[i] = std::max(dp0[i], dp0[last0[j]] + 1);
            else dp0[i] = std::max(dp0[i], 1);
        }
        last0[v[i]] = i;
    }

    std::vector<int> last1(m + 1);
    for (int i = 0; i <= m; i ++)
        last1[i] = -1;

    std::vector<int> dp1(m + 1);
    for (int i = m; i >= 1; i --)
    {
        for (int j = v[i]; j >= 0; j --)
        {
            if (last1[j] != -1)
                dp1[i] = std::max(dp1[i], dp1[last1[j]] + 1);
            else dp1[i] = std::max(dp1[i], 1);
        }
        last1[v[i]] = i;
    }

    int result = 0;
    for (int i = 1; i <= m; i ++)
        result = std::max(result, dp0[i] + dp1[i] - 1);

    return result;
}

int main(int argc, const char * argv[])
{
    std::ios_base::sync_with_stdio(false);

    int n, m;
    std::cin >> n >> m;

    //while (true)
    {
        std::vector<int> left(n);
        std::vector<int> right(n);

        std::vector<int> v(m + 2);
        for (int i = 0; i < n; i ++)
        {
            int l, r;
//            l = (arc4random() % m) + 1;
//            r = (arc4random() % m) + 1;
//
//            if (l > r)
//                std::swap(l, r);

            std::cin >> l >> r;

            left[i] = l;
            right[i] = r;

            v[l + 0] ++;
            v[r + 1] --;
        }

        for (int i = 1; i <= m; i ++)
            v[i] += v[i - 1];

        std::vector<int> last0(m + 1);
        for (int i = 0; i <= m; i ++)
            last0[i] = -1;

        std::vector<int> dp0(m + 1);
        for (int i = 1; i <= m; i ++)
        {
            int min = 0, max = v[i];
            while (max - min > 1)
            {
                int middle = (max + min) / 2;

                int value_mid = 1, value_max = 1;
                if (last0[max] != -1)
                    value_max = dp0[last0[max]] + 1;
                if (last0[middle] != -1)
                    value_mid = dp0[last0[middle]] + 1;

                if (value_mid > value_max)
                    max = middle;
                else min = middle;
            }

            if (last0[min] != -1)
                dp0[i] = std::max(dp0[i], dp0[last0[min]] + 1);
            else dp0[i] = std::max(dp0[i], 1);

            if (last0[max] != -1)
                dp0[i] = std::max(dp0[i], dp0[last0[max]] + 1);
            else dp0[i] = std::max(dp0[i], 1);

            last0[v[i]] = i;
        }

        std::vector<int> last1(m + 1);
        for (int i = 0; i <= m; i ++)
            last1[i] = -1;

        std::vector<int> dp1(m + 1);
        for (int i = m; i >= 1; i --)
        {
            int min = 0, max = v[i];
            while (max - min > 1)
            {
                int middle = (max + min) / 2;

                int value_mid = 1, value_max = 1;
                if (last1[max] != -1)
                    value_max = dp1[last1[max]] + 1;
                if (last1[middle] != -1)
                    value_mid = dp1[last1[middle]] + 1;

                if (value_mid > value_max)
                    max = middle;
                else min = middle;
            }

            if (last1[min] != -1)
                dp1[i] = std::max(dp1[i], dp1[last1[min]] + 1);
            else dp1[i] = std::max(dp1[i], 1);

            if (last1[max] != -1)
                dp1[i] = std::max(dp1[i], dp1[last1[max]] + 1);
            else dp1[i] = std::max(dp1[i], 1);

            last1[v[i]] = i;
        }

        int result = 0;
        for (int i = 1; i <= m; i ++)
            result = std::max(result, dp0[i] + dp1[i] - 1);

        std::cout << result << std::endl;

//        int valid = solve(v, n, m);
//        if (result != valid)
//        {
//            std::cout << "ERROR" << std::endl;
//            std::cout << valid << " " << result << std::endl;
//            std::cout << "==============" << std::endl;
//
//            for (int i = 0; i < n; i ++)
//                std::cout << left[i] << " " << right[i] << std::endl;
//
//            return 0;
//        }
    }

    return 0;
}

//5 10
//9 10
//2 4
//1 3
//5 10
//2 9


#include <iostream>
#include <algorithm>
#include <utility>
#include <set>
#include <vector>

int main(int argc, const char * argv[])
{
    std::ios_base::sync_with_stdio(false);

    int n, m;
    std::cin >> n >> m;

    std::vector<int> v(m + 2);
    for (int i = 0; i < n; i ++)
    {
        int l, r;
        std::cin >> l >> r;

        v[l + 0] ++;
        v[r + 1] --;
    }

    for (int i = 1; i <= m; i ++)
        v[i] += v[i - 1];

    std::vector<int> last0(m + 1);
    for (int i = 0; i <= m; i ++)
        last0[i] = -1;

    std::vector<int> dp0(m + 1);
    for (int i = 1; i <= m; i ++)
    {
        for (int j = v[i]; j >= 0; j --)
        {
            if (last0[j] != -1)
            {
                dp0[i] = std::max(dp0[i], dp0[last0[j]] + 1);
                break;
            }
            else dp0[i] = std::max(dp0[i], 1);
        }
        last0[v[i]] = i;
    }

    std::vector<int> last1(m + 1);
    for (int i = 0; i <= m; i ++)
        last1[i] = -1;

    std::vector<int> dp1(m + 1);
    for (int i = m; i >= 1; i --)
    {
        for (int j = v[i]; j >= 0; j --)
        {
            if (last1[j] != -1)
            {
                dp1[i] = std::max(dp1[i], dp1[last1[j]] + 1);
                break;
            }
            else dp1[i] = std::max(dp1[i], 1);
        }
        last1[v[i]] = i;
    }

    int result = 0;
    for (int i = 1; i <= m; i ++)
        result = std::max(result, dp0[i] + dp1[i] - 1);

    std::cout << result << std::endl;

    return 0;
}


#include <iostream>
#include <algorithm>
#include <utility>
#include <set>
#include <vector>

static int solve(std::vector<int> &v, int n, int m)
{
    std::vector<int> last0(m + 1);
    for (int i = 0; i <= m; i ++)
        last0[i] = -1;

    std::vector<int> dp0(m + 1);
    for (int i = 1; i <= m; i ++)
    {
        for (int j = v[i]; j >= 0; j --)
        {
            if (last0[j] != -1)
                dp0[i] = std::max(dp0[i], dp0[last0[j]] + 1);
            else dp0[i] = std::max(dp0[i], 1);
        }
        last0[v[i]] = i;
    }

    std::vector<int> last1(m + 1);
    for (int i = 0; i <= m; i ++)
        last1[i] = -1;

    std::vector<int> dp1(m + 1);
    for (int i = m; i >= 1; i --)
    {
        for (int j = v[i]; j >= 0; j --)
        {
            if (last1[j] != -1)
                dp1[i] = std::max(dp1[i], dp1[last1[j]] + 1);
            else dp1[i] = std::max(dp1[i], 1);
        }
        last1[v[i]] = i;
    }

    int result = 0;
    for (int i = 1; i <= m; i ++)
        result = std::max(result, dp0[i] + dp1[i] - 1);

    return result;
}

int main(int argc, const char * argv[])
{
    std::ios_base::sync_with_stdio(false);

    int n, m;
    std::cin >> n >> m;

    std::vector<int> v(m + 2);
    for (int i = 0; i < n; i ++)
    {
        int l, r;
        std::cin >> l >> r;

        v[l + 0] ++;
        v[r + 1] --;
    }

    for (int i = 1; i <= m; i ++)
        v[i] += v[i - 1];

    std::vector<int> last0(m + 1);
    for (int i = 0; i <= m; i ++)
        last0[i] = -1;

    std::vector<int> dp0(m + 1);
    for (int i = 1; i <= m; i ++)
    {
        for (int j = v[i]; j >= 0; j --)
        {
            if (last0[j] != -1)
                dp0[i] = std::max(dp0[i], dp0[last0[j]] + 1);
            else dp0[i] = std::max(dp0[i], 1);
        }
        last0[v[i]] = i;
    }

    std::vector<int> last1(m + 1);
    for (int i = 0; i <= m; i ++)
        last1[i] = -1;

    std::vector<int> dp1(m + 1);
    for (int i = m; i >= 1; i --)
    {
        for (int j = v[i]; j >= 0; j --)
        {
            if (last1[j] != -1)
                dp1[i] = std::max(dp1[i], dp1[last1[j]] + 1);
            else dp1[i] = std::max(dp1[i], 1);
        }
        last1[v[i]] = i;
    }

    int result = 0;
    for (int i = 1; i <= m; i ++)
        result = std::max(result, dp0[i] + dp1[i] - 1);

    std::cout << result << std::endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int bounds[100005], ans1[100005], ans2[100005];
int answer[100005];

const int INF = 1e9 + 7;

int main()
{
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; ++i) {
        int x, y;
        cin >> x >> y;
        ++bounds[x];
        --bounds[y + 1];
    }
    for(int i = 1; i <= m; i++) {
        bounds[i] += bounds[i - 1];
    }
    int len = 0, ans = 0;
    for(int i = 1; i <= m; ++i)
    {
        auto it = upper_bound(answer + 1, answer + len + 1, bounds[i]) - answer;
        ans1[i] = it;
        answer[it] = bounds[i];
        if(it > len) {
            len = it;
        }
    }
    len = 0;
    for(int i = m; i > 0; --i)
    {
        auto it = upper_bound(answer + 1, answer + len + 1, bounds[i]) - answer;
        ans2[i] = it;
        answer[it] = bounds[i];
        if(it > len) {
            len = it;
        }
    }
    for(int i = 1; i <= m; i++)
        ans = max(ans, ans1[i] + ans2[i] - 1);
    cout << ans;
    return 0;
}


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

int main (){
    int n, m;
    cin >> n >> m;
    int T [m + 1];
    for (int i = 0; i <= m; ++i)
        T [i] = 0;
    pair<int, int> P [2 * n + 1];
    int x, y;
    for (int i = 0; i < n; ++i){
        cin >> x >> y;
        P[2 * i].first = x;
        P[2 * i].second = 1;
        P[2 * i + 1].first = y + 1;
        P[2 * i + 1].second = -1;
    }
    P [2 * n].first = 2000000000;
    P [2 * n].second = 0;
    sort(P, P + 2 * n);
    int cnm = 0;
    int idx = 0;
    for (int i = 1; i <= m; ++i){
        while(P[idx].first == i){
            cnm += P[idx].second;
            idx++;
        }
        T[i] = cnm;
    }
    vector<int> ROS(m + 1), MAL(m + 1);
    vector<int> LIS(m + 1), LAS(m + 1);
    for (int i = 0; i <= m; ++i){
        ROS[i] = 2000000000;
        MAL[i] = 2000000000;
    }
    ROS[0] = MAL[0] = 0;
    for (int i = 1; i <= m; ++i){
        auto it = upper_bound(ROS.begin(), ROS.end(), T[i]);
        *it = T [i];
        LIS[i] = it - ROS.begin();
    }
    for (int i = m; i > 0; --i){
        auto it = upper_bound(MAL.begin(), MAL.end(), T[i]);
        *it = T [i];
        LAS[i] = it - MAL.begin();
    }
    int best = 0;
    for (int i = 1; i <= m; ++i)
        best = max(best, LAS[i] + LIS[i]);
    cout << best - 1 << endl;

}


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

int lbs( int arr[], int n )
{
   int i, j;

   /* Allocate memory for LIS[] and initialize LIS values as 1 for
      all indexes */
   int *lis = new int[n];
   for (i = 0; i < n; i++)
      lis[i] = 1;

   /* Compute LIS values from left to right */
   for (i = 1; i < n; i++)
      for (j = 0; j < i; j++)
         if (arr[i] > arr[j] && lis[i] < lis[j] + 1)
            lis[i] = lis[j] + 1;

   /* Allocate memory for lds and initialize LDS values for
      all indexes */
   int *lds = new int [n];
   for (i = 0; i < n; i++)
      lds[i] = 1;

   /* Compute LDS values from right to left */
   for (i = n-2; i >= 0; i--)
      for (j = n-1; j > i; j--)
         if (arr[i] > arr[j] && lds[i] < lds[j] + 1)
            lds[i] = lds[j] + 1;

   /* Return the maximum value of lis[i] + lds[i] - 1*/
   int max = lis[0] + lds[0] - 1;
   for (i = 1; i < n; i++)
     if (lis[i] + lds[i] - 1 > max)
         max = lis[i] + lds[i] - 1;
   return max;
}

int main (){
    int n, m;
    cin >> n >> m;
    int T [m + 1];
    for (int i = 0; i <= m; ++i)
        T [i] = 0;
    pair<int, int> P [2 * n + 1];
    int x, y;
    for (int i = 0; i < n; ++i){
        cin >> x >> y;
        P[2 * i].first = x;
        P[2 * i].second = 1;
        P[2 * i + 1].first = y + 1;
        P[2 * i + 1].second = -1;
    }
    P [2 * n].first = 2000000000;
    P [2 * n].second = 0;
    sort(P, P + 2 * n);
    int cnm = 0;
    int idx = 0;
    for (int i = 1; i <= m; ++i){
        while(P[idx].first == i){
            cnm += P[idx].second;
            idx++;
        }
        T[i] = cnm;
    }
    vector<int> ROS(m + 1), MAL(m + 1);
    for (int i = 0; i <= m; ++i){
        ROS[i] = 2000000000;
        MAL[i] = -2000000000;
    }
    cout << lbs(T + 1, m);

}


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

int main (){
    int n, m;
    cin >> n >> m;
    int T [m + 1];
    vector<vector<int>> maxes(m + 1);
    for (int i = 0; i <= m; ++i)
        T [i] = 0;
    pair<int, int> P [2 * n + 1];
    int x, y;
    for (int i = 0; i < n; ++i){
        cin >> x >> y;
        P[2 * i].first = x;
        P[2 * i].second = 1;
        P[2 * i + 1].first = y + 1;
        P[2 * i + 1].second = -1;
    }
    P [2 * n].first = 2000000000;
    P [2 * n].second = 0;
    sort(P, P + 2 * n);
    int cnm = 0;
    int idx = 0;
    for (int i = 1; i <= m; ++i){
        while(P[idx].first == i){
            cnm += P[idx].second;
            idx++;
        }
        T[i] = cnm;
        maxes[cnm].push_back(i);
    }
    int t = m;
    int ts = 0, ans = 0;
    while(--t){
        if (maxes[t].size() == 1){
            ans = max(ans, m - ts);
        } else if (maxes[t].size() > 1) {
            int cand = m - ts - (maxes[t][maxes[t].size() - 1] - maxes[t][0]) - 1 + maxes[t].size();
            //cout << cand << endl;
            ans = max(ans, cand);
        }
        ts += maxes[t].size();
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>
#ifdef ONLINE_JUDGE
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif
//functions
#define rep(i,b,e) for(__typeof(e) i= (b)-((b)>(e));i!=(e)-((begin)>(e));i+=1-2*((b)>(e)))
#define fd(i,x,y) for(__typeof(i) i=x;i>=y;--i)
#define fu(i,x,y) for(__typeof(i) i=x;i<=y;++i)
#define cu(i,x,y) for(__typeof(i) i=x;i<y;++i)
#define cd(i,x,y) for(__typeof(i) i=x;i>y;--i)
#define all(a) a.begin(),a.end()
#define spd() ios_base::sync_with_stdio(0),cin.tie(NULL);
//shorter terms
#define pii pair<int, int>
#define F first
#define S second
#define mp make_pair
#define mt make_tuple
#define pb push_back
//constants and types
#define MOD 1000000007
#define CON (1<<30)
#define ll long long
#define ull unsigned long long
#define bigi long double:
#define MAXN 600010
#define PI 3.141592653589793238462643383279502884
using namespace std;
int i,j,n,m,A[100005],L[100005],D[100005],t[100005],s[100005];
int mx=0;

int query(int x){
	++x;
	int res=0;
	while(x>0){
		res=max(res,t[x]);
		x&=x-1;
	}
	return res;
}

void add(int x){
	int q=query(x);
	++x;
	while(x<=m){
		t[x]=max(t[x],q+1);
		mx=max(mx,t[x]);
		x+=(x&-x);
	}
}

int solve(){
	int ans=0;
	fu(i,1,m){
		add(A[i]);
		L[i]=mx;
	}
	memset(t,0,sizeof(t));
	mx=0;
	fd(i,m,1){
		add(A[i]);
		D[i]=mx;
	}
	fu(i,0,m){ans=max(ans,D[i+1]+L[i]);}
	return ans;
}

int main(){
	scanf("%d %d",&n,&m);
	int x,y;
	cu(i,0,n){scanf("%d %d",&x,&y);
		++s[x];--s[y+1];
	}
	int val=0;
	fu(i,1,m){val+=s[i];A[i]=val;}
	printf("%d",solve());
}


#include <bits/stdc++.h>
#ifdef ONLINE_JUDGE
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif
//functions
#define rep(i,b,e) for(__typeof(e) i= (b)-((b)>(e));i!=(e)-((begin)>(e));i+=1-2*((b)>(e)))
#define fd(i,x,y) for(__typeof(i) i=x;i>=y;--i)
#define fu(i,x,y) for(__typeof(i) i=x;i<=y;++i)
#define cu(i,x,y) for(__typeof(i) i=x;i<y;++i)
#define cd(i,x,y) for(__typeof(i) i=x;i>y;--i)
#define all(a) a.begin(),a.end()
#define spd() ios_base::sync_with_stdio(0),cin.tie(NULL);
//shorter terms
#define pii pair<int, int>
#define F first
#define S second
#define mp make_pair
#define mt make_tuple
#define pb push_back
//constants and types
#define MOD 1000000007
#define CON (1<<30)
#define ll long long
#define ull unsigned long long
#define bigi long double:
#define MAXN 600010
#define PI 3.141592653589793238462643383279502884
using namespace std;
int i,j,n,m,A[100005],L[100005],D[100005],t[100005],s[100005];
int mx=0;

int query(int x){
	++x;
	int res=0;
	while(x>0){
		res=max(res,t[x]);
		x&=x-1;
	}
	return res;
}

void add(int x){
	int q=query(x);
	++x;
	while(x<=n){
		t[x]=max(t[x],q+1);
		mx=max(mx,t[x]);
		x+=(x&-x);
	}
}

int solve(){
	int ans=0;
	fu(i,1,m){
		add(A[i]);
		L[i]=mx;
	}
	memset(t,0,sizeof(t));
	mx=0;
	fd(i,m,1){
		add(A[i]);
		D[i]=mx;
	}
	fu(i,0,m){ans=max(ans,D[i+1]+L[i]);}
	return ans;
}

int main(){
	scanf("%d %d",&n,&m);
	int x,y;
	cu(i,0,n){scanf("%d %d",&x,&y);
		++s[x];--s[y+1];
	}
	int val=0;
	fu(i,1,m){val+=s[i];A[i]=val;}
	printf("%d",solve());
}


#include <bits/stdc++.h>
#ifdef ONLINE_JUDGE
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif
//functions
#define rep(i,b,e) for(__typeof(e) i= (b)-((b)>(e));i!=(e)-((begin)>(e));i+=1-2*((b)>(e)))
#define fd(i,x,y) for(__typeof(i) i=x;i>=y;--i)
#define fu(i,x,y) for(__typeof(i) i=x;i<=y;++i)
#define cu(i,x,y) for(__typeof(i) i=x;i<y;++i)
#define cd(i,x,y) for(__typeof(i) i=x;i>y;--i)
#define all(a) a.begin(),a.end()
#define spd() ios_base::sync_with_stdio(0),cin.tie(NULL);
//shorter terms
#define pii pair<int, int>
#define F first
#define S second
#define mp make_pair
#define mt make_tuple
#define pb push_back
//constants and types
#define MOD 1000000007
#define CON (1<<30)
#define ll long long
#define ull unsigned long long
#define bigi long double:
#define MAXN 600010
#define PI 3.141592653589793238462643383279502884
using namespace std;
int i,j,n,m,A[100005],L[100005],D[100005],t[100005],s[100005];

int query(int x){
	int res=0;
	while(x>0){
		res=max(res,t[x]);
		x&=x-1;
	}
	return res;
}

void add(int x){
	int q=query(x);
	while(x<=m){
		t[x]=max(t[x],q+1);
		x+=(x&-x);
	}
}

int solve(){
	int ans=0;
	fu(i,1,m){
		add(A[i]);
		L[i]=query(n);
	}
	memset(t,0,sizeof(t));
	fd(i,m,1){
		add(A[i]);
		D[i]=query(n);
	}
	fu(i,0,m){ans=max(ans,D[i+1]+L[i]);}
	return ans;
}

int main(){spd();
	cin>>n>>m;
	int x,y;
	cu(i,0,n){cin>>x>>y;
		++s[x];--s[y+1];
	}
	int val=0;
	fu(i,1,m){val+=s[i];A[i]=val;}
	cout<<solve();
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mk make_pair
#define fi first
#define se second

typedef long long ll;
typedef pair<int, int> ii;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);

const int N = 1e5 + 5;
int n, m, a[N], v[N];
int l[N], r[N], ct[N];

int seg[4*N + 1];
int A, B;

void update (int r, int i, int j) {
	if (j < A or i > A)	return;

	if (i == A and j == A) {
		seg[r]++;
	} else {
		int mid = (i + j)>>1;

		update (2*r, i, mid);
		update (2*r + 1, mid + 1, j);

		seg[r] = seg[2*r] + seg[2*r+1];
	}
}

int queryL (int r, int i, int j) {
	if (i > B)	return 0;

	if (j <= B) {
		if (i == j)
			return i;

		int mid = (i + j)>>1;

		if (!seg[2*r + 1])
			return queryL(2*r, i, mid);
		else
			return queryL(2*r + 1, mid + 1, j);
	} else {
		int mid = (i + j)>>1;

		int L = queryL (2*r, i, mid);
		int R = queryL (2*r + 1, mid + 1, j);

		return max (L, R);
	}
}

int queryR (int r, int i, int j) {
	if (j < A)	return INF;

	if (i >= A) {
		if (i == j)
			return i;

		int mid = (i + j)>>1;

		if (!seg[2*r])
			return queryR(2*r + 1, mid + 1, j);
		else
			return queryR(2*r, i, mid);
	} else {
		int mid = (i + j)>>1;

		int L = queryR (2*r, i, mid);
		int R = queryR (2*r + 1, mid + 1, j);

		return min (L, R);
	}
}

vector <int> occ[N];

int main (void) {
	ios_base::sync_with_stdio(false);

	cin >> n >> m;

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

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

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

	for (int i = 1; i <= m; i++) {
		occ[v[i]].pb(i);

		if (!ct[v[i]])
			l[v[i]] = i;

		ct[v[i]]++;
		r[v[i]] = i;
	}

	int ans = 0;
	for (int i = N - 1; i >= 0; i--) {
		if (!ct[i])
			continue;

		int res = ct[i];

		B = l[i] - 1;
		int left = queryL (1, 0, m + 1);

		A = r[i] + 1;
		int right = queryR (1, 0, m + 1);

/*		cout << i << " " << left << " " << right << endl;
		cout << l[i] << " " << r[i] << " " << ct[i] << endl;
		cout << endl;
*/
		res += l[i] - (left + 1);
		res += right - (r[i] + 1);

		for (auto it : occ[i]) {
			A = it;
			update (1, 0, m + 1);
		}

		ans = max (ans, res);
	}

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mk make_pair
#define fi first
#define se second

typedef long long ll;
typedef pair<int, int> ii;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);

const int N = 1e5 + 5;
int n, m, a[N], v[N];
int l[N], r[N], ct[N];

int seg[4*N + 1];
int A, B;

void update (int r, int i, int j) {
	if (j < A or i > A)	return;

	if (i == A and j == A) {
		seg[r]++;
	} else {
		int mid = (i + j)>>1;

		update (2*r, i, mid);
		update (2*r + 1, mid + 1, j);

		seg[r] = seg[2*r] + seg[2*r+1];
	}
}

int queryL (int r, int i, int j) {
	if (i > B)	return 0;

	if (j <= B) {
		if (i == j) {
			if (seg[r])
				return 0;
			else
				return i;
		}

		int mid = (i + j)>>1;

		if (!seg[2*r + 1])
			return queryL(2*r, i, mid);
		else
			return queryL(2*r + 1, mid + 1, j);
	} else {
		int mid = (i + j)>>1;

		int L = queryL (2*r, i, mid);
		int R = queryL (2*r + 1, mid + 1, j);

		return max (L, R);
	}
}

int queryR (int r, int i, int j) {
	if (j < A)	return INF;

	if (i >= A) {
		if (i == j) {
			if (seg[r])
				return INF;
			else
				return i;
		}

		int mid = (i + j)>>1;

		if (!seg[2*r])
			return queryR(2*r + 1, mid + 1, j);
		else
			return queryR(2*r, i, mid);
	} else {
		int mid = (i + j)>>1;

		int L = queryR (2*r, i, mid);
		int R = queryR (2*r + 1, mid + 1, j);

		return min (L, R);
	}
}

vector <int> occ[N];

int main (void) {
	ios_base::sync_with_stdio(false);

	cin >> n >> m;

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

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

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

	for (int i = 1; i <= m; i++) {
		occ[v[i]].pb(i);

		if (!ct[v[i]])
			l[v[i]] = i;

		ct[v[i]]++;
		r[v[i]] = i;
	}

	int ans = 0;
	for (int i = N - 1; i >= 0; i--) {
		if (!ct[i])
			continue;

		int res = ct[i];

		B = l[i] - 1;
		int left = queryL (1, 0, m + 1);

		A = r[i] + 1;
		int right = queryR (1, 0, m + 1);

		right = min (right, r[i] + 1);

/*		cout << i << " " << left << " " << right << endl;
		cout << l[i] << " " << r[i] << " " << ct[i] << endl;
		cout << endl;
*/
		res += l[i] - (left + 1);
		res += right - (r[i] + 1);

		for (auto it : occ[i]) {
			A = it;
			update (1, 0, m + 1);
		}

		ans = max (ans, res);
	}

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mk make_pair
#define fi first
#define se second

typedef long long ll;
typedef pair<int, int> ii;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);

const int N = 1e5 + 5;
int n, m, a[N], v[N], ve[N];
int res[2][N];

void go (int id) {

	memset (ve, INF, sizeof ve);

	for (int i = 1; i <= m; i++) {
		int j = lower_bound(ve, ve + N, v[i] + 1) - ve;

		ve[j] = v[i];

		res[id][i] = j;
	}
}

int main (void) {
	ios_base::sync_with_stdio(false);

	cin >> n >> m;

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

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

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

/*	for (int i = 1; i <= m; i++)
		cout << v[i] << " ";
	cout << endl;
*/
	go(0);
	reverse(v + 1, v + m + 1);
	go(1);
	reverse(res[1] + 1, res[1] + m + 1);
/*
	for (int i = 1; i <= m; i++)
		cout << res[0][i] << " " << res[1][i] << endl;
*/
	int ans = 0;
	for (int i = 1; i <= m; i++)
		ans = max (ans, res[0][i] + res[1][i] + 1);

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mk make_pair
#define fi first
#define se second

typedef long long ll;
typedef pair<int, int> ii;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);

const int N = 1e5 + 5;
int n, m, a[N], v[N];
int l[N], r[N], ct[N];

int seg[4*N + 1];
int A, B;

void update (int r, int i, int j) {
	if (j < A or i > A)	return;

	if (i == A and j == A) {
		seg[r]++;
	} else {
		int mid = (i + j)>>1;

		update (2*r, i, mid);
		update (2*r + 1, mid + 1, j);

		seg[r] = seg[2*r] + seg[2*r+1];
	}
}

int queryL (int r, int i, int j) {
	if (i > B)	return 0;

	if (j <= B) {
		if (i == j)
			return i;

		int mid = (i + j)>>1;

		if (!seg[2*r + 1])
			return queryL(2*r, i, mid);
		else
			return queryL(2*r + 1, mid + 1, j);
	} else {
		int mid = (i + j)>>1;

		int L = queryL (2*r, i, mid);
		int R = queryL (2*r + 1, mid + 1, j);

		return max (L, R);
	}
}

int queryR (int r, int i, int j) {
	if (j < A)	return INF;

	if (i >= A) {
		if (i == j)
			return i;

		int mid = (i + j)>>1;

		if (!seg[2*r])
			return queryR(2*r + 1, mid + 1, j);
		else
			return queryR(2*r, i, mid);
	} else {
		int mid = (i + j)>>1;

		int L = queryR (2*r, i, mid);
		int R = queryR (2*r + 1, mid + 1, j);

		return min (L, R);
	}
}

vector <int> occ[N];

int main (void) {
	ios_base::sync_with_stdio(false);

	cin >> n >> m;

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

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

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

	for (int i = 1; i <= m; i++) {
		occ[v[i]].pb(i);

		if (!ct[v[i]])
			l[v[i]] = i;

		ct[v[i]]++;
		r[v[i]] = i;
	}

	int ans = 0;
	for (int i = N - 1; i >= 0; i--) {
		int res = ct[i];

		B = l[i] - 1;
		int left = queryL (1, 0, m + 1);

		A = r[i] + 1;
		int right = queryR (1, 0, m + 1);

/*		cout << i << " " << left << " " << right << endl;
		cout << l[i] << " " << r[i] << " " << ct[i] << endl;
		cout << endl;
*/
		res += l[i] - (left + 1);
		res += right - (r[i] + 1);

		for (auto it : occ[i]) {
			A = it;
			update (1, 0, m + 1);
		}

		ans = max (ans, res);
	}

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mk make_pair
#define fi first
#define se second

typedef long long ll;
typedef pair<int, int> ii;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);

const int N = 1e5 + 5;
int n, m, a[N], v[N];
int l[N], r[N], ct[N];

int seg[4*N + 1];
int A, B;

void update (int r, int i, int j) {
	if (j < A or i > A)	return;

	if (i == A and j == A) {
		seg[r] = 1;
	} else {
		int mid = (i + j)>>1;

		update (2*r, i, mid);
		update (2*r + 1, mid + 1, j);

		seg[r] = seg[2*r] + seg[2*r+1];
	}
}

int query (int r, int i, int j) {
	if (j < A or i > B)	return 0;

	if (i >= A and j <= B) {
		return seg[r];
	} else {
		int mid = (i + j)>>1;

		int L = query (2*r, i, mid);
		int R = query (2*r + 1, mid + 1, j);

		return L + R;
	}
}

vector <int> occ[N];

int main (void) {
	ios_base::sync_with_stdio(false);

	cin >> n >> m;

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

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

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

	for (int i = 1; i <= m; i++) {
		occ[v[i]].pb(i);

		if (!ct[v[i]])
			l[v[i]] = i;

		ct[v[i]]++;
		r[v[i]] = i;
	}

	int ans = 0;
	for (int i = 0; i < N; i++) {
		if (!ct[i])
			continue;

		int res = ct[i];

		A = 0;	B = l[i] - 1;
		res += query (1, 0, N - 1);
		A = r[i] + 1;	B = N - 1;
		res += query (1, 0, N - 1);
		ans = max (ans, res);

		int ant = -1, cnt = 0, l, r;
		for (auto at : occ[i]) {
			if (ant != at - 1) {
				cnt = 0;
				l = at;
			}
			cnt++;
			r = at;

			res = cnt;
			A = 0;	B = l - 1;
			res += query (1, 0, N - 1);
			A = r + 1;	B = N - 1;
			res += query (1, 0, N - 1);

			res = max (ans, res);

			ant = at;
		}

		for (auto it : occ[i]) {
			A = it;
			update (1, 0, N - 1);
		}
	}

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mk make_pair
#define fi first
#define se second

typedef long long ll;
typedef pair<int, int> ii;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);

const int N = 1e5 + 5;
int n, m, a[N], v[N];
int l[N], r[N], ct[N];

int seg[4*N + 1];
int A, B;

void update (int r, int i, int j) {
	if (j < A or i > A)	return;

	if (i == A and j == A) {
		seg[r] = 1;
	} else {
		int mid = (i + j)>>1;

		update (2*r, i, mid);
		update (2*r + 1, mid + 1, j);

		seg[r] = seg[2*r] + seg[2*r+1];
	}
}

int query (int r, int i, int j) {
	if (j < A or i > B)	return 0;

	if (i >= A and j <= B) {
		return seg[r];
	} else {
		int mid = (i + j)>>1;

		int L = query (2*r, i, mid);
		int R = query (2*r + 1, mid + 1, j);

		return L + R;
	}
}

vector <int> occ[N];

int main (void) {
	ios_base::sync_with_stdio(false);

	cin >> n >> m;

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

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

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

	for (int i = 1; i <= m; i++) {
		occ[v[i]].pb(i);

		if (!ct[v[i]])
			l[v[i]] = i;

		ct[v[i]]++;
		r[v[i]] = i;
	}

	int ans = 0;
	for (int i = 0; i < N; i++) {
		if (!ct[i])
			continue;

		int res = ct[i];

		A = 0;	B = l[i] - 1;
		res += query (1, 0, N - 1);
		A = r[i] + 1;	B = N - 1;
		res += query (1, 0, N - 1);
		ans = max (ans, res);

		int ant = -1, cnt = 0, l, r;
		for (auto at : occ[i]) {
			if (at != ant - 1) {
				cnt = 0;
				l = at;
			}
			cnt++;
			r = at;

			res = cnt;
			A = 0;	B = l - 1;
			res += query (1, 0, N - 1);
			A = r + 1;	B = N - 1;
			res += query (1, 0, N - 1);

			res = max (ans, res);

			ant = at;
		}

		for (auto it : occ[i]) {
			A = it;
			update (1, 0, N - 1);
		}
	}

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mk make_pair
#define fi first
#define se second

typedef long long ll;
typedef pair<int, int> ii;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);

const int N = 1e5 + 5;
int n, m, a[N], v[N];
int l[N], r[N], ct[N];

int seg[4*N + 1];
int A, B;

void update (int r, int i, int j) {
	if (j < A or i > A)	return;

	if (i == A and j == A) {
		seg[r]++;
	} else {
		int mid = (i + j)>>1;

		update (2*r, i, mid);
		update (2*r + 1, mid + 1, j);

		seg[r] = seg[2*r] + seg[2*r+1];
	}
}

int queryL (int r, int i, int j) {
	if (i > B)	return 0;

	if (j <= B) {
		if (i == j)
			return i;

		int mid = (i + j)>>1;

		if (!seg[2*r + 1])
			return queryL(2*r, i, mid);
		else
			return queryL(2*r + 1, mid + 1, j);
	} else {
		int mid = (i + j)>>1;

		int L = queryL (2*r, i, mid);
		int R = queryL (2*r + 1, mid + 1, j);

		return max (L, R);
	}
}

int queryR (int r, int i, int j) {
	if (j < A)	return INF;

	if (i >= A) {
		if (i == j)
			return i;

		int mid = (i + j)>>1;

		if (!seg[2*r])
			return queryR(2*r + 1, mid + 1, j);
		else
			return queryR(2*r, i, mid);
	} else {
		int mid = (i + j)>>1;

		int L = queryR (2*r, i, mid);
		int R = queryR (2*r + 1, mid + 1, j);

		return min (L, R);
	}
}

vector <int> occ[N];

int main (void) {
	ios_base::sync_with_stdio(false);

	cin >> n >> m;

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

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

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

	for (int i = 1; i <= m; i++) {
		occ[v[i]].pb(i);

		if (!ct[v[i]])
			l[v[i]] = i;

		ct[v[i]]++;
		r[v[i]] = i;
	}

	int ans = 0;
	for (int i = 2 /*N - 1*/; i >= 0; i--) {
		int res = ct[i];

		B = l[i] - 1;
		int left = queryL (1, 0, m + 1);

		A = r[i] + 1;
		int right = queryR (1, 0, m + 1);

/*		cout << i << " " << left << " " << right << endl;
		cout << l[i] << " " << r[i] << " " << ct[i] << endl;
		cout << endl;
*/
		res += l[i] - (left + 1);
		res += right - (r[i] + 1);

		for (auto it : occ[i]) {
			A = it;
			update (1, 0, m + 1);
		}

		ans = max (ans, res);
	}

	cout << ans << endl;

	return 0;
}


#include <cstring>
#include <map>
#include <unordered_map>
#include <string>
#include <list>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstdio>
#include <iostream>
#include <set>
#include <unordered_set>
using namespace std;

#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef pair<int, int> pii;

int gi() {
    int a;
    scanf("%d", &a);
    return a;
}

ll gli() {
    ll a;
#ifndef ONLINE_JUDGE
    scanf("%lld", &a);
#else
    scanf("%I64d", &a);
#endif
    return a;
}

#define N 100000

int b[N];
int M[N];
int cl[N];
int cr[N];

void lcs(int n, int R, int *c) {
    for (int i = 0; i < n; i++) {
        int l = 1;
        int r = (i == 0 ? 0 : c[i-1]);
        while (l <= r) {
            int m = (l+r+1)/2;
            if (R ? (b[M[m-1]] <= b[i]) : (b[n-1-M[m-1]] <= b[n-1-i]))
                l = m+1;
            else
                r = m-1;
        }

        M[l-1] = i;
        if (i == 0 || l > c[i-1])
            c[i] = l;
        else
            c[i] = c[i-1];
    }
}

int main() {
    int n = gi();
    int m = gi();

    memset(b, 0, sizeof(b));
    for (int i = 0; i < n; i++) {
        b[gi()-1]++;
        int x = gi();
        if (x < N)
            b[x]--;
    }

    for (int i = 1; i < m; i++)
        b[i] += b[i-1];

    lcs(m, 1, cl);
    lcs(m, 0, cr);

    int r = MAX(cl[m-1], cr[m-1]);
    for (int i = 1; i < m; i++) {
        int a = cl[i-1] + cr[m-i-1];
        r = MAX(r, a);
    }

#ifndef ONLINE_JUDGE
    for (int i = 0; i < m; i++)
        printf("%d cl=%d cr=%d\n", i, cl[i], cr[i]);
#endif

    printf("%d\n", r);

    return 0;
}


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

const int N = (int)2e5;
int n, m;
int l_has[N], r_has[N];
int has[N];
int asc_len[N], desc_len[N];

set<int> ms[N];

bool asc( int m, int x )
{
	return ms[m].upper_bound(x) != ms[m].begin();
}

bool desc( int m, int x )
{
	return ms[m].lower_bound(x) != ms[m].end();
}

int bin( int x, bool is_asc )
{
	int l = 0;
	int r = m + 1;

	while( r - l > 1 ) {
		int m = (l + r) /2;

		if( (is_asc && asc(m, x)) || (!is_asc && desc(m, x)) )
			l = m;
		else
			r = m;
	}

	return l;
}

void solve( void )
{
	for( int i = 1; i <= m; i++ ) {
		int len = bin(has[i], true);
		ms[len + 1].insert(has[i]);
		asc_len[i] = len + 1;
	}

	for( int i = 1; i <= m; i++ )
		ms[i].clear();

	for( int i = m; i >= 1; i-- ) {
		int len = bin(has[i], false);
		ms[len + 1].insert(has[i]);
		desc_len[i] = len + 1;
	}
}

int main( void )
{
	cin >> n >> m;
	for( int i = 0; i < n; i++ ) {
		int cur_l, cur_r;

		cin >> cur_l >> cur_r;
		l_has[cur_l]++;
		r_has[cur_r]++;
	}

	int cur_cnt = 0;
	for( int i = 1; i <= m; i++ ) {
		cur_cnt += l_has[i];
		has[i] = cur_cnt;
		cur_cnt -= r_has[i];
	}

	solve();

	int ans = 0;
	for( int i = 1; i <= m; i++ ) {
		ans = max(ans, asc_len[i] + desc_len[i] - 1);
		//cout << has[i] << " " << asc_len[i] << " " << desc_len[i] << endl;
	}
	cout << ans;

	return 0;
}


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

const int N = (int)2e5;
int n, m;
int l_has[N], r_has[N];
int has[N];
int asc_len[N], desc_len[N];

set<int> ms[N];

bool test( int m, int x )
{
	return ms[m].upper_bound(x) != ms[m].begin();
}

int bin( int x )
{
	int l = 0;
	int r = m + 1;

	while( r - l > 1 ) {
		int m = (l + r) /2;

		if( test(m, x) )
			l = m;
		else
			r = m;
	}

	return l;
}

void solve( void )
{
	for( int i = 1; i <= m; i++ ) {
		int len = bin(has[i]);
		ms[len + 1].insert(has[i]);
		asc_len[i] = len + 1;
	}

	for( int i = 1; i <= m; i++ )
		ms[i].clear();

	for( int i = m; i >= 1; i-- ) {
		int len = bin(has[i]);
		ms[len + 1].insert(has[i]);
		desc_len[i] = len + 1;
	}
}

int main( void )
{
	cin >> n >> m;
	for( int i = 0; i < n; i++ ) {
		int cur_l, cur_r;

		cin >> cur_l >> cur_r;
		l_has[cur_l]++;
		r_has[cur_r]++;
	}

	int cur_cnt = 0;
	for( int i = 1; i <= m; i++ ) {
		cur_cnt += l_has[i];
		has[i] = cur_cnt;
		cur_cnt -= r_has[i];
	}

	solve();

	int ans = 0;
	for( int i = 1; i <= m; i++ ) {
		ans = max(ans, asc_len[i] + desc_len[i] - 1);
		//cout << has[i] << " " << asc_len[i] << " " << desc_len[i] << endl;
	}
	cout << ans;

	return 0;
}


/* Codeforces Round #468 (Div. 1, based on Technocup 2018 Final Round). Problem C, by Abreto<m@abreto.net>. */
#include <bits/stdc++.h>

using namespace std;

#define MAXM    100010

int n, m;
int cnt[MAXM];
int sr;
int maxo[MAXM<<2];
int dpl[MAXM], dpr[MAXM];

void build(void)
{
    memset(maxo, 0, sizeof maxo);
}

int qry(int L, int R, int o = 1, int l = 1, int r = sr)
{
    if(L <= l && r <= R)
    {
        return maxo[o];
    } else if (R < l || L > r) {
        return 0;
    } else {
        int mid = (l+r)>>1;
        return max(qry(L,R,o<<1,l,mid), qry(L,R,o<<1|1,mid+1,r));
    }
}

void upd(int pos, int v, int o = 1, int l = 1, int r = sr)
{
    if(r <= pos && pos <= l)
    {
        maxo[o] = max(maxo[o], v);
    } else {
        int mid = (l+r)>>1;
        if(pos > mid) upd(pos, v, o<<1|1, mid+1, r);
        else upd(pos, v, o<<1, l, mid);
        maxo[o] = max(maxo[o<<1], maxo[o<<1|1]);
    }
}

int main(void)
{
    int i;
    scanf("%d%d", &n, &m); sr = n+1;
    for(i = 0;i < n;++i)
    {
        int li, ri;
        scanf("%d%d", &li, &ri);
        cnt[li] += 1;
        cnt[ri+1] -= 1;
    }
    for(i = 1;i < m;++i) cnt[i+1] += cnt[i];

    build();
    for(i = 1;i <= m;++i)
    {
        dpl[i] = qry(1, cnt[i]+1) + 1;
        upd(cnt[i]+1, dpl[i]);
    }

    build();
    for(i = m;i > 0;i--)
    {
        dpr[i] = qry(1, cnt[i]+1) + 1;
        upd(cnt[i]+1, dpr[i]);
    }
    /*for(i = 1;i <= m;++i) printf("%d ", dpl[i]); puts("");
    for(i = 1;i <= m;++i) printf("%d ", dpr[i]); puts("");*/

    int ans = 0;
    for(i = 1;i <= m;++i)
        ans = max(ans, dpl[i] + dpr[i] - 1);

    printf("%d\n", ans);
    return 0;
}


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

//#define int long long
#define reps(i,s,n) for(int (i)=(s);(i)<(n);++(i))
#define rep(i,n) reps(i,0,n)
#define rept(i,n) reps(i,0,((n)+1))
#define repst(i,s,n) reps(i,s,((n)+1))
#define reprt(i,n,t) for(int (i)=(n);(i)>=(t);--(i))
#define repr(i,n) reprt(i,n,0)
#define each(itr,v) for(auto &(itr):(v))
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define mp(x,y) make_pair((x),(y))
#define fi first
#define se second
#define tmax(x,y,z) max(x,max(y,z))
#define tmin(x,y,z) min(x,min(y,z))
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define ln '\n'
#define bln(i,n) (i==n-1?'\n':' ')
#define dbg(x) cout<<#x" = "<<(x)<<ln
#define dbga(x,n) {cout<<#x" : ";for(int (i)=0;i<(n);++i){cout<<((x)[i])<<(i==((n)-1)?'\n':' ');}}

typedef long double ld;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vst;
typedef vector<bool> vb;
typedef vector<ld> vld;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vector<int> > mat;

const ll inf = (ll)1e9+10;
const ll linf = (ll)1e18+10;
const ll mod = (ll)(1e9+7);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const int ddx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[] = {1, 1, 0, -1, -1, -1, 0, 1};
const double eps = 1e-10;

struct oreno_initializer {
	oreno_initializer() {
		cin.tie(0);
		ios::sync_with_stdio(0);
	}
} oreno_initializer;

// ━━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…
// .｡.:( ^ω^)・ﾟ＋.｡.:( ^ω^)・ﾟ＋.｡.:( ^ω^)・ﾟ＋.｡.:( ^ω^)・ﾟ＋.｡.:( ^ω^)・ﾟ＋
// ・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・

int n, m, a[114514], l, r;
int dp[114514];
pii p[114514];

signed main() {
	cin >> n >> m;
	rep(i,n) {
		cin >> l >> r;
		l--, r--;
		a[l]++, a[r+1]--;
	}
	p[0] = {a[0],0};
	reps(i,1,m) {
		a[i] += a[i-1];
		p[i] = {a[i],-i};
	}
	reps(i,1,m) a[i] += i;
	// 山か谷を発見できれば嘘をついてないことがわかる
	// a[1,m]の中の最長非減少部分列 or 最長非増加部分列の長さが答え？
	fill(dp, dp+m, inf);
	rep(i,m) *lower_bound(dp, dp+m, a[i]) = a[i];
	int ans = lower_bound(dp, dp+m, inf) - dp;
	reps(i,1,m) a[i] += i;
	reverse(a, a+m);
	reps(i,1,m) a[i] += i;
	fill(dp, dp+m, inf);
	rep(i,m) *lower_bound(dp, dp+m, a[i]) = a[i];
	chmax(ans, (int)(lower_bound(dp, dp+m, inf) - dp));
	cout << ans << ln;
}


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

//#define int long long
#define reps(i,s,n) for(int (i)=(s);(i)<(n);++(i))
#define rep(i,n) reps(i,0,n)
#define rept(i,n) reps(i,0,((n)+1))
#define repst(i,s,n) reps(i,s,((n)+1))
#define reprt(i,n,t) for(int (i)=(n);(i)>=(t);--(i))
#define repr(i,n) reprt(i,n,0)
#define each(itr,v) for(auto &(itr):(v))
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define mp(x,y) make_pair((x),(y))
#define fi first
#define se second
#define tmax(x,y,z) max(x,max(y,z))
#define tmin(x,y,z) min(x,min(y,z))
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define ln '\n'
#define bln(i,n) (i==n-1?'\n':' ')
#define dbg(x) cout<<#x" = "<<(x)<<ln
#define dbga(x,n) {cout<<#x" : ";for(int (i)=0;i<(n);++i){cout<<((x)[i])<<(i==((n)-1)?'\n':' ');}}

typedef long double ld;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vst;
typedef vector<bool> vb;
typedef vector<ld> vld;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vector<int> > mat;

const ll inf = (ll)1e9+10;
const ll linf = (ll)1e18+10;
const ll mod = (ll)(1e9+7);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const int ddx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[] = {1, 1, 0, -1, -1, -1, 0, 1};
const double eps = 1e-10;

struct oreno_initializer {
	oreno_initializer() {
		cin.tie(0);
		ios::sync_with_stdio(0);
	}
} oreno_initializer;

// ━━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…
// .｡.:( ^ω^)・ﾟ＋.｡.:( ^ω^)・ﾟ＋.｡.:( ^ω^)・ﾟ＋.｡.:( ^ω^)・ﾟ＋.｡.:( ^ω^)・ﾟ＋
// ・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・

// 単調非減少と単調非増加が繋がった山の長さの最大値を求めればいい

int n, m, a[114514], l, r, dp[114514], d1[114514], d2[114514], ans;

signed main() {
	cin >> n >> m;
	repst(i,1,n) {
		cin >> l >> r;
		a[l]++, a[r+1]--;
	}
	repst(i,1,m) a[i] += a[i-1];
	fill(dp, dp+m+1, inf);
	dp[0] = -inf;
	repst(i,1,m) {
		int p = upper_bound(dp, dp+m, a[i]) - dp;
		d1[i] = p;
		chmin(dp[p], a[i]);
	}
	fill(dp, dp+m+1, inf);
	dp[0] = -inf;
	reprt(i,m,i) {
		int p = upper_bound(dp, dp+m, a[i]) - dp;
		d2[i] = p;
		chmin(dp[p], a[i]);
	}
	repst(i,1,m) chmax(ans, d1[i]+d2[i]-1);
	cout << ans << ln;
}


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

//#define int long long
#define reps(i,s,n) for(int (i)=(s);(i)<(n);++(i))
#define rep(i,n) reps(i,0,n)
#define rept(i,n) reps(i,0,((n)+1))
#define repst(i,s,n) reps(i,s,((n)+1))
#define reprt(i,n,t) for(int (i)=(n);(i)>=(t);--(i))
#define repr(i,n) reprt(i,n,0)
#define each(itr,v) for(auto &(itr):(v))
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define mp(x,y) make_pair((x),(y))
#define fi first
#define se second
#define tmax(x,y,z) max(x,max(y,z))
#define tmin(x,y,z) min(x,min(y,z))
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define ln '\n'
#define bln(i,n) (i==n-1?'\n':' ')
#define dbg(x) cout<<#x" = "<<(x)<<ln
#define dbga(x,n) {cout<<#x" : ";for(int (i)=0;i<(n);++i){cout<<((x)[i])<<(i==((n)-1)?'\n':' ');}}

typedef long double ld;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vst;
typedef vector<bool> vb;
typedef vector<ld> vld;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vector<int> > mat;

const ll inf = (ll)1e9+10;
const ll linf = (ll)1e18+10;
const ll mod = (ll)(1e9+7);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const int ddx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[] = {1, 1, 0, -1, -1, -1, 0, 1};
const double eps = 1e-10;

struct oreno_initializer {
	oreno_initializer() {
		cin.tie(0);
		ios::sync_with_stdio(0);
	}
} oreno_initializer;

// ━━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…
// .｡.:( ^ω^)・ﾟ＋.｡.:( ^ω^)・ﾟ＋.｡.:( ^ω^)・ﾟ＋.｡.:( ^ω^)・ﾟ＋.｡.:( ^ω^)・ﾟ＋
// ・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・

int n, m, a[114514], l, r;
pii dp[114514];
pii p[114514];

signed main() {
	cin >> n >> m;
	rep(i,n) {
		cin >> l >> r;
		l--, r--;
		a[l]++, a[r+1]--;
	}
	p[0] = {a[0],0};
	reps(i,1,m) {
		a[i] += a[i-1];
		p[i] = {a[i],-i};
	}
	// 山か谷を発見できれば嘘をついてないことがわかる
	// a[1,m]の中の最長非減少部分列 or 最長非増加部分列の長さが答え？
	rep(i,m) dp[i] = {inf,inf};
	rep(i,m) *lower_bound(dp, dp+m, mp(p[i].fi, -p[i].se)) = p[i];
	pii tmp = {inf, inf};
	cout << lower_bound(dp, dp+m, tmp) - dp << endl;
}


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

//#define int long long
#define reps(i,s,n) for(int (i)=(s);(i)<(n);++(i))
#define rep(i,n) reps(i,0,n)
#define rept(i,n) reps(i,0,((n)+1))
#define repst(i,s,n) reps(i,s,((n)+1))
#define reprt(i,n,t) for(int (i)=(n);(i)>=(t);--(i))
#define repr(i,n) reprt(i,n,0)
#define each(itr,v) for(auto &(itr):(v))
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define mp(x,y) make_pair((x),(y))
#define fi first
#define se second
#define tmax(x,y,z) max(x,max(y,z))
#define tmin(x,y,z) min(x,min(y,z))
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define ln '\n'
#define bln(i,n) (i==n-1?'\n':' ')
#define dbg(x) cout<<#x" = "<<(x)<<ln
#define dbga(x,n) {cout<<#x" : ";for(int (i)=0;i<(n);++i){cout<<((x)[i])<<(i==((n)-1)?'\n':' ');}}

typedef long double ld;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vst;
typedef vector<bool> vb;
typedef vector<ld> vld;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vector<int> > mat;

const ll inf = (ll)1e9+10;
const ll linf = (ll)1e18+10;
const ll mod = (ll)(1e9+7);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const int ddx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[] = {1, 1, 0, -1, -1, -1, 0, 1};
const double eps = 1e-10;

struct oreno_initializer {
	oreno_initializer() {
		cin.tie(0);
		ios::sync_with_stdio(0);
	}
} oreno_initializer;

// ━━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…
// .｡.:( ^ω^)・ﾟ＋.｡.:( ^ω^)・ﾟ＋.｡.:( ^ω^)・ﾟ＋.｡.:( ^ω^)・ﾟ＋.｡.:( ^ω^)・ﾟ＋
// ・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・

// 単調非減少と単調非増加が繋がった山の長さの最大値を求めればいい

int n, m, a[114514], l, r, dp[114514], d1[114514], d2[114514], ans;

signed main() {
	cin >> n >> m;
	repst(i,1,n) {
		cin >> l >> r;
		a[l]++, a[r+1]--;
	}
	repst(i,1,m) a[i] += a[i-1];
	fill(dp, dp+m+1, inf);
	dp[0] = -inf;
	repst(i,1,m) {
		int p = upper_bound(dp, dp+m, a[i]) - dp;
		d1[i] = p;
		chmin(dp[p], a[i]);
	}
	fill(dp, dp+m+1, inf);
	dp[0] = -inf;
	reprt(i,m,1) {
		int p = upper_bound(dp, dp+m, a[i]) - dp;
		d2[i] = p;
		chmin(dp[p], a[i]);
	}
	repst(i,1,m) chmax(ans, d1[i]+d2[i]-1);
	cout << ans << ln;
}


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

#define forn(i,n) for(int i=0;i<int(n);i++)
#define forsn(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define dforsn(i,s,n) for(int i=(int)(n-1);i>=int(s);i--)
#define si(a) int((a).size())
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define all(a) a.begin(),a.end()
#define fastio ios_base::sync_with_stdio(false); cin.tie(0)
#define endl '\n'
#ifdef LOCAL
    #define DBG(a) cerr << #a << "=" << a << endl
    #define RAYA cerr << "----------" << endl
#else
    #define DBG(a)
    #define RAYA
#endif
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long int tint;
typedef vector<pii> vii;

const int INF = 1e9;

vi lis(vi a) {//O(nlogn)
    int N = si(a);
    vi d(N+1, INF), v;

	forn(i, N) {
		int j = upper_bound(all(d), a[i])-d.begin();

		if (a[i] < d[j])
			d[j] = a[i];

        v.pb(lower_bound(all(d), INF)-d.begin());
	}

    return v;
}

int main() {
    fastio;

    vii events;
    int n, m;
    cin >> n >> m;

    forn(i, n) {
        int l, r;
        cin >> l >> r;
        events.eb(l, i+1);
        events.eb(r+1, -(i+1));
    }
    sort(all(events));

    vi v;
    int i = 0, cnt = 0;
    while (i < 2*n) {
        int pos, ind;

        do {
            pos = events[i].first, ind = events[i].second;
            //cerr << pos << ' ' << ind << endl;

            if (ind > 0) cnt++;
            else cnt--;

            i++;
        }
        while (i < 2*n && events[i].first == pos);
        //cerr << pos << ' ' << cnt << endl;
        //cerr << endl;

        int next = i < 2*n ? events[i].first : m+1;
        while (pos < next)
            v.pb(cnt), pos++;
    }

    /*
    forn(i, si(v)) cerr << v[i] << ' ';
    cerr << endl;
    */

    vi a = lis(v);
    reverse(all(v));
    vi b = lis(v);

    int ans = 0;
    forn(i, m-1) {
        //cerr << a[i] << ' ' << b[m-2-i] << endl;
        ans = max(ans, a[i] + b[m-2-i]);
    }
    cout << ans << endl;

    return 0;
}


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

#define forn(i,n) for(int i=0;i<int(n);i++)
#define forsn(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define dforsn(i,s,n) for(int i=(int)(n-1);i>=int(s);i--)
#define si(a) int((a).size())
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define all(a) a.begin(),a.end()
#define fastio ios_base::sync_with_stdio(false); cin.tie(0)
#define endl '\n'
#ifdef LOCAL
    #define DBG(a) cerr << #a << "=" << a << endl
    #define RAYA cerr << "----------" << endl
#else
    #define DBG(a)
    #define RAYA
#endif
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long int tint;
typedef vector<pii> vii;

const int INF = 1e9;

vi lis(vi a) {//O(nlogn)
    int N = si(a);
    vi d(N+1, INF), v;

	forn(i, N) {
		int j = upper_bound(all(d), a[i])-d.begin();

		if (a[i] < d[j])
			d[j] = a[i];

        v.pb(lower_bound(all(d), INF)-d.begin());
	}

    return v;
}

int main() {
    fastio;

    int n, m;
    cin >> n >> m;

    vi events(m+1);

    forn(i, n) {
        int l, r;
        cin >> l >> r;
        l--; r--;
        events[l]++;
        events[r+1]--;
    }

    int cnt = 0;
    vi v(m);

    forn(i, m) {
        cnt += events[i];
        v[i] = cnt;
    }

    vi a = lis(v);
    reverse(all(v));
    vi b = lis(v);

    int ans = 0;
    forn(i, m-1) {
        ans = max(ans, a[i] + b[m-2-i]);
    }
    cout << ans << endl;

    return 0;
}


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

#define forn(i,n) for(int i=0;i<int(n);i++)
#define forsn(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define dforsn(i,s,n) for(int i=(int)(n-1);i>=int(s);i--)
#define si(a) int((a).size())
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define all(a) a.begin(),a.end()
#define fastio ios_base::sync_with_stdio(false); cin.tie(0)
#define endl '\n'
#ifdef LOCAL
    #define DBG(a) cerr << #a << "=" << a << endl
    #define RAYA cerr << "----------" << endl
#else
    #define DBG(a)
    #define RAYA
#endif
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long int tint;
typedef vector<pii> vii;

const int INF = 1e9;

vi lis(vi a) {//O(nlogn)
    int N = si(a);
    vi d(N+1, INF), v;

	forn(i, N) {
		int j = upper_bound(all(d), a[i])-d.begin();

		if (a[i] < d[j])
			d[j] = a[i];

        v.pb(lower_bound(all(d), INF)-d.begin());
	}

    return v;
}

int main() {
    fastio;

    vii events;
    int n, m;
    cin >> n >> m;

    forn(i, n) {
        int l, r;
        cin >> l >> r;
        events.eb(l, i+1);
        events.eb(r+1, -(i+1));
    }
    sort(all(events));

    vi v;
    int i = 0, cnt = 0;
    while (i < 2*n) {
        int pos, ind;

        do {
            pos = events[i].first, ind = events[i].second;
            //cerr << pos << ' ' << ind << endl;

            if (ind > 0) cnt++;
            else cnt--;

            i++;
        }
        while (i < 2*n && events[i].first == pos);
        //cerr << pos << ' ' << cnt << endl;
        //cerr << endl;

        int next = i < 2*n ? events[i].first : m;
        while (pos < next)
            v.pb(cnt), pos++;
    }

    /*
    forn(i, si(v)) cerr << v[i] << ' ';
    cerr << endl;
    */

    vi a = lis(v);
    reverse(all(v));
    vi b = lis(v);

    int ans = 0;
    forn(i, m-1) {
        //cerr << a[i] << ' ' << b[m-2-i] << endl;
        ans = max(ans, a[i] + b[m-2-i]);
    }
    cout << ans << endl;

    return 0;
}


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

#define forn(i,n) for(int i=0;i<int(n);i++)
#define forsn(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define dforsn(i,s,n) for(int i=(int)(n-1);i>=int(s);i--)
#define si(a) int((a).size())
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define all(a) a.begin(),a.end()
#define fastio ios_base::sync_with_stdio(false); cin.tie(0)
#define endl '\n'
#ifdef LOCAL
    #define DBG(a) cerr << #a << "=" << a << endl
    #define RAYA cerr << "----------" << endl
#else
    #define DBG(a)
    #define RAYA
#endif
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long int tint;
typedef vector<pii> vii;

const int INF = 1e9;

vi lis(vi a) {//O(nlogn)
    int N = si(a);
    vi d(N+1, INF), v;

	forn(i, N) {
		int j = upper_bound(all(d), a[i])-d.begin();

		if (a[i] < d[j])
			d[j] = a[i];

        v.pb(lower_bound(all(d), INF)-d.begin());
	}

    return v;
}

int main() {
    fastio;

    vii events;
    int n, m;
    cin >> n >> m;

    forn(i, n) {
        int l, r;
        cin >> l >> r;
        events.eb(l, i+1);
        events.eb(r+1, -(i+1));
    }
    sort(all(events));

    vi v;
    int i = 0, cnt = 0;
    while (i < 2*n) {
        int pos, ind;

        do {
            pos = events[i].first, ind = events[i].second;
            //cerr << pos << ' ' << ind << endl;

            if (ind > 0) cnt++;
            else cnt--;

            i++;
        }
        while (i < 2*n && events[i].first == pos);
        //cerr << pos << ' ' << cnt << endl;
        //cerr << endl;

        int next = i < 2*n ? events[i].first : m;
        while (pos < next)
            v.pb(cnt), pos++;
    }

    /*
    forn(i, si(v)) cerr << v[i] << ' ';
    cerr << endl;
    */

    vi a = lis(v);
    reverse(all(v));
    vi b = lis(v);

    int ans = 0;
    forn(i, m-2) {
        //cerr << a[i] << ' ' << b[m-2-i] << endl;
        ans = max(ans, a[i] + b[m-2-i]);
    }
    cout << ans << endl;

    return 0;
}


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

#define fi(i,a,b)    for(long long i=a;i<=b;i++)
#define fr(i,a)      for(long long i=0;i<a;i++)
#define fd(i,a,b)    for(long long i=b;i>=a;i--)
#define clr(x)       memset(x,0,sizeof(x))
#define cln(x)       memset(x,-1,sizeof(x))
#define __           printf(" ")
#define _            printf("\n")
#define _o           printf("1\n")
#define stree        long long lft=node<<1,rht=(node<<1)|1,mid=(s+e)>>1
#define mod          1000000007
#define read()       freopen("in.txt","r",stdin)
#define write()      freopen("out.txt","w",stdout)
#define sfl(x)       scanf("%I64d",&x)
#define sfll(x,y)    scanf("%I64d %I64d",&x,&y)
#define sflll(x,y,z) scanf("%I64d %I64d %I64d",&x,&y,&z)
#define pfl(x)       printf("%I64d",x)
#define pfll(x,y)    printf("%I64d %I64d",x,y)
#define pflll(x,y,z) printf("%I64d %I64d %I64d",x,y,z)
#define xx           100000

void incr(long long a[],long long len,long long x[]){
    vector<long long>vec;
    long long sz=0;
    fr(i,len){
        long long ix=upper_bound(vec.begin(),vec.end(),a[i])-vec.begin();
        x[i]=ix+1;
        if(ix==sz) vec.push_back(a[i]),sz++;
        else vec[ix]=a[i];
    }
    return;
}

long long mp[xx+100];
long long p[xx+100],q[xx+100],b[xx+100];

main(){
    long long n,m,s,e,cnt=0;
    clr(mp);
    sfll(n,m);
    while(n--){
        sfll(s,e);
        mp[s]++,mp[e+1]--;
    }
    fi(i,1,m){
       cnt+=mp[i];
       b[i-1]=cnt;
    }

    incr(b,m,p);
    reverse(b,b+m);
    incr(b,m,q);
    reverse(q,q+m);
    long long ans=0;
    fr(i,m) ans=max(ans,p[i]+q[i]-1);
    cout<<ans<<endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long i,j,n,m,a,b,sak[100005],bei[100005],sum1[100005],sum2[100005],dp[100005][2],liel,k,rez[100005][2],li[100005][2],rr;
long long bins(long long l,long long r,long long y,long long p)
{
    long long c=r-(r-l)/2;
    if (l<r)
    {
        if (dp[c][p]<=y)
        {
            return bins(c,r,y,p);
        }
        else
        {
            return bins(l,c-1,y,p);
        }
    }
    else
    {
        if (dp[l][p]>y)
        {
            return 0;
        }
        else
        {
            return l;
        }
    }
}
long long bins2(long long l,long long r,long long y,long long p)
{
    long long c=r-(r-l)/2;
    if (l<r)
    {
        if (dp[c][p]<=y)
        {
            return bins2(c,r,y,p);
        }
        else
        {
            return bins2(l,c-1,y,p);
        }
    }
    else
    {
        if (dp[l][p]>y)
        {
            return 0;
        }
        else
        {
            return l;
        }
    }
}
int main()
{
    std::cin.sync_with_stdio(false);
    std::cin.tie(0);
    cin >> n >> m;
    i=0;
    while (i<n)
    {
        cin >> a >> b;
        sak[a]++;
        bei[b]++;
        i++;
    }
    i=2;
    sum1[1]=sak[1];
    while (i<=m)
    {
        sum1[i]=sum1[i-1]+sak[i]-bei[i-1];
        i++;
    }
    i=m-1;
    sum2[m]=bei[m];
    while (i>0)
    {
        sum2[i]=sum2[i+1]+bei[i]-sak[i+1];
        i--;
    }
    dp[1][0]=sum1[1];
    liel=1;
    i=2;
    while (i<=m)
    {
        k=bins(1,liel,sum1[i],0)+1;
        dp[k][0]=sum1[i];
        rez[i][0]=k;
        liel=max(liel,k);
        i++;
    }
    rez[1][0]=1;
    dp[m][1]=sum2[m];
    rez[m][1]=1;
    liel=1;
    k=0;
    i=m-1;
    while (i>=1)
    {
        k=bins2(1,liel,sum2[i],1)+1;
        dp[k][1]=sum2[i];
        rez[i][1]=k;
        liel=max(liel,k);
        i--;
    }
    i=1;
    while (i<=m)
    {
        li[i][0]=max(li[i-1][0],rez[i][0]);
        i++;
    }
    i=m;
    while (i>=1)
    {
        li[i][1]=max(li[i+1][1],rez[i][1]);
        i--;
    }
    i=1;
    rr=0;
    while (i<=m)
    {
        //cout << rez[i][0] << " " << rez[i][1] << " " << li[i][0] << " " << li[i][1] << "\n";
        rr=max(rr,li[i][0]+li[i][1]-1);
        i++;
    }
    cout << rr;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int i,j,n,m,a,b,sak[100005],bei[100005],rez;
int main()
{
    std::cin.sync_with_stdio(false);
    std::cin.tie(0);
    cin >> n >> m;
    i=0;
    while (i<n)
    {
        cin >> a >> b;
        sak[a]++;
        bei[b]++;
        i++;
    }
    i=2;
    while (i<m-1)
    {
        //cout << i << " " << bei[i-1] << " " << sak[i] << " " << bei[i] << " " << sak[i+1] << "\n";
        if (bei[i-1]!=sak[i]&&bei[i]!=sak[i+1])
        {
            rez++;
        }
        i++;
    }
    cout << m-rez;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long i,j,n,m,a,b,sak[100005],bei[100005],sum1[100005],sum2[100005],dp[100005][2],liel,k,rez[100005][2],li[100005][2],rr;
long long bins(long long l,long long r,long long y,long long p)
{
    long long c=r-(r-l)/2;
    if (l<r)
    {
        if (dp[c][p]<=y)
        {
            return bins(c,r,y,p);
        }
        else
        {
            return bins(l,c-1,y,p);
        }
    }
    else
    {
        if (dp[l][p]>y)
        {
            return 0;
        }
        else
        {
            return l;
        }
    }
}
long long bins2(long long l,long long r,long long y,long long p)
{
    long long c=r-(r-l)/2;
    if (l<r)
    {
        if (dp[c][p]>=y)
        {
            return bins2(c,r,y,p);
        }
        else
        {
            return bins2(l,c-1,y,p);
        }
    }
    else
    {
        if (dp[l][p]>y)
        {
            return 0;
        }
        else
        {
            return l;
        }
    }
}
int main()
{
    std::cin.sync_with_stdio(false);
    std::cin.tie(0);
    cin >> n >> m;
    i=0;
    while (i<n)
    {
        cin >> a >> b;
        sak[a]++;
        bei[b]++;
        i++;
    }
    i=2;
    sum1[1]=sak[1];
    while (i<=m)
    {
        sum1[i]=sum1[i-1]+sak[i]-bei[i-1];
        i++;
    }
    i=m-1;
    sum2[m]=bei[m];
    while (i>0)
    {
        sum2[i]=sum2[i+1]+bei[i]-sak[i+1];
        i--;
    }
    dp[1][0]=sum1[1];
    liel=1;
    i=2;
    while (i<=m)
    {
        k=bins(1,liel,sum1[i],0)+1;
        dp[k][0]=sum1[i];
        rez[i][0]=k;
        liel=max(liel,k);
        i++;
    }
    rez[1][0]=1;
    dp[m][1]=sum2[m];
    rez[m][1]=1;
    liel=1;
    k=0;
    i=m-1;
    while (i>=1)
    {
        k=bins2(1,liel,sum2[i],1)+1;
        dp[k][1]=sum2[i];
        rez[i][1]=k;
        liel=max(liel,k);
        i--;
    }
    i=1;
    while (i<=m)
    {
        li[i][0]=max(li[i-1][0],rez[i][0]);
        i++;
    }
    i=m;
    while (i>=1)
    {
        li[i][1]=max(li[i+1][1],rez[i][1]);
        i--;
    }
    i=1;
    rr=0;
    while (i<=m-1)
    {
        //cout << li[i][0] << " " << li[i][1] << "\n";
        rr=max(rr,li[i][0]+li[i+1][1]);
        i++;
    }
    cout << rr;
    return 0;
}