Under construction

Still being built. Things may change

The Phoenix That Topples the School

Time limit2sMemory limit1024 MB

Summary
Given bricks stacked on floors, find the most bricks that can fall after removing one brick, when unsupported bricks collapse floor by floor.
Level

Hard8 of 10

Topics
Dynamic programming, Stack, Tree, Intervals
Solved
No attempts yet

Problem

Phoenix was practicing aerobatics in the sky one day when he suddenly decided that the buildings of POSTECH were in the way of his flight. Wanting to fly more freely, he resolved to bring down the school buildings!

The buildings of POSTECH are structured as follows.

  • Each building is built by stacking rectangular bricks. All bricks have height 11, but their widths can differ. Two bricks on the same floor cannot overlap.
  • The building has HH floors in total, each of height 11. Every floor has at least one brick.
  • Every brick in the building is either on floor 11 or directly touches a brick on the floor just below it. Bricks that touch only at a corner do not count as touching. A brick that fails this condition is said to be floating in the air.

In the figure below, building A satisfies all of the conditions above. Building B is not valid because brick 7 is floating in the air.

Building A

Building B

A solidly built building is not easy to topple in an instant. So Phoenix wants to remove exactly 11 brick to bring down as many bricks as possible. The building collapses as follows.

  1. Phoenix picks and removes one brick on floor hh.
  2. Any brick on floor h+1h+1 that touched only the removed bricks on floor hh is now floating. Such bricks are removed immediately, since no floating brick may remain.
  3. In the same way, the bricks removed in step 2 can leave some bricks on floor h+2h+2 floating, and those are removed immediately as well.
  4. This chain continues one floor at a time until the building collapses. The process ends when no floating brick remains or when the top floor is reached.

Phoenix wants to know in advance how many bricks will fall before he acts. Find the maximum number of bricks that can be brought down on Phoenix's behalf. The brick removed first is also counted among the bricks that fall.

Input

The first line contains the number of bricks NN and the height of the building HH, separated by a space. (1≤N,H≤5000001 \leq N, H \leq 500000)

Each of the next NN lines contains three integers hih_i, lil_i, and rir_i, separated by spaces. This means that brick ii is on floor hih_i, its left end is at x-coordinate lil_i on the coordinate plane, and its right end is at x-coordinate rir_i. (1≤hi≤H1 \leq h_i \leq H; 0≤li<ri≤1090 \leq l_i < r_i \leq 10^9)

It is guaranteed that every floor has at least one brick, no two bricks on the same floor overlap, and no brick is floating in the air.

Output

Print the maximum number of bricks that can be brought down by removing exactly one brick.

Examples2

  1. Example 1

    Input
    5 3
    1 1 4
    1 6 8
    2 0 2
    3 1 3
    2 3 7
    
    Expected output
    3
    
  2. Example 2

    Input
    9 3
    1 0 1
    1 1 5
    1 5 6
    1 6 7
    2 0 2
    2 2 3
    2 3 4
    2 4 7
    3 0 7
    
    Expected output
    3