Jump
Time limit1sMemory limit1024 MB
Find the fewest upward jumps from any floor 1 platform to any floor K platform, where each jump lands on the first platform above.
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.
- Move freely on the current platform so that the position stays an integer. That is, move left or right by 1.
- 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 and the floor of the highest platform. The next lines each contain three integers , , and . This means the -th platform spans from to and is on floor . No two platforms on the same floor overlap. It is guaranteed that floor 1 and floor 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 . If there is no way to reach it, print -1.
Constraints
- ,