// I need some holiday
// But I don't wish that everyday is holiday
// Because I didn't bear the pain
// Because it wasn't a true freedom
//         ——『イヤホンと蝉時雨』·Orangestar

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef unsigned int UI;
typedef pair<int, int> pii;

#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define MP make_pair
#define fst first
#define snd second
#define SZ(u) ((int) (u).size())
#define ALL(u) (u).begin(), (u).end()

inline void proc_status()
{
	ifstream t("/proc/self/status");
	cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>()) << endl;
}

template<typename T> inline int chkmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template<typename T> inline int chkmax(T &a, const T &b) { return b > a ? a = b, 1 : 0; }
template<typename T> inline T sqr(const T &val) { return val * val; }

namespace fastIO
{
	const int MAX_BUFFER_SIZE = 1 << 16;

	char buffer[MAX_BUFFER_SIZE], *cur = buffer, *ed = buffer;

	inline char getc() { return cur == ed && (ed = (cur = buffer) + fread(buffer, 1, MAX_BUFFER_SIZE, stdin), cur == ed) ? EOF : *cur++; }
}
using fastIO::getc;

template<typename T> inline T read()
{
	register T sum(0), fg(1);
	register char ch(getc());
	for(; !isdigit(ch); ch = getc()) if(ch == '-') fg = -1;
	for(;  isdigit(ch); ch = getc()) sum = sum * 10 - '0' + ch;
	return sum * fg;
}

inline char *read_str(char *s)
{
	register char ch(getc());
	while(!isgraph(ch)) ch = getc();
	for(; isgraph(ch); ch = getc()) *s++ = ch;
	*s = '\0';
	return s;
}

// END tpl.

const int MAXN = (int) 2e5;

struct seg
{
	int l, r;

	seg() { }
	seg(int _l, int _r): l(_l), r(_r) { }
};

int n, m;

seg a[MAXN + 5];

inline void input()
{
	n = read<int>(), m = read<int>();
	for(int i = 1; i <= n; ++i) a[i].l = read<int>(), a[i].r = read<int>();
}

namespace SEGT
{
	const int MAX_NODE = MAXN * 4;

	int mx[MAX_NODE + 5];
	int add[MAX_NODE + 5];

	inline void push_up(int u) { mx[u] = max(mx[u << 1], mx[u << 1 | 1]); }

	inline void add_node(int u, int val)
	{
		mx[u] += val;
		add[u] += val;
	}

	inline void push_down(int u)
	{
		if(add[u])
		{
			add_node(u << 1, add[u]);
			add_node(u << 1 | 1, add[u]);
			add[u] = 0;
		}
	}

	inline void build(int u, int l, int r)
	{
		add[u] = 0;
		if(l == r)
		{
			mx[u] = -l;
			return;
		}

		int mid = (l + r) / 2;
		build(u << 1, l, mid);
		build(u << 1 | 1, mid + 1, r);
		push_up(u);
	}

	inline void update(int u, int l, int r, int ql, int qr, int val)
	{
		if(ql > qr) return;
		if(ql <= l && r <= qr)
		{
			add_node(u, val);
			return;
		}
		push_down(u);

		int mid = (l + r) / 2;
		if(ql <= mid) update(u << 1, l, mid, ql, qr, val);
		if(mid < qr) update(u << 1 | 1, mid + 1, r, ql, qr, val);
		push_up(u);
	}

	inline int query(int u, int l, int r, int ql, int qr)
	{
		if(ql > qr) return 0;
		if(ql <= l && r <= qr) return max(0, mx[u]);
		push_down(u);

		int mid = (l + r) / 2, res = 0;
		if(ql <= mid) chkmax(res, query(u << 1, l, mid, ql, qr));
		if(mid < qr) chkmax(res, query(u << 1 | 1, mid + 1, r, ql, qr));
		return res;
	}
}

inline void solve()
{
	sort(a + 1, a + n + 1, [] (seg A, seg B) { return A.r > B.r; });

	int ans = 0, mxl = 0, mnr = m + 1;
	for(int i = 1; i <= n; ++i)
	{
		chkmax(mxl, a[i].l);
		chkmin(mnr, a[i].r);
	}
	if(mxl >= mnr) chkmax(ans, n - m);

	SEGT::build(1, 0, m);
	for(int i = 1; i <= n; ++i)
	{
		int res = -(m - a[i].r + 1);
		SEGT::update(1, 0, m, max(0, a[i].l), m, +1);
		chkmax(ans, res + SEGT::query(1, 0, m, 0, a[i].r - 1));
	}
	printf("%d\n", ans);
}

int main()
{
#ifdef K_ON
	freopen("F.in", "r", stdin);
	freopen("F.out", "w", stdout);
#endif

	input();
	solve();

	return 0;
}




Measured memory_bytes: 7936