Statement
You are given a weighted directed graph with vertices. The weight of the edge from vertex to vertex is . If , that edge does not exist.
Arrange all vertices in some order, follow the directed edges between consecutive vertices, and return from the last vertex to the first. Every edge used must exist. Find any such order that minimizes the sum of the weights of these edges.
Input
The input is given in the following format:
Each case is given in the following format:
Output
For each case, print only if no valid order exists.
Otherwise, print its minimum total weight on the first line and the vertex numbers on the second line, separated by spaces. The vertices must form a permutation of through . Include the edge from back to in the total weight. If several answers exist, print any of them.
Constraints
- .
- .
- , where means that the edge does not exist.
- .
- The sum of over all cases is at most .
- The sum of over all cases is at most .