#include <bits/stdc++.h>
using namespace std;
typedef double db;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
#define dd(x) cout << #x << " = " << x << ", "
#define de(x) cout << #x << " = " << x << endl
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define per(i,a,b) for(int i=(b)-1;i>=(a);--i)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
//INF = 1 061 109 567 ; INF + INF = 2 122 219 134 ;
//LINF = 4 557 430 888 798 830 399 ; LINF + LINF = 9 114 861 777 597 660 798 ;
const int INF = 0x3f3f3f3f;     // -INF: 0xc0   abs(-INF) = INF + 1
const ll LINF = 0x3f3f3f3f3f3f3f3f;
/*}}}*/                         // zr, zm
const int MOD = (int)1e9+7;     // when MOD = 1e9+7, inv2 = 500000004
const int N = (int)2e5+7;
int lazy[N<<2],tree[N<<2];
#define ls rt << 1
#define rs rt << 1 | 1
#define lson l, m, ls
#define rson m+1, r, rs
void Add(int rt,int val) {
	lazy[rt] += val;
	tree[rt] += val;
}
void pushdown(int rt) {
	if(lazy[rt]) {
		Add(ls,lazy[rt]);
		Add(rs,lazy[rt]);
		lazy[rt] = 0;
	}
}
void update(int ql,int qr,int val,int l,int r,int rt) {
	if(ql <= l && qr >= r) {
		Add(rt,val);return;
	}
	pushdown(rt);
	int m = (l + r) >> 1;
	if(ql <= m) update(ql,qr,val,lson);
	if(m < qr) update(ql,qr,val,rson);
	tree[rt] = max(tree[ls],tree[rs]);
}
int query(int ql,int qr,int l,int r,int rt) {
	if(ql <= l && qr >= r) return tree[rt];
	pushdown(rt);
	int m = (l + r) >> 1;
	int res = 0;
	if(ql <= m) res = max(res,query(ql,qr,lson));
	if(m < qr) res = max(res,query(ql,qr,rson));
	return res;
}
pii p[N];
vi vl,vr;
int main()
{
	int n,m;scanf("%d%d",&n,&m);
	rep(i,0,n) {
		scanf("%d%d",&p[i].fi,&p[i].se);
		vl.pb(p[i].fi);
		vr.pb(p[i].se);
	}
	vl.pb(0);
	vr.pb(m+1);
	sort(all(vl));
	sort(all(vr));
	vl.erase(unique(all(vl)),vl.end());
	vr.erase(unique(all(vr)),vr.end());
	sort(p,p+n);
	int ans = 0;
	rep(i,0,sz(vr)) {
		update(i,i,vr[i]-1,0,sz(vr),1);
	}
	for(int i = 0,j;i<n;i=j) {
		for(j = i;p[i].fi == p[j].fi;j++);
		int cany = lower_bound(all(vr),p[i].fi) - vr.begin();
		per(k,i,j) {
			int y = lower_bound(all(vr),p[k].se) - vr.begin();
			update(0,y,1,0,sz(vr),1);
		}
		ans = max(ans,query(cany,sz(vr),0,sz(vr),1) - p[i].fi - m);
	}
	printf("%d\n",ans);
	return 0;
}

	
