// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

const int N = 200005;

int n, m, L, R;

vector<int> val[N];

int lch[N << 2], rch[N << 2], Max[N << 2], lazy[N << 2];

inline void update(int x, int v) {

    lazy[x] += v; Max[x] += v;

}

inline void pushup(int x) {Max[x] = max(Max[x << 1], Max[x << 1 | 1]);}

inline void pushdown(int x) {

    if(lazy[x]) {

        update(x << 1, lazy[x]); update(x << 1 | 1, lazy[x]); lazy[x] = 0; 

    }

}

void build(int x,int l, int r) {

    lch[x] = l; rch[x] = r;

    if(l == r) {

        Max[x] = l; return;

    }

    int mid = (l + r) / 2;

    build(x << 1, l, mid); build(x << 1 | 1, mid + 1, r);

    pushup(x);

}

void update(int x, int l, int r, int v) {

    if(l <= lch[x] && rch[x] <= r) {

        update(x, v); return;

    }

    pushdown(x);

    int mid = (lch[x] + rch[x]) / 2;

    if(r <= mid) update(x << 1, l, r, v);

    else if(l > mid) update(x << 1 | 1, l, r, v);

    else update(x << 1, l, mid, v), update(x << 1 | 1, mid + 1, r, v);

    pushup(x);

}

int query(int x, int l, int r) {

    if(l <= lch[x] && rch[x] <= r) {

        return Max[x];

    }

    pushdown(x);

    int mid = (lch[x] + rch[x]) / 2;

    if(r <= mid) return query(x << 1, l, r);

    else if(l > mid) return query(x << 1 | 1, l, r);

    else return max(query(x << 1, l, mid), query(x << 1 | 1, mid + 1, r));

}

int ans = 0;

int main() {

    scanf("%d%d", &n, &m);

    for(int i = 1; i <= n; ++i) {

        scanf("%d%d", &L, &R); val[L].push_back(R);

    }

    build(1, 0, m + 1);

    for(int i = 0; i <= m; ++i) {

        for(int j = 0; j < val[i].size(); ++j) {

            update(1, 0, val[i][j], 1);

        }

        ans = max(ans, query(1, i + 1, m + 1) - m - i - 1);

    }

    printf("%d\n", max(ans, n - m));

    return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

#define ls(x) (x * 2)

#define rs(x) (x * 2 + 1)

using namespace std;

const int maxn = 2e5 + 7, inf = 1e9 + 7;

int n, m, ans = 0;



struct Opt

{

    int l, r;

} p[maxn];



struct Node

{

    int lazy, maxx;

} node[maxn * 4];



bool cmp(const Opt &A, const Opt &B) { return A.l < B.l; }



void pushdown(int num, int nl, int nr)

{

    if (!node[num].lazy)

        return;

    int lz = node[num].lazy;

    node[num].lazy = 0;

    node[ls(num)].maxx += lz;

    node[ls(num)].lazy += lz;

    node[rs(num)].maxx += lz;

    node[rs(num)].lazy += lz;

}



void update(int num)

{

    node[num].maxx = max(node[ls(num)].maxx, node[rs(num)].maxx);

}



void build(int num, int nl, int nr)

{

    node[num].lazy = 0;

    if (nl == nr)

    {

        node[num].maxx = nr - m - 1;

        return;

    }

    int mid = (nl + nr) / 2;

    build(ls(num), nl, mid);

    build(rs(num), mid + 1, nr);

    update(num);

}

void add(int num, int nl, int nr, int ql, int qr, int ad)

{

    if (ql <= nl && nr <= qr)

    {

        node[num].maxx += ad;

        node[num].lazy += ad;

        return;

    }

    if (nr < ql || qr < nl)

        return;

    int mid = (nl + nr) / 2;

    pushdown(num, nl, nr);

    add(ls(num), nl, mid, ql, qr, ad);

    add(rs(num), mid + 1, nr, ql, qr, ad);

    update(num);

    return;

}



int quary(int num, int nl, int nr, int ql, int qr)

{

    if (ql <= nl && nr <= qr)

        return node[num].maxx;

    if (nr < ql || qr < nl)

        return -inf;

    int mid = (nl + nr) / 2;

    pushdown(num, nl, nr);

    return max(quary(ls(num), nl, mid, ql, qr), quary(rs(num), mid + 1, nr, ql, qr));

}



int main()

{

    scanf("%d%d", &n, &m);

    for (int i = 1; i <= n; i++)

        scanf("%d%d", &p[i].l, &p[i].r);

    sort(p + 1, p + n + 1, cmp);

    p[n + 1].l = inf;

    build(1, 0, m + 1);

    for (int i = 1; i <= n; i++)

    {

        add(1, 0, m + 1, 0, p[i].r, 1);

        if (p[i].l != p[i + 1].l && p[i].l <= m - 1)

            ans = max(ans, quary(1, 0, m + 1, p[i].l + 2, m + 1) - p[i].l);

    }

    ans = max(ans, n - m);

    printf("%d\n", ans);

    return 0;

}


// Fastest known solution for this problem: