#include <bits/stdc++.h>
#define err(args...) {}
#ifdef DEBUG
#include "_debug.cpp"
#endif
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> using lim = numeric_limits<T>;
template <typename T> istream& operator>>(istream& is, vector<T>& a) { for(T& x : a) { is >> x; } return is; }
template <typename X, typename Y> istream& operator>>(istream& is, pair<X, Y>& p) { return is >> p.first >> p.second; }
void solve(long long N, long long M, std::vector<long long> L, std::vector<long long> R) {
    vector<vector<int>> opens_at(M + 2), closes_at(M + 2);
    for(int i = 0; i < N; i++) {
        closes_at[L[i]].push_back(i);
        opens_at[R[i]].push_back(i);
    }
    vector<bool> left_removed(N);
    vector<int> seat(N);
    auto left_removed_unseated_ordering = [&](int i) {
        return make_pair(L[i], i);
    };
    auto left_removed_unseated_cmp = [&](int i, int j) {
        return left_removed_unseated_ordering(i) < left_removed_unseated_ordering(j);
    };
    set<int, decltype(left_removed_unseated_cmp)> left_removed_unseated(left_removed_unseated_cmp);
    auto active_unseated_ordering = [&](int i) {
        return make_tuple(left_removed[i] ? M + 1 : L[i], -R[i], i);
    };
    auto active_unseated_cmp = [&](int i, int j) {
        return active_unseated_ordering(i) < active_unseated_ordering(j);
    };
    set<int, decltype(active_unseated_cmp)> active_unseated(active_unseated_cmp);
    auto active_seated_ordering = [&](int i) {
        return make_pair(seat[i], i);
    };
    auto active_seated_cmp = [&](int i, int j) {
        return active_seated_ordering(i) < active_seated_ordering(j);
    };
    set<int, decltype(active_seated_cmp)> active_seated(active_seated_cmp);
    for(int i = 0; i < N; i++) {
        active_unseated.insert(i);
    }
    for(int x = 0; x <= M + 1; x++) {
        for(int i : opens_at[x]) {
            if(seat[i]) {
                active_seated.insert(i);
            } else {
                active_unseated.insert(i);
            }
        }
        if(1 <= x and x <= M) {
            if(not active_unseated.empty()) {
                seat[*active_unseated.begin()] = x;
                active_unseated.erase(active_unseated.begin());
            } else if(not active_seated.empty() and not left_removed_unseated.empty()) {
                int i = *active_seated.begin();
                while(not left_removed_unseated.empty() and seat[i] > L[*left_removed_unseated.begin()]) {
                    left_removed_unseated.erase(left_removed_unseated.begin());
                }
                if(not left_removed_unseated.empty()) {
                    seat[*left_removed_unseated.begin()] = seat[i];
                    seat[i] = x;
                    active_seated.erase(i);
                    active_seated.insert(i);
                    left_removed_unseated.erase(left_removed_unseated.begin());
                }
            }
        }
        for(int i : closes_at[x]) {
            active_seated.erase(i);
            active_unseated.erase(i);
            left_removed[i] = true;
            if(not seat[i]) {
                left_removed_unseated.insert(i);
            }
        }
    }
    cout << N - count_if(seat.begin(), seat.end(), [](int x) { return x > 0; }) << endl;
}
int main() {
    long long N;
    scanf("%lld",&N);
    long long M;
    scanf("%lld",&M);
    std::vector<long long> L(N);
    std::vector<long long> R(N);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&L[i]);
        scanf("%lld",&R[i]);
    }
    solve(N, M, std::move(L), std::move(R));
    return 0;
}


Measured memory_bytes: 34304