Fax McClad, Croneria's most diligent bounty hunter, is waiting for Dankey Kang to exit his hideout so he can capture him. In order to avoid capture, Dankey Kang asks for your assistance.
Dankey Kang needs to travel from his initial hideout to another hideout to deliver goods (they're green). A straight road with traffic lights connects the two hideouts, numbered in order from to . Each traffic light has a cycle length of seconds. The traffic light is green (meaning that Dankey Kang can go, but he may choose not to, angering the other drivers on the road) for seconds, and red (meaning that Dankey Kang must stop) for the remaining seconds. Each light's cycle repeats indefinitely and initially, the light is seconds into its cycle (a light with has just turned green). In the case that Dankey Kang arrives at a light at the same time it changes color, he will obey the new color.
Also, for the traffic light from light to light , seconds are required to travel to the next traffic light. Dankey Kang's initial hideout is located just before the first light and he reaches the other hideout as soon as he passes the light. In other words, no time is required to reach the first light from the initial hideout or to reach the other hideout from the light.
Dankey Kang wants to minimize the amount of time he spends exposed on the road. Anytime he spends waiting in the initial hideout is not counted, but he must wait in his hideout for a non-negative integer number of seconds. Can you tell him the minimum amount of time necessary to reach the destination?
Constraints
In all subtasks:
Subtask | Points | ||
---|---|---|---|
1 | 20 | ||
2 | 20 | ||
3 | 60 |
Input Specification
The first line of input will contain two space-separated integers, and .
lines of input follow. The line will contain 2 space separated integers, and .
lines of input follow. The line will contain .
Output Specification
On a single line, output the minimum amount of time Dankey Kang must spend on the road.
Sample Input
5 10
4 2
7 3
3 6
5 2
8 0
1
2
3
4
Sample Output
11
Explanation for Sample Output
Initially, the traffic lights are at the following seconds in their cycle: .
An optimal strategy for Dankey Kang begins with waiting for second. Note that this second will not be counted in the final answer.
The lights are now at , so Dankey Kang can travel from the first light to the second light, which requires second to travel.
The lights are now at , so Dankey Kang can continue traveling, without stopping, to the third light, which requires seconds to travel.
The lights are now at , so Dankey Kang can continue traveling, without stopping, to the fourth light, which requires seconds to travel.
The lights are now at . Dankey Kang must wait second for the light to turn green before going to the fifth light, which requires seconds to travel.
The lights are now at , so Dankey Kang can continue traveling, without stopping, to the destination hideout. The total time that Dankey Kang takes for this route is .
Comments