In the distant future the organizers of Tour de France have relocated the competition to Treeland. Treeland consists of cities, numbered from to , and bidirectional roads numbered from to . Road connects city to city and has length . It's possible to reach every city from every other city using these roads. Due to strange socio-economical factors the cities with only one road adjacent to them have become metropolises.
The rules of the competition are the following:
- The cyclists start from a metropolis chosen by the organizers.
- They visit all cities using the roads in some way. They may visit the same city and use the same road multiple times.
- They finish in a metropolis chosen by the organizers.
The organizers choose metropolises, where is either or . If , then the chosen metropolis is used to start as well as to finish the race.
If , then two different metropolises are chosen, one of them serving as the start, the other as the finish of the race.
Given the map of Treeland and the value of , what is the minimum total length of the roads the cyclists have to cycle through
from start to finish, if both the organizers and the cyclists want to minimize this sum?
Input data
The first line contains two integers and . The next lines contains three integers each, the numbers , and for road .
Output data
You need to write a single line with an integer: the minimal sum of road lengths that the cyclists need to traverse.
Constraints and Clarifications
- .
- .
- for each .
- for each .
# | Score | Restrictions |
---|---|---|
1 | 0 | Examples. |
2 | 20 | and . |
3 | 30 | and . |
4 | 50 | No additional limitations. |
Example 1
stdin
3 1
1 2 4
2 3 5
stdout
18
Explanation
In the first sample case the value of is . Choosing the metropolis with index results in the cyclists taking the path which has a length of .
Example 2
stdin
4 2
1 2 10
2 3 10
2 4 11
stdout
41
Explanation
In the second sample case , and it is optimal to choose metropolises and .