In the Kingdom of Graphonia, there are towns connected by bidirectional roads, with each pair of towns connected by at most one road. The royal castle is located in the first town (that is, town ), and the network is connected, meaning it is possible to travel from the royal castle to any other town using the roads.
To minimize maintenance costs, the king has tasked the royal engineers with closing some roads in the kingdom while ensuring the following requirements are met:
- The network must stay connected.
- The royal castle must have exactly direct connections to other towns.
- The total maintenance cost of the remaining roads must be minimized.
Task
Help the royal engineers determine the total maintenance cost of the optimized network. If it is impossible to close some of the roads in the required way, write .
Input data
The first line contains , and .
The -th of the following lines contains three integers, , and , representing a road between towns and with a maintenance cost .
Output data
You need to write a single line with an integer: the unique integer that solves this task.
Constraints and clarifications
- , where is the number of roads from the royal castle to other towns.
- , for each .
- for each .
- There is at most one edge between any two nodes and the edges form a connected graph.
# | Points | Constraints |
---|---|---|
1 | 0 | Examples |
2 | 19 | for each where . |
3 | 23 | |
4 | 58 | No additional constraints. |
Example 1
stdin
5 6 2
1 2 3
1 3 1
1 4 4
2 5 1
3 5 5
4 5 9
stdout
11
Explanation
The first and sixth road of the input must be closed.
Example 2
stdin
4 3 2
1 2 2
1 3 7
1 4 1
stdout
-1
Explanation
There is no way to satisfy the requirements.