You have won a lottery to go sightseeing at Santa's North Pole! Being very excited about this trip, you have procured a map of the sightseeing location and are planning the optimal route.
The North Pole is represented as a collection of locations numbered from to , and tour routes between these locations. Each tour route is bidirectional and has a certain time that it takes to traverse. There is also a start location where the tour bus will drop you off and an end location where you will be picked up and flown home. Lastly, to make the North Pole more navigable, the designers ensured that every location is part of at most one cycle.
You wish to minimize the time you spend as you have an important CS club meeting right after the trip. However, you also want to see many locations, without going through much navigation. With this in mind, you have decided on a specific set of requirements for the optimal route:
- The route must start and end at locations and respectively.
- The route must pass through a simple cycle, which is defined as a non-empty path in which only the first and last vertices are equal, and there are no repeated edges or vertices (excluding the first and last vertex).
- The route must be as short as possible in terms of the total traversal time while meeting requirements 1 and 2.
Given the map of the North Pole sightseeing location, and the locations and , find the traversal time of the route that meets the given requirements.
Constraints
As mentioned in the problem statement, it is guaranteed that every location will be part of at most one simple cycle.
Input Specification
The first line will contain two space-separated integers, and , representing the number of locations and tour routes, respectively.
Each of the next lines will contain three space-separated integers, , , (such that ) and , indicating that there is a tour route between locations and that takes minutes to traverse.
The final line will contain two space-separated integers, and (such that ), indicating the start and end locations, respectively.
Output Specification
Output a single integer on a single line: the traversal time of the route that meets the requirements in minutes. If there is no possible route that meets the requirements, output -1
.
Sample Input 1
10 11
1 2 5
2 3 4
3 9 9
4 5 7
6 7 2
7 9 4
6 8 5
1 6 3
5 10 2
10 4 6
9 4 2
1 8
Sample Output 1
35
Explanation of Sample 1
This is the graph given in this sample case:
An optimal path that starts at location and ends at location while meeting the requirement is as follows:
By examining the times associated with the traversed routes, it is evident that the total time is minutes.
Sample Input 2
7 6
1 4 8
5 4 2
5 1 6
2 7 3
7 3 2
3 6 7
2 6
Sample Output 2
-1
Explanation of Sample 2
This is the graph given in this sample case:
It can be observed that there is no path from location to location that passes through a simple cycle, so the correct output is -1
.
Comments