atcoder#ABC192E. [ABC192E] Train

[ABC192E] Train

Score : 500500 points

Problem Statement

In the Republic of AtCoder, there are NN cities numbered 11 through NN and MM railroads numbered 11 through MM.

Railroad ii connects City AiA_i and City BiB_i bidirectionally. At time 00, KiK_i, and all subsequent multiples of KiK_i, a train departs from each of these cities and head to the other city. The time each of these trains takes to reach the destination is TiT_i.

You are now at City XX. Find the earliest time you can reach City YY when you start the journey by taking a train that departs City XX not earlier than time 00. If City YY is unreachable, report that fact. The time it takes to transfer is ignorable. That is, at every city, you can transfer to a train that departs at the exact time your train arrives at that city.

Constraints

  • 2N1052 \leq N \leq 10^5
  • 0M1050 \leq M \leq 10^5
  • 1X,YN1 \leq X,Y \leq N
  • XYX \neq Y
  • 1Ai,BiN1 \leq A_i,B_i \leq N
  • AiBiA_i \neq B_i
  • 1Ti1091 \leq T_i \leq 10^9
  • 1Ki1091 \leq K_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN MM XX YY

A1A_1 B1B_1 T1T_1 K1K_1

\vdots

AMA_M BMB_M TMT_M KMK_M

Output

Print the earliest possible time you can reach City YY. If City YY is unreachable, print -1 instead.

3 2 1 3
1 2 2 3
2 3 3 4
7

First, you take Railroad 11 at time 00 and go from City 11 to City 22. You arrive at City 22 at time 22.

Then, you take Railroad 22 at time 44 and go from City 22 to City 33. You arrive at City 33 at time 77.

There is no way to reach City 33 earlier.

3 2 3 1
1 2 2 3
2 3 3 4
5

First, you take Railroad 22 at time 00 and go from City 33 to City 22. You arrive at City 22 at time 33.

Then, you take Railroad 11 at time 33 and go from City 22 to City 11. You arrive at City 11 at time 55.

3 0 3 1
-1
9 14 6 7
3 1 4 1
5 9 2 6
5 3 5 8
9 7 9 3
2 3 8 4
6 2 6 4
3 8 3 2
7 9 5 2
8 4 1 9
7 1 6 9
3 9 9 3
7 5 1 5
8 2 9 7
4 9 4 4
26