Under construction

Still being built. Things may change

Date Pickup

Time limit8sMemory limit512 MB

Summary
Given a directed road network and a call window [a, b], find the smallest worst-case time Janet waits at intersection n when Richard routes optimally from intersection 1.
Level

Medium7 of 10

Topics
Graph, Shortest path
Solved
No attempts yet

Problem

Richard and Janet are going on their first date. Richard offered to meet her at home with his bicycle. Janet says she will call when she is ready, in 10 to 20 minutes. Richard is impatient. He can wait at home for her signal, or he can leave early and ride around the neighbourhood so that he reaches her as quickly as possible after she calls. Once he is on his bicycle, he will not ride slower than the legal speed limit, stop at intersections, or wait outside Janet's house. He does not mind passing by her house and coming back to it later.

The neighbourhood is a directed graph. Richard wants a route, after an optional wait at his own house, that minimizes the time Janet has to wait in the worst case. He can travel for as long as he likes and visit each intersection as many times as he likes.

Janet calls Richard as soon as she is ready, and then Richard takes the shortest path to her. Richard does not know exactly when she will be ready, only that it will be between aa and bb minutes, not necessarily a whole number of minutes.

If Richard passes an intersection at the exact instant Janet calls, the call counts as happening first, so he can choose his next move after hearing it. For example, if he is passing her house when she calls, he can stop there immediately and she does not wait.

Janet may never wait ww minutes, yet she may wait w−εw - \varepsilon minutes for an arbitrarily small ε>0\varepsilon > 0 if she calls at an unlucky moment, such as a few nanoseconds after Richard leaves an intersection. In that case the worst case waiting time is still ww.

Input

The first line has two integers aa and bb (0≤a≤b≤10120 \le a \le b \le 10^{12}), meaning Janet will be ready in at least aa minutes and at most bb minutes.

The second line has two integers nn and mm (2≤n≤m≤1052 \le n \le m \le 10^5), the number of intersections and the number of roads. Intersections are numbered from 1 to nn.

Each of the next mm lines has three integers uu, vv, and tt (1≤u,v≤n1 \le u, v \le n, 1≤t≤1061 \le t \le 10^6), meaning there is a one-way road from uu to vv that takes exactly tt minutes to travel.

Richard's house is intersection 1, and Janet's house is intersection nn. Janet's house can be reached from Richard's house, and every intersection has at least one outgoing road, possibly a loop back to itself.

Output

Print the worst case waiting time for Janet, given that she will be ready at some time between aa and bb minutes and Richard plans his route optimally.

The worst case waiting time is always an integer.

Examples2

  1. Example 1

    Input
    10 20
    3 5
    1 3 7
    2 1 1
    2 3 2
    2 3 5
    3 2 4
    
    Expected output
    6
    
  2. Example 2

    Input
    4 10
    5 7
    1 4 6
    4 5 5
    4 5 3
    5 5 30
    1 2 1
    2 3 1
    3 2 1
    
    Expected output
    5