#P5957. [POI 2017] Flappy Bird
[POI 2017] Flappy Bird
Background
Flappy Bird is a very popular casual game.
Problem Description
In the game, the bird starts at . Its goal is to reach some point whose -coordinate is .
Every second, you may choose to tap the screen. If you tap, the bird moves from to . If you do not tap, the bird moves from to .
There are also obstacles in the game, each described by a triple . This means that on the vertical line , the parts with or are obstacles. Touching an obstacle or even just grazing its boundary is considered a failure.
Now, find the minimum number of taps needed for the bird to fly from to the destination.
Input Format
The first line contains two integers .
The next lines each contain three integers . The testdata guarantees that .
Output Format
If it is impossible to reach the destination no matter what you do, output NIE. Otherwise, output the minimum number of taps.
4 11
4 1 4
7 -1 2
8 -1 3
9 0 2
5
Hint
For of the testdata, , , , .
Sample Explanation.

Translated by ChatGPT 5