
#include <cmath>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <list>
#include <time.h>
#include <math.h>
#include <random>
#include <deque>
#include <queue>
#include <cassert>
#include <unordered_map>
#include <iomanip>

using namespace std;


typedef long long ll;

struct edge
{
	int v, to;
	int cap, scap;
};

const int M = 1e6 + 7;

int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
vector<edge> e;
vector<int> g[M];
int d[M];
int ptr[M];
int s, t;

bool bfs()
{
	for (int i = 0; i <= t; i++)
	{
		d[i] = -1;
	}
	queue<int> q;
	q.push(s);
	d[s] = 0;
	while (!q.empty())
	{
		int v = q.front();
		q.pop();
		for (auto x : g[v])
		{
			if (e[x].cap > 0 && d[e[x].to] == -1)
			{
				d[e[x].to] = d[v] + 1;
				q.push(e[x].to);
			}
		}
	}
	return d[t] != -1;
}

ll dfs(int v = s, int fl = 1e9)
{
	if (!fl)
	{
		return 0;
	}
	if (v == t)
	{
		return fl;
	}
	for (; ptr[v] < (int) g[v].size(); ptr[v]++)
	{
		int x = g[v][ptr[v]];
		if (e[x].cap > 0 && d[e[x].to] == d[v] + 1)
		{
			ll push = dfs(e[x].to, min(fl, e[x].cap));
			if (push)
			{
				e[x].cap -= push;
				e[x ^ 1].cap += push;
				return push;
			}
		}
	}
	return 0;
}

void add_edge(int v, int to, int cap)
{
	g[v].push_back(e.size());
	e.push_back({v, to, cap, cap});
	g[to].push_back(e.size());
	e.push_back({to, v, 0, 0});
}

int n, m;
vector <int> ind;

void build(int v, int l, int r)
{
	if (r - l == 1)
	{
		ind.push_back(v + n);
	}
	else
	{
		int m = (l + r) / 2;
		add_edge(v + n, n + v * 2 + 1, 1);
		add_edge(v + n, n + v * 2 + 2, 1);
        build(v * 2 + 1, l, m);
		build(v * 2 + 2, m, r);
	}
}

void add(int i, int v, int l, int r, int tl, int tr)
{
	if (tl >= r || tr <= l)
	{
		return;
	}
	if (tl >= l && tr <= r)
	{
		add_edge(i, n + v, 1);
	}
	else
	{
		int tm = (tl + tr) / 2;
		add(i, v * 2 + 1, l, r, tl, tm);
		add(i, v * 2 + 2, l, r, tm, tr);
	}
}

class Main
{
public:
	void solve(std::istream &in, std::ostream &out)
	{
		e.clear();
		for (int i = 0; i < M; i++)
		{
			g[i].clear();
		}
		in >> n >> m;
		ind.clear();
		build(0, 0, m);
        for (int i = 0; i < n; i++)
		{
			int l, r;
			in >> l >> r;
			l--, r--;
            if (l != -1)
			{
				add(i, 0, 0, l + 1, 0, m);
			}
			if (r != m)
			{
				add(i, 0, r, m, 0, m);
			}
		}
		s = ind.back() + 1;
		t = ind.back() + 2;
		for (int i = 0; i < n; i++)
		{
			add_edge(s, i, 1);
		}
        for (int c : ind)
		{
			add_edge(c, t, 1);
		}
		ll ans = 0;
		while (bfs())
		{
			for (int i = 0; i <= t; i++)
			{
				ptr[i] = 0;
			}
			while (ll cur = dfs())
			{
				ans += cur;
			}
		}
		out << n - ans << '\n';
	}
};
 
 