#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = (int)1.01e9;

struct Edge {
    int from, to, cap, flow;
};

int q[(int)1e7], qL, qR;
struct Graph {
    int n;
    vector<Edge> edges;
    vector<vector<int> > e;
    vector<int> d, c;

    Graph(int N) {
        n = N;
        e.resize(n);
    }

    void addEdge(int from, int to, int cap) {
        e[from].push_back(edges.size());
        edges.push_back(Edge({from, to, cap, 0}));
        e[to].push_back(edges.size());
        edges.push_back(Edge({to, from, 0, 0}));
    }

    bool bfs() {
        c.assign(n, 0);
        d.assign(n, INF);
        qL = qR = 0;
        q[qR++] = 0;
        d[0] = 0;
        while (qL < qR) {
            int v = q[qL++];
            for (int i = 0; i < (int)e[v].size(); i++) {
                Edge cur = edges[e[v][i]];
                if (d[cur.to] > d[v] + 1 && cur.flow < cur.cap) {
                    d[cur.to] = d[v] + 1;
                    q[qR++] = cur.to;
                }
            }
        }
        return d[n - 1] != INF;
    }

    int dfs(int v, int flow) {
        if (v == n - 1) return flow;
        if (flow == 0) return 0;
        for (int &i = c[v]; i < (int)e[v].size(); i++) {
            Edge cur = edges[e[v][i]];
            if (d[cur.to] != d[v] + 1) continue;
            int pushed = dfs(cur.to, min(flow, cur.cap - cur.flow));
            if (pushed) {
                edges[e[v][i]].flow += pushed;
                edges[e[v][i] ^ 1].flow -= pushed;
                return pushed;
            }
        }
        return 0;
    }

    int flow() {
        int res = 0;
        int it = 0;
        while (bfs()) {
            cerr << ++it << endl;
            while (int pushed = dfs(0, INF)) {
                res += pushed;
            }
        }
        return res;
    }
};

int slowsolve(int n, int m, vector<int> l, vector<int> r) {
    Graph gr(1 + n + m + m + m + 1);
    for (int i = 0; i < n; i++) {
        gr.addEdge(0, 1 + i, 1);
        if (l[i] >= 0) gr.addEdge(1 + i, 1 + n + m + l[i], 1);
        if (r[i] < m) gr.addEdge(1 + i, 1 + n + m + m + r[i], 1);
    }
    for (int i = 0; i < m; i++) {
        gr.addEdge(1 + n + m + i, 1 + n + i, 1);
        if (i) gr.addEdge(1 + n + m + i, 1 + n + m + i - 1, n);
        gr.addEdge(1 + n + m + m + i, 1 + n + i, 1);
        if (i < m - 1) gr.addEdge(1 + n + m + m + i, 1 + n + m + m + i+1, n);
        gr.addEdge(1 + n + i, 1 + n + m + m + m, 1);
    }
    int fl = gr.flow();
    return n - fl;
}

int fastsolve(int n, int m, vector<int> l, vector<int> r) {
    int N = 1;
    while (N < m) N <<= 1;
    vector<int> t(2 * N);
    vector<int> mn(2 * N, -1), mx(2 * N, -1);
    for (int i = 0; i < m; i++) {
        mn[i + N] = mx[i + N] = i;
    }
    for (int i = N - 1; i > 0; i--) {
        mn[i] = mn[i * 2];
        mx[i] = mx[i * 2 + 1];
    }

    Graph gr(1 + n + m + 2 * N + 1);
    for (int i = 0; i < n; i++) {
        gr.addEdge(0, 1 + i, 1);
    }
    for (int i = 0; i < m; i++) {
        gr.addEdge(1 + n + i, 1 + n + m + 2 * N, 1);
    }
    for (int i = 1; i < 2 * N; i++) {
        if (mn[i] == -1 || mx[i] == -1) continue;
        for (int j = mn[i]; j <= mx[i]; j++) {
            gr.addEdge(1 + n + m + i, 1 + n + j, 1);
        }
    }
    auto addEdge = [&](int i, int L, int R) {
        L += N;
        R += N;
        while (L <= R) {
            if (L & 1) gr.addEdge(1 + i, 1 + n + m + L, 1);
            if (!(R & 1)) gr.addEdge(1 + i, 1 + n + m + R, 1);
            L = (L + 1) >> 1;
            R = (R - 1) >> 1;
        }
    };
    for (int i = 0; i < n; i++) {
        if (l[i] >= 0) addEdge(i, 0, l[i]);
        if (r[i] < m) addEdge(i, r[i], m - 1);
    }

    int fl = gr.flow();
    return n - fl;
}

int solve(int n, int m, vector<int> l, vector<int> r) {
    vector<int> t(n);
    iota(t.begin(), t.end(), 0);
    sort(t.begin(), t.end(), [&](int i, int j) {
        return l[i] < l[j];
    });
    int cur = 0;
    for (int i = 0; i < n; i++) {
        if (l[t[i]] >= cur && cur < m) {
            cur++;
        }
    }
    vector<char> used(n);
    priority_queue<pair<int, int> > q;
    int ci = n - 1;
    cur--;
    while (cur >= 0) {
        while (ci >= 0 && l[t[ci]] >= cur) {
            q.push(make_pair(r[t[ci]], t[ci]));
            ci--;
        }
        assert(!q.empty());
        used[q.top().second] = 1;
        q.pop();
        cur--;
    }
    sort(t.begin(), t.end(), [&](int i, int j) {
        return r[i] > r[j];
    });
    cur = m - 1;
    for (int i = 0; i < n; i++) {
        if (!used[t[i]] && cur >= 0 && r[t[i]] <= cur) {
            used[t[i]] = 1;
            cur--;
        }
    }
    int ans = 0;
    for (int i = 0; i < n; i++) ans += used[i];
    ans = min(ans, m);
    return n - ans;
}

int main() {
#ifdef HOME
    freopen("in", "r", stdin);
#endif

    if (0) {
        int n = 2e5;
        int m = 2e5;
        vector<int> l(n), r(n);
        for (int i = 0; i < n; i++) {
            l[i] = 1e3;
            r[i] = 1e3 + 1;
        }
        cerr << fastsolve(n, m, l, r) << endl;
    }


    int n, m;
    while (scanf("%d%d", &n, &m) == 2) {
        vector<int> l(n), r(n);
        for (int i = 0; i < n; i++) {
            scanf("%d%d", &l[i], &r[i]);
            l[i]--; r[i]--;
        }

        printf("%d\n", solve(n, m, l, r));
    }


#ifdef HOME
    cerr << clock() / (double)CLOCKS_PER_SEC << endl;
#endif
    return 0;
}

Measured memory_bytes: 6516