Problem:
Score : 1000 points

Problem Statement

There are M chairs arranged in a line. The coordinate of the i-th chair (1 ≤ i ≤ M) is i.

N people of the Takahashi clan played too much games, and they are all suffering from backaches. They need to sit in chairs and rest, but they are particular about which chairs they sit in. Specifically, the i-th person wishes to sit in a chair whose coordinate is not greater than L_i, or not less than R_i. Naturally, only one person can sit in the same chair.

It may not be possible for all of them to sit in their favorite chairs, if nothing is done.
Aoki, who cares for the health of the people of the Takahashi clan, decides to provide additional chairs so that all of them can sit in chairs at their favorite positions.

Additional chairs can be placed at arbitrary real coordinates. Find the minimum required number of additional chairs.

Constraints

1 ≤ N,M ≤ 2 × 10^5

0 ≤ L_i < R_i ≤ M + 1(1 ≤ i ≤ N)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1
:
L_N R_N

Output

Print the minimum required number of additional chairs.

Sample Input 1

4 4
0 3
2 3
1 3
3 4

Sample Output 1

0

The four people can sit in chairs at the coordinates 3, 2, 1 and 4, respectively, and no more chair is needed.

Sample Input 2

7 6
0 7
1 5
3 6
2 7
1 6
2 6
3 7

Sample Output 2

2

If we place additional chairs at the coordinates 0 and 2.5, the seven people can sit at coordinates 0, 5, 3, 2, 6, 1 and 2.5, respectively.

Sample Input 3

3 1
1 2
1 2
1 2

Sample Output 3

2

Sample Input 4

6 6
1 6
1 6
1 5
1 5
2 6
2 6

Sample Output 4

2

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

//package com.company;
// Always comment out package when submitting.
import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main {
    public static class Task {
        public class SegmentTree {
            int n;
            int[] val;
            int[] lazy;
            public SegmentTree(int n) {
                this.n = n;
                val = new int[4 * n];
                lazy = new int[4 * n];
            }
            public void build(int[] initialVal) {
//                System.arraycopy(initialVal, 0, val, 0, initialVal.length);
                build(initialVal, 0, n, 1);
            }
            public void build(int[] initialVal, int l, int r, int idx) {
                if (l == r - 1) {
                    val[idx] = initialVal[l];
                    return;
                }
                int mid = (l + r) >> 1;
                build(initialVal, l, mid, idx << 1);
                build(initialVal, mid, r, idx << 1 | 1);
                val[idx] = Math.max(val[idx << 1], val[idx << 1 | 1]);
            }
            public void push(int idx) {
                if ((idx << 1 | 1) < val.length) {
                    lazy[idx << 1] += lazy[idx];
                    lazy[idx << 1 | 1] += lazy[idx];
                    val[idx << 1] += lazy[idx];
                    val[idx << 1 | 1] += lazy[idx];
                    lazy[idx] = 0;
                }
            }
            public void addRange(int L, int R, int v) {
//                for (int i = L; i < R; i++) {
//                    val[i] += v;
//                }
                addRange(0, n, 1, L, R, v);
            }
            public void addRange(int l, int r, int idx, int L, int R, int v) {
                if (L <= l && r <= R) {
                    lazy[idx] += v;
                    val[idx] += v;
                    return;
                }
                if (L >= r || l >= R) return;
                push(idx);
                int mid = (l + r) >> 1;
                addRange(l, mid, idx << 1, L ,R, v);
                addRange(mid, r, idx << 1 | 1, L ,R, v);
                val[idx] = Math.max(val[idx << 1], val[idx << 1 | 1]);
            }
            public int query() {
//                int mx = 0;
//                for (int i = 0; i < n; i++) {
//                    mx = Math.max(mx, val[i]);
//                }
//                return mx;
//                return query(0, n, 1, 0, n);
                push(1);
                return val[1];
            }
            public int query(int l, int r, int idx, int L, int R) {
                return 0;
            }
        }
        int n, m;
        int[][] lr;
        public void solve(Scanner sc, PrintWriter pw) throws IOException {
            n = sc.nextInt();
            m = sc.nextInt();
            lr = new int[n][2];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < 2; j++) {
                    lr[i][j] = sc.nextInt();
                }
            }
            SegmentTree st = new SegmentTree(m + 1);
            int[] initialArr = new int[m + 1];
            for (int i = 0; i < initialArr.length; i++) {
                initialArr[i] = -(initialArr.length - i);
            }
            st.build(initialArr);
            Arrays.sort(lr, new Comparator<int[]>() {
                @Override
                public int compare(int[] o1, int[] o2) {
                    return o1[0] - o2[0];
                }
            });
            int maxVal = n - m;
            int free = 0;
            for (int i = 0; i < n; i++) {
                int l = lr[i][0];
                int r = lr[i][1];
                if (r <= m) {
                    st.addRange(0, r + 1, 1);
                } else {
                    free++;
                }
                maxVal = Math.max(maxVal, st.query() - l + free);
            }
            pw.println(Math.max(0, maxVal));
        }
    }

    // template, actual code is in class Task.
    static long TIME_START, TIME_END;

    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
//        Scanner sc = new Scanner(new FileInputStream("Test.in"));
        PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out));
//        PrintWriter pw = new PrintWriter(new FileOutputStream("Test.out"));

        Runtime runtime = Runtime.getRuntime();
        long usedMemoryBefore = runtime.totalMemory() - runtime.freeMemory();
        TIME_START = System.currentTimeMillis();
        Task t = new Task();
        t.solve(sc, pw);
        TIME_END = System.currentTimeMillis();
        long usedMemoryAfter = runtime.totalMemory() - runtime.freeMemory();
        pw.close();
//        System.err.println("Memory increased: " + (usedMemoryAfter - usedMemoryBefore) / 1000000);
//        System.err.println("Time used: " + (TIME_END - TIME_START) + ".");
    }

    // Faster IO with BufferedReader wrapped with Scanner
    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public Scanner(FileReader s) throws FileNotFoundException {
            br = new BufferedReader(s);
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public String nextLine() throws IOException {
            return br.readLine();
        }

        public double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        public boolean ready() throws IOException {
            return br.ready();
        }
    }
}