#include <cstdio>
#include <cstring>

#define R register

#define maxn 800010
#define maxm 8000010
#define inf 0x7fffffff
#define dmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))
struct Edge {
	Edge *next, *rev;
	int to, cap;
} *last[maxn], *cur[maxn], e[maxm], *ecnt = e;
inline void link(R int a, R int b, R int w)
{
	*++ecnt = (Edge) {last[a], ecnt + 1, b, w}; last[a] = ecnt;
	*++ecnt = (Edge) {last[b], ecnt - 1, a, 0}; last[b] = ecnt;
}
int s, t, q[maxn], dep[maxn], ans;
inline bool bfs()
{
	R int head = 0, tail = 1;
	memset(dep, -1, (t + 1) << 2);
	dep[q[1] = t] = 0;
	while (head < tail)
	{
		R int now = q[++head];
		for (R Edge *iter = last[now]; iter; iter = iter -> next)
			if (iter -> rev -> cap && dep[iter -> to] == -1)
				dep[q[++tail] = iter -> to] = dep[now] + 1;
	}
	return dep[s] != -1;
}
int dfs(R int x, R int f)
{
	if (x == t) return f;
	R int used = 0;
	for (R Edge* &iter = cur[x]; iter; iter = iter -> next)
		if (iter -> cap && dep[iter -> to] + 1 == dep[x])
		{
			R int v = dfs(iter -> to, dmin(f - used, iter -> cap));
			iter -> cap -= v;
			iter -> rev -> cap += v;
			used += v;
			if (used == f) return f;
		}
	return used;
}
inline void dinic()
{
	while (bfs())
	{
		memcpy(cur, last, sizeof cur);
		ans += dfs(s, inf);
	}
}
int main()
{
	R int n, m; scanf("%d%d", &n, &m);
	s = 0; t = n + m * 3 + 1;
	for (R int i = 1; i <= n; ++i)
	{
		link(s, i, 1);
		R int l, r; scanf("%d%d", &l, &r);
		if (l > 0) link(i, n + m + l, 1);
		if (r <= m) link(i, n + r, 1);
	}
	for (R int i = 1; i <= m; ++i)
	{
		if (i != 1) link(n + m + i, n + m + i - 1, inf);
		if (i != m) link(n + i, n + i + 1, inf);
		link(n + m + i, n + m + m + i, 1);
		link(n + i, n + m + m + i, 1);
		link(n + m + m + i, t, 1);
	}
	dinic();
	printf("%d\n", n - ans);
	return 0;
}