542_B. Duck Hunt_9689
A duck hunter is doing his favorite thing, hunting. He lives in a two dimensional world and is located at point (0, 0). As he doesn't like walking for his prey, he prefers to shoot only vertically up (because in this case, the ducks fall straight into his hands). The hunter doesn't reload the gun immediately — r or more seconds must pass between the shots. When the hunter shoots up, the bullet immediately hits all the ducks who are directly above the hunter.

In a two dimensional world each duck is a horizontal segment that moves horizontally in the negative direction of the Ox axis at the speed 1 length unit per second. For each duck we know the values hi and ti — the x-coordinates of its head (the left end of the segment) and its tail (the right end of the segment) at time 0. The height where the duck is flying isn't important as the gun shoots vertically up to the infinite height and hits all the ducks on its way. 

<image> The figure to the first sample.

What maximum number of ducks can the hunter shoot? The duck is considered shot by the hunter if at the moment of the shot at least one of its point intersects the Oy axis. After the hunter shoots the duck, it falls and it can't be shot anymore. The hunter cannot make shots before the moment of time 0.

Input

The first line of the input contains integers n, r (1 ≤ n ≤ 200 000, 1 ≤ r ≤ 109) — the number of ducks and the minimum time in seconds between the shots. 

Then n lines follow, each of them contains two integers hi, ti ( - 109 ≤ hi < ti ≤ 109) — the x-coordinate of the head and tail of the i-th duck at the moment 0.

Output

Print a single integer — the maximum number of ducks that can be shot by the hunter.

Examples

Input

3 3
-3 0
1 3
-1 2


Output

3


Input

4 5
-1 1
2 4
5 9
6 8


Output

3

Note

In the first sample the hunter must shoot at time 0, this shot kills ducks 1 and 3. Then the hunter needs to reload the gun and shoot again at time 3. His second shot hits the tail of duck 2.

In the second sample the hunter can make shots at times 0 and 6 to hit three ducks.
import java.io.*;
import java.util.Arrays;
import java.util.Random;

public class VK3B {

    static final Random rnd = new Random(42);

    static class Node {
        int x, y;
        int val, add;
        int min;

        Node l, r;

        public Node(int x, int val) {
            this.x = x;
            this.y = rnd.nextInt();
            this.val = this.min = val;
        }
    }

    static Node pushDown(Node n) {
        n.val += n.add;
        if (n.l != null) {
            n.l.add += n.add;
            n.l.min += n.add;
        }
        if (n.r != null) {
            n.r.add += n.add;
            n.r.min += n.add;
        }
        n.add = 0;
        return n;
    }

    static Node mergeUp(Node n) {
        n.min = n.val;
        if (n.l != null) {
            n.min = Math.min(n.min, n.l.min);
        }
        if (n.r != null) {
            n.min = Math.min(n.min, n.r.min);
        }
        n.min += n.add;
        return n;
    }

    static Node merge(Node l, Node r) {
        if (l == null) {
            return r;
        }
        if (r == null) {
            return l;
        }
        if (l.x >= r.x) {
            throw new AssertionError();
        }
        if (l.y > r.y) {
            pushDown(l);
            l.r = merge(l.r, r);
            return mergeUp(l);
        } else {
            pushDown(r);
            r.l = merge(l, r.l);
            return mergeUp(r);
        }
    }

    static Node[] split(Node n, int x) {
        if (n == null) {
            return new Node[] {null, null};
        }
        pushDown(n);
        Node[] ret;
        if (x <= n.x) {
            ret = split(n.l, x);
            n.l = ret[1];
            ret[1] = mergeUp(n);
        } else {
            ret = split(n.r, x);
            n.r = ret[0];
            ret[0] = mergeUp(n);
        }
        return ret;
    }

    static Node left(Node n) {
        while (n.l != null) {
            pushDown(n);
            n = n.l;
        }
        pushDown(n);
        return n;
    }

    static Node right(Node n) {
        while (n.r != null) {
            pushDown(n);
            n = n.r;
        }
        pushDown(n);
        return n;
    }

    static Node popLeft(Node n) {
        if (n.l == null) {
            return n.r;
        }
        n.l = popLeft(n.l);
        return mergeUp(n);
    }

    static Node next(Node n, int x) {
        if (n == null) {
            return null;
        }
        if (x < n.x) {
            Node ret = next(n.l, x);
            return ret == null ? n : ret;
        } else {
            return next(n.r, x);
        }
    }

    static Node inc(Node n, int x) {
        Node[] p = split(n, x);
        if (p[0] != null) {
            int val = right(p[0]).val;
            if (p[1] == null || left(p[1]).x > x) {
                if (p[1] != null && left(p[1]).val == val) {
                    left(p[1]).x = x;
                } else {
                    Node nn = new Node(x, val);
                    p[1] = merge(nn, p[1]);
                }
            }
            p[0].add++;
            mergeUp(p[0]);
            if (p[1] != null && left(p[0]).val == right(p[1]).val) {
                p[1] = popLeft(p[1]);
            }
        }
        return merge(p[0], p[1]);
    }

    public static void solve(Input in, PrintWriter out) throws IOException {
        int n = in.nextInt();
        int d = in.nextInt();
        int[] l = new int[n];
        int[] r = new int[n];
        for (int i = 0; i < n; ++i) {
            l[i] = in.nextInt();
            r[i] = in.nextInt();
            if (r[i] < 0) {
                --n;
                --i;
            } else {
                l[i] = Math.max(l[i], 0);
                r[i] = Math.max(r[i], 0);
            }
        }
        l = Arrays.copyOf(l, n);
        r = Arrays.copyOf(r, n);
        out.println(solveFast(d, l, r));
    }

    private static int solveFast(int d, int[] l, int[] r) {
        int n = l.length;
        Integer[] ord = new Integer[n];
        for (int i = 0; i < n; ++i) {
            ord[i] = i;
        }
        Arrays.sort(ord, (o1, o2) -> Integer.compare(r[o1], r[o2]));
        Node u = new Node(Integer.MIN_VALUE, 0);
        int prevx = Integer.MIN_VALUE + d;
        for (int i = 0; i < n || next(u, prevx - d) != null; ) {
            int xAt = Integer.MAX_VALUE;
            if (i < n) {
                xAt = Math.min(xAt, r[ord[i]] + 1);
            }
            if (next(u, prevx - d) != null) {
                xAt = Math.min(xAt, next(u, prevx - d).x + d);
            }
            if (xAt == Integer.MAX_VALUE) {
                throw new AssertionError();
            }
            while (i < n && r[ord[i]] + 1 <= xAt) {
                if (r[ord[i]] + 1 < xAt) {
                    throw new AssertionError();
                }
                int x = l[ord[i]];
                u = inc(u, x);
                ++i;
            }
            Node[] p = split(u, xAt + 1 - d);
            Node nn = new Node(xAt, p[0].min);
            u = merge(p[0], p[1]);
            if (nn.val != right(u).val) {
                u = merge(u, nn);
            }
            prevx = xAt;
        }
        return n - u.min;
    }

    private static int solveSlow(int d, int[] l, int[] r) {
        int n = l.length;
        int[] din = new int[101];
        for (int i = 0; i < n; ++i) {
            if (l[i] < 0 || r[i] > din.length) {
                throw new AssertionError();
            }
        }
        for (int i = 0; i < din.length; ++i) {
            for (int t = 0; t < n; ++t) {
                if (l[t] <= i && i <= r[t]) {
                    din[i]++;
                }
            }
            for (int j = 0; j + d <= i; ++j) {
                int cur = din[j];
                for (int t = 0; t < n; ++t) {
                    if (j < l[t] && l[t] <= i && i <= r[t]) {
                        cur++;
                    }
                }
                din[i] = Math.max(din[i], cur);
            }
        }
        int ans = 0;
        for (int x : din) {
            ans = Math.max(ans, x);
        }
        return ans;
    }

    public static void main(String[] args) throws IOException {
        boolean t = false;
//        t = true;
        while (t) {
            int n = rnd.nextInt(100) + 1;
            int d = rnd.nextInt(100) + 1;
            int[] l = new int[n];
            int[] r = new int[n];
            for (int i = 0; i < n; ++i) {
                do {
                    l[i] = rnd.nextInt(100);
                    r[i] = rnd.nextInt(100);
                } while (l[i] > r[i]);
            }
            System.err.println("--------");
            if (solveFast(d, l, r) != solveSlow(d, l, r)) {
                System.err.println(n + " " + d);
                for (int i = 0; i < n; ++i) {
                    System.err.println(l[i] + " " + r[i]);
                }
                System.err.println("--------");
                System.err.println(solveFast(d, l, r) + " " + solveSlow(d, l, r));
                throw new AssertionError();
            }
        }

        PrintWriter out = new PrintWriter(System.out);
        solve(new Input(new BufferedReader(new InputStreamReader(System.in))), out);
        out.close();
    }

    static class Input {
        BufferedReader in;
        StringBuilder sb = new StringBuilder();

        public Input(BufferedReader in) {
            this.in = in;
        }

        public Input(String s) {
            this.in = new BufferedReader(new StringReader(s));
        }

        public String next() throws IOException {
            sb.setLength(0);
            while (true) {
                int c = in.read();
                if (c == -1) {
                    return null;
                }
                if (" \n\r\t".indexOf(c) == -1) {
                    sb.append((char)c);
                    break;
                }
            }
            while (true) {
                int c = in.read();
                if (c == -1 || " \n\r\t".indexOf(c) != -1) {
                    break;
                }
                sb.append((char)c);
            }
            return sb.toString();
        }

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

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

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

3 3
-3 0
1 3
-1 2
 4 5
-1 1
2 4
5 9
6 8
 10 1000
280 779
427 613
413 774
822 922
699 700
101 257
236 472
899 940
516 565
328 413
 10 5
552 979
170 792
196 562
267 327
505 736
603 799
143 564
181 286
247 882
727 941
 2 10
-2 2
2 5
 10 500
484 582
285 838
154 763
548 669
721 729
492 610
380 659
700 969
328 563
245 360
 10 7
3 8
6 11
9 14
12 17
15 20
18 23
21 26
24 29
27 32
30 35
 10 50
824 974
436 909
318 924
213 733
416 761
624 641
712 980
316 596
425 627
240 456
 10 5
552 979
170 792
196 562
267 327
505 736
603 799
143 564
122 286
247 882
727 941
 2 10
-2 2
0 5
 10 500
484 582
285 838
154 763
548 669
721 729
492 610
235 659
700 969
328 563
245 360
 10 7
3 8
6 11
9 10
12 17
15 20
18 23
21 26
24 29
27 32
30 35
 3 3
-3 0
1 6
-1 2
 10 500
484 582
285 838
154 1459
548 669
721 729
492 610
235 659
90 969
328 563
245 360
 5 5
566 1551
108 1531
165 562
267 327
505 1178
255 505
278 1016
22 286
336 882
670 1639
 2 5
-1 1
2 4
5 9
6 8
 10 500
484 582
285 838
154 1459
548 669
721 729
492 610
87 405
700 969
328 563
245 360
 4 5
-1 1
2 4
8 9
6 8
 10 5
552 979
170 792
196 562
267 327
505 736
603 799
143 564
122 286
247 882
670 941
 2 10
-2 2
0 4
 10 500
484 582
285 838
154 1459
548 669
721 729
492 610
235 659
700 969
328 563
245 360
 10 5
3 8
6 11
9 10
12 17
15 20
18 23
21 26
24 29
27 32
30 35
 3 3
-3 0
0 6
-1 2
 10 5
552 979
170 792
196 562
267 327
505 1178
603 799
143 564
122 286
247 882
670 941
 2 10
-2 2
0 6
 10 9
3 8
6 11
9 10
12 17
15 20
18 23
21 26
24 29
27 32
30 35
 3 3
-3 -1
0 6
-1 2
 10 5
552 979
170 1531
196 562
267 327
505 1178
603 799
143 564
122 286
247 882
670 941
 2 10
-2 3
0 6
 10 500
484 582
285 838
154 1459
548 669
721 729
431 610
235 659
90 969
328 563
245 360
 3 3
-3 -2
0 6
-1 2
 10 5
552 979
170 1531
196 562
267 327
505 1178
603 799
143 564
122 286
336 882
670 941
 10 500
68 582
285 838
154 1459
548 669
721 729
431 610
235 659
90 969
328 563
245 360
 3 3
-6 -2
0 6
-1 2
 10 5
552 979
170 1531
196 562
267 327
505 1178
603 799
143 564
158 286
336 882
670 941
 10 500
68 582
285 838
154 1459
548 669
721 729
431 610
235 659
90 969
328 563
245 466
 3 3
-1 -2
0 6
-1 2
 10 5
552 979
126 1531
196 562
267 327
505 1178
603 799
143 564
158 286
336 882
670 941
 10 500
68 582
285 838
154 1459
548 669
721 729
431 610
235 659
90 546
328 563
245 466
 3 3
-1 -2
0 5
-1 2
 10 5
552 979
126 1531
165 562
267 327
505 1178
603 799
143 564
158 286
336 882
670 941
 10 500
5 582
285 838
154 1459
548 669
721 729
431 610
235 659
90 546
328 563
245 466
 3 3
-1 -2
0 5
-2 2
 10 5
566 979
126 1531
165 562
267 327
505 1178
603 799
143 564
158 286
336 882
670 941
 10 500
5 582
285 838
154 1459
548 669
721 729
431 610
235 659
1 546
328 563
245 466
 3 3
-1 -2
0 5
-3 2
 10 5
566 1551
126 1531
165 562
267 327
505 1178
603 799
143 564
158 286
336 882
670 941
 10 500
5 582
285 838
154 1459
548 669
721 729
431 610
460 659
1 546
328 563
245 466
 10 5
566 1551
180 1531
165 562
267 327
505 1178
603 799
143 564
158 286
336 882
670 941
 10 500
5 582
285 838
154 1459
548 669
721 729
431 610
460 659
1 812
328 563
245 466
 10 5
566 1551
180 1531
165 562
267 327
505 1178
255 799
143 564
158 286
336 882
670 941
 10 500
5 582
285 838
154 1459
548 669
721 729
431 954
460 659
1 812
328 563
245 466
 10 5
566 1551
180 1531
165 562
267 327
505 1178
255 799
278 564
158 286
336 882
670 941
 10 500
5 582
285 838
154 1459
548 669
721 729
823 954
460 659
1 812
328 563
245 466
 10 5
566 1551
180 1531
165 562
267 327
505 1178
255 505
278 564
158 286
336 882
670 941
 10 500
5 582
285 838
154 1459
548 669
721 729
823 954
460 659
2 812
328 563
245 466
 10 5
566 1551
180 1531
165 562
267 327
505 1178
255 505
278 1016
158 286
336 882
670 941
 10 500
5 582
285 838
154 1459
603 669
721 729
823 954
460 659
2 812
328 563
245 466
 10 5
566 1551
180 1531
165 562
267 327
505 1178
255 505
278 1016
158 286
336 882
670 1639
 10 500
5 1006
285 838
154 1459
603 669
721 729
823 954
460 659
2 812
328 563
245 466
 10 5
566 1551
108 1531
165 562
267 327
505 1178
255 505
278 1016
158 286
336 882
670 1639
 10 500
5 1006
285 838
154 1459
146 669
721 729
823 954
460 659
2 812
328 563
245 466
 10 5
566 1551
108 1531
165 562
267 327
505 1178
255 505
278 1016
13 286
336 882
670 1639
 10 500
5 1006
285 838
154 1459
146 259
721 729
823 954
460 659
2 812
328 563
245 466
 10 5
566 1551
108 1531
165 562
267 327
505 1178
255 505
278 1016
22 286
336 882
670 1639
 10 500
5 1006
285 838
154 1459
226 259
721 729
823 954
460 659
2 812
328 563
245 466
 10 500
5 803
285 838
154 1459
226 259
721 729
823 954
460 659
2 812
328 563
245 466
 4 1000
280 779
427 613
413 774
822 922
699 700
101 257
236 472
899 940
516 565
328 413
 2 10
-2 2
0 3
 10 7
3 8
6 18
9 14
12 17
15 20
18 23
21 26
24 29
27 32
30 35
 10 50
824 974
436 909
318 924
213 733
416 761
624 641
712 942
316 596
425 627
240 456
 10 5
552 979
170 792
196 700
267 327
505 736
603 799
143 564
122 286
247 882
727 941
 2 1
-2 2
0 5
 10 7
3 8
6 11
9 10
12 17
15 20
18 27
21 26
24 29
27 32
30 35
 3 3
-3 0
1 6
0 2
 4 5
-1 1
2 4
8 9
6 10
 2 9
-2 2
0 4
 10 500
484 582
285 838
154 1459
548 669
721 729
492 610
87 659
700 969
328 563
245 360
 10 5
3 8
6 11
9 10
12 17
15 20
18 23
21 26
24 39
27 32
30 35
 10 5
552 979
1 792
196 562
267 327
505 1178
603 799
143 564
122 286
247 882
670 941
 2 10
-2 2
0 2
 10 500
484 582
285 838
154 1459
548 669
721 729
492 610
235 659
90 969
222 563
245 360
 10 9
3 8
5 11
9 10
12 17
15 20
18 23
21 26
24 29
27 32
30 35
 3 3
-3 -1
0 4
-1 2
 3 6
-3 0
0 6
-1 2
 10 5
552 979
170 1531
196 562
267 327
505 1178
603 799
143 564
122 286
336 822
670 941
 3 3
-6 -2
0 6
0 2
 10 500
68 582
285 1560
154 1459
548 669
721 729
431 610
235 659
90 969
328 563
245 466
 3 3
-1 -2
0 6
-1 1
 10 5
552 979
126 1531
196 562
267 327
505 1178
603 799
143 564
158 286
592 882
670 941
 10 500
68 582
285 838
154 1459
548 669
721 729
431 610
235 659
90 546
453 563
245 466
 3 3
-2 -2
0 5
-1 2
 10 500
5 582
285 838
154 1459
548 669
721 729
431 610
235 659
90 639
328 563
245 466
 3 3
-1 -2
0 5
-2 3
 10 3
566 979
126 1531
165 562
267 327
505 1178
603 799
143 564
158 286
336 882
670 941
 10 500
5 582
285 838
154 1459
558 669
721 729
431 610
235 659
1 546
328 563
245 466
 3 3
-1 -2
0 4
-3 2
 10 500
5 582
285 771
154 1459
548 669
721 729
431 610
460 659
1 546
328 563
245 466
 10 5
566 1551
180 1531
165 562
267 378
505 1178
603 799
143 564
158 286
336 882
670 941
 10 5
566 1551
180 1531
165 562
267 327
505 1178
255 799
143 564
158 286
336 1122
670 941
 10 500
5 582
285 838
154 1459
429 669
721 729
431 954
460 659
1 812
328 563
245 466
 10 5
566 1551
180 1531
165 562
267 327
505 1178
255 799
278 564
158 286
94 882
670 941
 10 500
5 582
285 838
154 1459
548 1247
721 729
823 954
460 659
1 812
328 563
245 466
 10 5
566 1551
180 1531
165 562
267 327
322 1178
255 505
278 1016
158 286
336 882
670 1639
 10 500
5 1006
285 838
154 1459
603 669
721 729
823 954
460 659
0 812
328 563
245 466
 10 5
566 1551
108 1531
310 562
267 327
505 1178
255 505
278 1016
158 286
336 882
670 1639
 10 500
5 1006
285 838
154 1078
146 669
721 729
823 954
460 659
2 812
328 563
245 466
 10 5
566 1551
108 1531
165 562
267 327
505 1178
12 505
278 1016
13 286
336 882
670 1639

3
 3
 4
 10
 2
 7
 9
 10
 10
 2
 7
 9
 3
 8
 5
 1
 6
 3
 10
 2
 7
 10
 3
 10
 2
 7
 2
 10
 2
 8
 2
 10
 8
 2
 10
 8
 2
 10
 8
 2
 10
 8
 2
 10
 8
 2
 10
 8
 10
 8
 10
 8
 10
 7
 10
 7
 10
 7
 10
 7
 10
 8
 10
 7
 10
 7
 7
 3
 2
 9
 10
 10
 2
 10
 3
 3
 2
 7
 10
 10
 2
 8
 7
 2
 3
 10
 2
 8
 2
 10
 8
 2
 8
 2
 10
 8
 2
 8
 10
 10
 9
 10
 8
 10
 7
 10
 8
 10
