Given a DAG with vertices and arcs with unique positive integer labels on the arcs, find the smallest lexicographic path (considering the labels on the arcs, not the numbers of the vertices) between 0 and .
A DAG (directed acyclic graph) is a directed graph without cycles. Given two sequences of integers and , we say is lexicographically smaller than when, for the first such that , we have that , or when in case that no such exists.
Input consists of several cases. Every case consists of and , followed by triples meaning that there is an arc from to with label . Assume , , , that vertices are numbered between 0 and , , and that there is no more than one arc from to . All are distinct in every given case.
For every case, print the smallest lexicographic path between
and
.
Print the labels separated by spaces. If there is no path between
and
,
print -1.
Input
3 3 0 1 100 1 2 300 0 2 200 4 5 2 3 50 1 2 20 0 1 10 1 3 30 0 2 40 2 1 1 0 1000000000
Output
100 300 10 20 50 -1