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 C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
class Solve{
    public Solve(){}
    StringBuilder sb;
    public static int Main(){
        new Solve().Run();
        return 0;
    }
    void Run(){
        sb = new StringBuilder();
        Calc();
        Console.Write(sb.ToString());
    }
    void Calc(){
        string[] str = Console.ReadLine().Split(' ');
        int N = int.Parse(str[0]);
        int M = int.Parse(str[1]);
        int[] L = new int[N];
        int[] R = new int[N];
        int[] C = new int[N];
        for(int i=0;i<N;i++){
            str = Console.ReadLine().Split(' ');
            L[i] = int.Parse(str[0]);
            R[i] = int.Parse(str[1]);
            C[i] = i;
        }
        Array.Sort(C,(x,y)=>(R[y] - R[x]));
        int count = Math.Max(N-M,0);
        SegTree Seg = new SegTree(M+2,this);
        for(int i=2*M+2;i>=0;i--){
            if(i >= Seg.segf){
                Seg.Z[i] = -Seg.ToPoint(i); 
            }
            else{
                int sl = i*2+1;
                int sr = i*2+2;
                Seg.Z[i] = Math.Max(Seg.Z[sl],Seg.Z[sr]);
            }
        }
        int p = 0;
        for(int i=M;i>=0;i--){
            while(p < N && R[C[p]] >= i+1){
                Seg.Add(L[C[p]],M+1);
                p++;
            }
            int A = Seg.Max(0,i)-(M-i);
            count = Math.Max(count,A);
        }
        sb.Append(count+"\n");
    }
}
struct SegTree{
    Solve Sol;
    public int[] Y;
    public int[] Z;
    int C;
    //葉の最初の場所
    public int segf;
    //segfのbit
    public int segfb;
    //葉の深さの内浅い方
    public int depth;
    //浅い葉の内座標が最大のもの
    public int borderP;
    //はみ出すものの数
    public int border;
    public SegTree(int N,Solve So){
        C = 0;
        Sol = So;
        Y = new int[2*N-1];
        Z = new int[2*N-1];
        for(depth = 1;(1 << depth) <= N;depth++){
            ;
        }
        segf = N-1;
        borderP = (1 << depth) - 2;
        border = 2*N - borderP - 2;
        segfb = border/2;
    }
    public int ToBit(int p){
        if(p >= border){
            return segfb + p - border;
        }
        else{
            return p;
        }
    }
    //葉のある場所
    public int ToLeaf(int p){
        if(p >= border){
            return segf + p - border;
        }
        else{
            return borderP + p + 1;
        }
    }
    //葉の示す頂点
    public int ToPoint(int leaf){
        if(leaf > borderP){
            return leaf - borderP - 1;
        }
        else{
            return leaf - segf + border;
        }
    }
    void Act2(int v){
        C = Math.Max(Z[v],C);
    }
    void Act3(int v){
        if(v < segf){
            Z[v] = Math.Max(Z[v],Z[v*2+1]);
            Z[v] = Math.Max(Z[v],Z[v*2+2]);
        }
    }
    void Act4(int v){
        if(v < segf){
            Y[v*2+1] += Y[v];
            Y[v*2+2] += Y[v];
            Z[v*2+1] += Y[v];
            Z[v*2+2] += Y[v];
        }
        Y[v] = 0;
    }
    //vの子の内bより大きいものに(bは子孫,bはToBitしたもの)
    void More2(int b,int v,int depth){
        Act4(v);
        if((b & (1 << depth)) == 0){
            //右の子は全てbより大きい
            if(((b+1) % (1 << depth)) != 0){
                More2(b,v*2+1,depth-1);
                Act2(v*2+2);
            }
            else{
                Act2(v*2+2);
            }
        }
        else{
            //左の子は全てb未満
            More2(b,v*2+2,depth-1);
        }
    }
    //vの子の内bより小さなものに(bは子孫,bはToBitしたもの)
    void Less2(int b,int v,int depth){
        Act4(v);
        if((b & (1 << depth)) == 0){
            //右の子は全てbより大きい
            Less2(b,v*2+1,depth-1);
        }
        else{
            //左の子は全てb未満
            if(b % (1 << depth) != 0){
                Act2(v*2+1);
                Less2(b,v*2+2,depth-1);
            }
            else{
                //右の子は全てb以上
                Act2(v*2+1);
            }
        }
    }
    void Section2(int bl,int br,int v,int dl,int dr){
        Act4(v);
        if(v >= segf){
            //葉
            Act2(v);
        }
        else{
            if((bl & (1 << dl)) == 0 && (br & (1 << dr)) == 0){
                //両方左
                Section2(bl,br,v*2+1,dl-1,dr-1);
            }
            else if((bl & (1 << dl)) != 0 && (br & (1 << dr)) != 0){
                //両方右
                Section2(bl,br,v*2+2,dl-1,dr-1);
            }
            else{
                //左と右に分かれた
                if(bl % (1 << dl) != 0){
                    More2(bl-1,v*2+1,dl-1);
                }
                else{
                    //左は全て含まれる
                    Act2(v*2+1);
                }
                if((br + 1) % (1 << dl) != 0){
                    Less2(br+1,v*2+2,dr-1);
                }
                else{
                    //右は全て含まれる
                    Act2(v*2+2);
                }
            }
        }
    }
    public int Max(int l0,int r0){
        C = -2000000;
        int bl = ToBit(l0);
        int br = ToBit(r0);
        int dl = l0 >= border ? depth - 2 : depth - 1;
        int dr = r0 >= border ? depth - 2 : depth - 1;
        Section2(bl,br,0,dl,dr);
        return C;
    }
    void Act(int v){
        Y[v]++;
        Z[v]++;
    }
    //vの子の内bより大きいものに(bは子孫,bはToBitしたもの)
    void More(int b,int v,int depth){
        Act4(v);
        if((b & (1 << depth)) == 0){
            //右の子は全てbより大きい
            if(((b+1) % (1 << depth)) != 0){
                More(b,v*2+1,depth-1);
                Act(v*2+2);
            }
            else{
                Act(v*2+2);
            }
        }
        else{
            //左の子は全てb未満
            More(b,v*2+2,depth-1);
        }
        Act3(v);
    }
    //vの子の内bより小さなものに(bは子孫,bはToBitしたもの)
    void Less(int b,int v,int depth){
        Act4(v);
        if((b & (1 << depth)) == 0){
            //右の子は全てbより大きい
            Less(b,v*2+1,depth-1);
        }
        else{
            //左の子は全てb未満
            if(b % (1 << depth) != 0){
                Act(v*2+1);
                Less(b,v*2+2,depth-1);
            }
            else{
                //右の子は全てb以上
                Act(v*2+1);
            }
        }
        Act3(v);
    }
    void Section(int bl,int br,int v,int dl,int dr){
        Act4(v);
        if(v >= segf){
            //葉
            Act(v);
        }
        else{
            if((bl & (1 << dl)) == 0 && (br & (1 << dr)) == 0){
                //両方左
                Section(bl,br,v*2+1,dl-1,dr-1);
            }
            else if((bl & (1 << dl)) != 0 && (br & (1 << dr)) != 0){
                //両方右
                Section(bl,br,v*2+2,dl-1,dr-1);
            }
            else{
                //左と右に分かれた
                if(bl % (1 << dl) != 0){
                    More(bl-1,v*2+1,dl-1);
                }
                else{
                    //左は全て含まれる
                    Act(v*2+1);
                }
                if((br + 1) % (1 << dl) != 0){
                    Less(br+1,v*2+2,dr-1);
                }
                else{
                    //右は全て含まれる
                    Act(v*2+2);
                }
            }
        }
        Act3(v);
    }
    public void Add(int l0,int r0){
        int bl = ToBit(l0);
        int br = ToBit(r0);
        int dl = l0 >= border ? depth - 2 : depth - 1;
        int dr = r0 >= border ? depth - 2 : depth - 1;
        Section(bl,br,0,dl,dr);
    }
}