#include <bits/stdc++.h>
#define ADD(a, b) a = (a + (ll)b) % mod
#define MUL(a, b) a = (a * (ll)b) % mod
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define rep(i, a, b) for(int i = a; i < b; i++)
#define rer(i, a, b) for(int i = a - 1; i >= b; i--)
#define all(a) (a).begin(), (a).end()
#define sec second
#define fst first
#define debug(fmt, ...) Debug(__LINE__, ":", fmt, ##__VA_ARGS__)

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef pair<pi, pi> ppi;
typedef vector<ll> vec;

const int MAX_N = 200010;
const double eps = 1e-6;
const ll mod = 1000000007;
const int inf = 1 << 30;

void Debug() {cout << '\n'; }

template<class FIRST, class... REST> 
void Debug(FIRST arg, REST... rest) { cout << arg << " "; Debug(rest...); }

template<class T>
ostream& operator<< (ostream& out, const vector<T>& v) {
	out << "[";
	if(!v.empty()) {
		rep(i, 0, (int)v.size() - 1) out << v[i] << ", ";
		out << v.back();
	}
	out << "]";
  return out;
}

template<class S, class T>
ostream& operator<< (ostream& out, const pair<S, T>& v) {
	out << "(" << v.first << ", " << v.second << ")";
	return out;
}

///g++ -g3 -std=c++0x -DLOCAL -Wall -ftrapv -D_GLIBCXX_DEBUG -Wl,-stack,268435456 -o
///g++ -O2 -std=c++0x -DLOCAL -Wall -Wl,-stack,268435456 -o

///////////////////////////////////////////////////////////////////////

struct node {
	ll sum, lazy;
	node() : sum(0), lazy(0){}
	node(ll sum, ll lazy) : sum(sum), lazy(lazy){}
};

int M;
node seg[200000 * 4];

inline void lazy_eval(int k, int l, int r) {
	if(seg[k].lazy) {
		seg[k * 2 + 1].sum += seg[k].lazy;//if range_min, erase (m - l)
		seg[k * 2 + 1].lazy += seg[k].lazy;
		seg[k * 2 + 2].sum += seg[k].lazy;//here too
		seg[k * 2 + 2].lazy += seg[k].lazy;
		seg[k].lazy = 0;
	}
}

void update(int a, int b, ll s, int k = 0, int l = 0, int r = M + 1) {
	if(b <= l || r <= a) return;
	if(a <= l && r <= b) {
		seg[k].sum += s;//here too
		seg[k].lazy += s;
	}
	else {
		int m = (l + r) / 2;
		lazy_eval(k, l, r);
		update(a, b, s, k * 2 + 1, l, m);
		update(a, b, s, k * 2 + 2, m, r);
		MAX(seg[k].sum, seg[k * 2 + 1].sum);
		MAX(seg[k].sum, seg[k * 2 + 2].sum);
	}
}

ll get_sum(int a, int b, int k = 0, int l = 0, int r = M + 1) {
	if(b <= l || r <= a) return 0;
	if(a <= l && r <= b) return seg[k].sum;
	else {
		int m = (l + r) / 2;
		lazy_eval(k, l, r);
		ll lsum = get_sum(a, b, k * 2 + 1, l, m);
		ll rsum = get_sum(a, b, k * 2 + 2, m, r);
		return max(lsum, rsum);
	}
}

void show_seg() {
	vector<int> vec;
	rep(i, 0, M + 1) vec.push_back(get_sum(i, i + 1));
	cout << vec << "\n";
}

int N;
vector<int> P[MAX_N];

void solve() {
	cin >> N >> M;
	rep(i, 0, N) {
		int a, b;
		cin >> a >> b;
		P[a].push_back(b);
	}
	M++;
	rep(i, 0, M + 1) update(i, i + 1, i);
	ll res = max(N - M + 1, 0);
	rep(i, 0, M) {
		rep(j, 0, (int)P[i].size()) {
			int n = P[i][j];
			update(0, n + 1, 1);
		}
		ll t = get_sum(i + 2, M + 1);
		//debug(t, M, i, i + 2);
		MAX(res, t - M - i);
		//show_seg();
	}
	cout << res << "\n";
}


int main() {
	ios::sync_with_stdio(false);
    cin.tie(0);	
    cout << fixed;
	cout.precision(20);
#ifdef LOCAL
    freopen("in.txt", "rt", stdin);
#endif	
	solve();
#ifdef LOCAL
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
	return 0;
}


Measured memory_bytes: 20992