#P6983. King Karl's Royal Journey
King Karl's Royal Journey
King Karl's Royal Journey
King Karl is a responsible and diligent ruler. Each year he makes a journey across his country which has \(n\) cities and \(m\) unidirectional roads. The capital is city 1. King Karl intends to start at the capital, visit every non-capital city exactly once, and then return to the capital.
The roads are one-way; that is, a road from city \(a\) to city \(b\) can only be traversed from \(a\) to \(b\) and not the reverse.
Your task is to determine a valid route satisfying the above conditions, or report that such a route does not exist. If a route exists, output the sequence of city numbers representing the journey. If no route exists, output -1.
inputFormat
The first line contains two integers \(n\) and \(m\) where \(n\) is the number of cities and \(m\) is the number of roads. The cities are numbered from 1 to \(n\), with city 1 being the capital.
Each of the following \(m\) lines contains two integers \(a\) and \(b\), representing a directed road from city \(a\) to city \(b\).
outputFormat
If a valid route exists, output the route as a sequence of \(n+1\) space-separated integers (starting and ending at city 1, and all other cities visited exactly once). Otherwise, output -1.
sample
4 6
1 2
2 3
3 4
4 1
1 3
2 4
1 2 3 4 1