//Problem:
//Date:
//Skill:
//Bug:
/////////////////////////////////////////Definations/////////////////////////////////////////////////
//循环控制
#define CLR(a) memset((a),0,sizeof(a))
#define F(i,a,b) for(int i=a;i<=int(b);++i)
#define F2(i,a,b) for(int i=a;i>=int(b);--i)
#define RE(i,n)  for(int i=0;i<int(n);i++)
#define RE2(i,n) for(int i=1;i<=int(n);i++)
//简化敲击
#define PII pair<int,int>
#define PLL pari<long long ,long long>
#define PB push_back
#define ENDL '\n'
#define x first
#define y second
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const long long llinf = 0x3f3f3f3f3f3f3f3f;
////////////////////////////////////////Options//////////////////////////////////////////////////////
#define stdcpph
#define CPP_IO

#ifdef stdcpph
#include<bits/stdc++.h>
#else
#include<ctype.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<algorithm>
#include<functional>
#ifdef CPP_IO
#include<iostream>
#include<iomanip>
#include<string>
#else
#include<stdio.h>
#endif
#endif
////////////////////////////////////////Basic Functions//////////////////////////////////////////////
template<typename INint> inline void IN(INint &x)
{
	x = 0; int f = 1; char c; cin.get(c);
	while (c<'0' || c>'9') { if (c == '-')f = -1; cin.get(c); }
	while (c >= '0'&&c <= '9') { x = x * 10 + c - '0'; cin.get(c); }
	x *= f;
}
template<typename INint> inline void OUT(INint x)
{
	if (x > 9)OUT(x / 10); cout.put(x % 10 + '0');
}
////////////////////////////////////////Added Functions//////////////////////////////////////////////
const int maxn = int(2e5+5);


//6_2-线段树
//线段树模板，例子是区间求和

#define ls (o<<1)
#define rs (o<<1|1)
#define mid ((e[o].l+e[o].r)>>1)
struct node
{
	int l, r;
	ll ma;
	ll lazy;
};
node e[maxn << 2];
inline void PushUp(int &o)
//TODO: 根据子节点更新父节点
{
	e[o].ma = max(e[ls].ma, e[rs].ma);
}
void build(int o, int l, int r)
{
	//TODO: 初始化lazy和sum
	e[o].l = l; e[o].r = r; e[o].lazy = 0;
	if (l == r) { e[o].ma = e[o].r; return; }
	build(ls, l, mid);
	build(rs, mid + 1, r);
	PushUp(o);
}
void S_T(int o, ll inc)//update from new inc and update my lazy    S_T: solve and tag
{
	//TODO: 根据情景完成自身区间的改变，将改变参数传递给下一代
	e[o].ma += inc;
	e[o].lazy += inc;
}
inline void PushDown(int &o)//solve my lazy ans pass it to sons
{
	if (!e[o].lazy)return;
	//TODO: lazy是不是默认值不是0
	S_T(ls, e[o].lazy); S_T(rs, e[o].lazy);
	e[o].lazy = 0;
}
void update(int o, int l, int r, int v)//区间加v
{
	if (l > r)return;
	if (e[o].l == l && e[o].r == r) { S_T(o, v); return; }//找到边缘

	PushDown(o);
	update(ls, max(e[ls].l, l), min(r, mid), v);
	update(rs, max(mid + 1, l), min(e[rs].r, r), v);
	PushUp(o);
}
void update2(int o, int l, int r, int v)
{
	if (l <= e[o].l&&e[o].r <= r) { S_T(o, v); return; }
	if (max(l, e[o].l) > min(r, e[o].r))return;//舍弃

	PushDown(o);
	update2(ls, l, r, v);
	update2(rs, l, r, v);
	PushUp(o);
}

ll query(int o, int l, int r)//done
{
	if (l > r)return 0;
	if (e[o].l == l && e[o].r == r)return e[o].ma;
	PushDown(o);
	PushUp(o);
	return max(query(ls, max(l, e[ls].l), min(r, mid)), query(rs, max(l, mid + 1), min(r, e[rs].r)));
}
vector<int>R[maxn];
////////////////////////////////////////////Code/////////////////////////////////////////////////////
int main()
{
	//freopen("C:\\Users\\VUL%CAN\\Desktop\\data.in", "r", stdin);
	int T(1), times(0);
#ifdef CPP_IO// CPP_IO
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	//cin >> T;
#else
	//IN(T);
#endif 
	/////////////////////////////////////////////////////////////////////////////////////////////////
	while (++times, T--)
	{
		int n, m; cin >> n >> m;
		RE2(i, n) {
			int l, r; cin >> l >> r;
			R[l].PB(r);
		}
		build(1, 0, m + 1);
		int mis = 0;
		F(i, 0, m + 1)
		{
			for (auto r : R[i])
			{
				update(1, 0, r, 1);
			}
			if (i != m + 1)
			{
				int res = query(1, i + 1, m + 1);

				mis = max(mis, res - m - i - 1);//F(x)最大mis数
			}
		}
		int ans = mis;
		if (ans != max(mis, n - m))
			ans = max(mis, n - m);
		cout << ans << endl;
	}
	///////////////////////////////////////////////////////////////////////////////////////////////////
	return 0;
}

Measured memory_bytes: 22400