Under construction

Still being built. Things may change

Genius Walk

Time limit2sMemory limit1024 MB

Summary
Find the earliest time Sam can reach city t from s when each road has periodic rain and he cannot stop midway along a road.
Level

Medium7 of 10

Topics
Shortest path, Graph, Math
Solved
No attempts yet

Problem

Sam found nn cities in a new region, connected by mm two-way roads. Sam can only move along the roads. He needs to get from city ss to city tt without being caught in temporal rain. According to the weather forecast, rain falls over the ii-th road during the time intervals [(ai+bi)⋅k+ai,(ai+bi)⋅(k+1)][(a_i + b_i) \cdot k + a_i, (a_i + b_i) \cdot (k + 1)] for all integers kk (aia_i and bib_i are positive). To travel along the ii-th road, Sam needs did_i units of time, and there must be no rain over that road during the entire trip. Sam can take shelter from the rain in cities, so he can be in a city at any time. Sam can leave a city onto a road at the moment the rain stops, and enter a city from a road at the moment the rain starts.

At time 00, Sam is in city ss. Find the earliest moment at which he can be in city tt.

Input

The first line contains four integers nn, mm, ss, and tt: the number of cities, the number of roads, the starting city, and the destination city (1≤n≤1000001 \le n \le 100000; 0≤m≤2000000 \le m \le 200000; 1≤s,t≤n1 \le s, t \le n). Each of the next mm lines describes a road with five integers uiu_i, viv_i, aia_i, bib_i, and did_i (1≤ui,vi≤n1 \le u_i, v_i \le n; 1≤ai,bi,di≤1091 \le a_i, b_i, d_i \le 10^9). Road ii connects cities uiu_i and viv_i.

Output

If Sam cannot get from city ss to city tt, print −1-1. Otherwise, print the minimum moment at which he can be in city tt.

Examples1

  1. Example 1

    Input
    3 2 1 3
    1 2 3 4 1
    2 3 2 3 2
    
    Expected output
    7