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
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
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,
Each of the next
The final line will contain two space-separated integers,
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
By examining the times associated with the traversed routes, it is evident that the total time is
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 -1
.
Comments