#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
 
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<ll> vi;
typedef long double ld; 
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
typedef set<ll>::iterator sit;
typedef map<ll,ll>::iterator mit;

const int MX = 601111;
const int INF = 250000;

	struct edge
	{
		int to,cap,rev;
		edge(int to=0,int cap=0,int rev=0):to(to),cap(cap),rev(rev){}
	};
	vector <edge> vec[MX];
	int level[MX];
	int iter[MX];
	
	void addedge(int s,int t,int c) //adds an edge of cap c to the flow graph
	{
		int S=vec[s].size(),T=vec[t].size();
		vec[s].push_back(edge(t,c,T));
		vec[t].push_back(edge(s,0,S));
	}
	void bfs(int s)
	{
		memset(level,-1,sizeof(level));
		queue <int> que;
		level[s] = 0;
		que.push(s);
		while (!que.empty())
		{
			int v = que.front();que.pop();
			for(int i=0;i<vec[v].size();i++)
			{
				edge&e=vec[v][i];
				if (e.cap>0&&level[e.to]<0)
				{
					level[e.to]=level[v]+1;
					que.push(e.to);
				}
			}
		}
	}
	int flow_dfs(int v,int t,int f)
	{
		if (v==t) return f;
		for(int &i=iter[v];i<vec[v].size();i++)
		{
			edge &e=vec[v][i];
			if (e.cap>0&&level[v]<level[e.to])
			{
				int d=flow_dfs(e.to,t,min(f,e.cap));
				if (d>0)
				{
					e.cap-=d;
					vec[e.to][e.rev].cap+=d;
					return d;
				}
			}
		}
		return 0;
	}
	int maxflow(int s,int t) //finds max flow using dinic from s to t
	{
		int flow = 0;
		while(1)
		{
			bfs(s);
			if (level[t]<0) return flow;
			memset(iter,0,sizeof(iter));
			while (1)
			{
				int f=flow_dfs(s,t,INF);
				if(f==0) break;
				flow += f;
			}
		}
	}

int n,m;

void upd(int id, int l, int r)
{
	for (l += m, r += m; l < r; l >>= 1, r >>= 1) 
	{
		if (l&1) 
		{
			//cerr<<id<<' '<<l<<'\n';
			addedge(2*m+id,l++,1);
		}
		if (r&1) 
		{
			//cerr<<id<<' '<<r-1<<'\n';
			addedge(2*m+id,--r,1);
		}
	}
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin>>n>>m;
	//n=200000;m=200000;
	int s,e;
	s=2*m+n+50; e=2*m+n+51;
	for(int i=0;i<n;i++)
	{
		addedge(s,2*m+i,1);
	}
	for(int i=m;i<2*m;i++) 
	{
		addedge(i,e,1);
	}
	for(int i=m-1;i>=1;i--)
	{
		addedge(i,(i<<1),INF);
		addedge(i,(i<<1)|1, INF);
	}
	for(int i=0;i<n;i++)
	{
		int l,r; 
		cin>>l>>r;
		//l=rand(); r=rand();
		//l%=m; r%=m;
	//	if(l<0) l+=m; 
		//if(r<0) r+=m;
		//l=1;r=m;
		//if(l>r) swap(l,r);
		l--; r--;
		//cerr<<0<<' '<<l<<' '<<r<<' '<<m-1<<'\n';
		if(l>=0) upd(i,0,l+1);
		if(r<m) upd(i,r,m);
	}
	cout<<n-maxflow(s,e)<<'\n';
}
