After becoming the overlord of the new world, Sora realized that it was imperative to optimize the transportation system of his country in order to manage such a big empire. As a new nation, the country is arranged in a very cost efficient manner: There are cities, and bidirectional highways connecting the cities, allowing anyone in one city to reach another city by traversing a series of highways. In order to revamp the transportation, Sora has decided to first invest some amount of time into Research and Development before revising the highways, to perhaps develop technologies to make traversing highways more efficient.
For each highway, if the country invested at least hours into R&D prior to beginning the rollout, it will take hours to traverse the highway. Otherwise, it will take hours to traverse it instead. Furthermore, Sora has set a strict goal for this operation: it should take at most hours to traverse from any one city to another. Of course, he would like to minimize the amount of time that he needs to invest in R&D while reaching his goal.
As his dear Shiro, you must find out how many hours he should pour into R&D!
Constraints
In all tasks,
Subtask 1 [30%]
Subtask 2 [70%]
No additional constraints.
Input Specification
The first line will contain two integers, and .
The next lines will contain 5 integers, , , , , . The represents a highway joining and , requiring hours of R&D to take hours to traverse, and hours otherwise.
Output Specification
One integer, the optimal number of hours to put into R&D to ensure that we can traverse between any two cities in at most hours. If this is impossible, print out instead.
Sample Input
4 5
1 2 2 1 3
2 3 4 1 2
1 4 2 2 4
Sample Output
2
Sample Explanation
If 2 hours are put into R&D, the longest path has length . This is less than or equal to , so it is valid. Any less, and the longest path will have length . So, it is optimal to invest hours into R&D.
Comments