Andrew and William are playing a game involving an array of integers. At the beginning of the game, Andrew is shown an array of distinct positive integers:
. However, William does not get to see the array.
Instead, Andrew makes statements about the array. Each statement is in the form of three integers
L R X
, which means the minimum value among the elements from index to
(inclusive) is
.
However, Andrew is not always honest, and some of his statements may contradict previous ones. William wants to determine whether all of Andrew's statements can be true at the same time — i.e., whether they are mutually consistent.
Your task is to help William by analyzing the statements and identifying the first one (by input order) that contradicts any earlier ones. If all statements are consistent, print
0
.
Input Specification
The first line includes two integers, and
(
,
), the size of the array and the number of statements Andrew made.
Each of the following lines contains three integers,
, representing one of Andrew's statement that the minimal value among
to
is
(
,
)
Output Specification
Print if Andrew's statements are all consistent. Otherwise, print the index from
to
of the earliest statement which is inconsistent with statements before it.
Constraints
Subtask | Points | Additional constraints |
---|---|---|
No additional constraints |
Sample Input 1
18 4
2 10 6
5 18 6
3 11 9
11 15 12
Sample Output 1
3
Sample Input 2
5 3
2 4 3
1 4 1
4 4 5
Sample Output 2
0
Comments