문제
Farmer John's
After weeks of study, Farmer John has made
Farmer John's observations are prioritized, so his goal is to maximize the value of
Please help Farmer John determine the best order in which to milk his cows.
Problem credits: Jay Leeds
입력
The first line contains
출력
Output
예제
4 3
3 1 2 3
2 4 2
3 3 4 1
1 4 2 3
Here, Farmer John has four cows and should milk cow 1 before cow 2 and cow 2 before cow 3 (the first observation), cow 4 before cow 2 (the second observation), and cow 3 before cow 4 and cow 4 before cow 1 (the third observation). The first two observations can be satisfied simultaneously, but Farmer John cannot meet all of these criteria at once, as to do so would require that cow 1 come before cow 3 and cow 3 before cow 1.
This means there are two possible orderings: 1 4 2 3 and 4 1 2 3, the first being lexicographically smaller.