Under construction

Still being built. Things may change

Jump

Time limit1sMemory limit1024 MB

Summary
Find the fewest upward jumps from any floor 1 platform to any floor K platform, where each jump lands on the first platform above.
Level

Medium7 of 10

Topics
BFS, Graph, Sorting
Solved
No attempts yet

Problem

A simple jump game. The character picks one platform on floor 1 and can start at any integer position on it. Each turn, the character takes one of two actions, and the goal is to reach the highest platform with the fewest jumps.

  1. Move freely on the current platform so that the position stays an integer. That is, move left or right by 1.
  2. Jump straight up from the current platform. The character lands on the first platform the jump meets. That is, draw an upward ray from the current position and move to the platform where it first intersects.

Consider the example below.

Floor 1 has 2 platforms. The character can start on either the right or the left platform of floor 1. In that case, jumping once from the right platform to floor 4 is optimal. Also, under the jump rule, if the character stands on the right platform of floor 1 and jumps from position 8, it reaches a platform on floor 2.

Input

The first line contains the number of platforms NN and the floor KK of the highest platform. The next NN lines each contain three integers LiL_i, RiR_i, and KiK_i. This means the ii-th platform spans from LiL_i to RiR_i and is on floor KiK_i. No two platforms on the same floor overlap. It is guaranteed that floor 1 and floor KK each have at least one platform.

Output

Print the minimum number of jumps needed to start on any platform on floor 1 and arrive at any platform on floor KK. If there is no way to reach it, print -1.

Constraints

  • 1≤N,K≤300 0001 \le N, K \le 300\,000
  • 1≤Li<Ri≤1091 \le L_i < R_i \le 10^9, 1≤Ki≤K1 \le K_i \le K

Examples1

  1. Example 1

    Input
    9 4
    1 5 1
    1 2 2
    4 7 2
    8 10 2
    1 6 3
    9 10 3
    1 4 4
    8 12 1
    8 12 4
    
    Expected output
    1