PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000424. Cuộc gọi trên đường đi (Call During the Journey)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Cuộc gọi trên đường đi (Call During the Journey)

Call During the Journey

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Your city has nn intersections numbered 11 through nn and mm bidirectional streets. Every street joins two distinct intersections, no pair is joined by more than one street, and every intersection is reachable from every other one.

Street ii connects uiu_i and viv_i. Traversing it takes li1l_{i1} minutes by bus or li2l_{i2} minutes on foot. Buses may depart at any integer minute; boarding and leaving a bus are allowed only at intersections. Waiting at intersections is allowed.

You are at home, intersection 11, at time 00. You must arrive at intersection nn by the event deadline t0t_0. A scheduled call occupies the interval from t1t_1 to t2t_2. During the call you may walk, wait, or stay home, but may not ride a bus. A ride ending exactly at t1t_1 or beginning exactly at t2t_2 is permitted.

Find the latest integer departure time from home that meets the deadline and respects the call restriction.

Input

The first line contains integers n,mn,m, the numbers of intersections and streets.

The second line contains integers t0,t1,t2t_0,t_1,t_2 (in minutes), the event deadline, call start, and call end.

Each of the next mm lines contains four integers ui,vi,li1,li2u_i,v_i,l_{i1},l_{i2}: the two endpoints, bus travel time, and walking time for street ii.

Output

Print one integer: the latest possible departure time, measured from time 0. Print -1 if the event cannot be reached by the deadline.

Subtasks

  • Subtask 1 (20 points): 2≤n≤302\le n\le 30; 1≤m≤1001\le m\le 100; 1≤t1<t2<t0≤1001\le t_1<t_2<t_0\le 100; 1≤ui,vi≤n1\le u_i,v_i\le n; ui≠viu_i\ne v_i; 1≤li1<li2≤1091\le l_{i1}<l_{i2}\le 10^9; for every 1≤i≤m1\le i\le m. The undirected graph is connected, with no parallel edges.
  • Subtask 2 (30 points): 2≤n≤10002\le n\le 1000; 1≤m≤50001\le m\le 5000; 1≤t1<t2<t0≤10000000001\le t_1<t_2<t_0\le 1000000000; 1≤ui,vi≤n1\le u_i,v_i\le n; ui≠viu_i\ne v_i; 1≤li1<li2≤1091\le l_{i1}<l_{i2}\le 10^9; for every 1≤i≤m1\le i\le m. The undirected graph is connected, with no parallel edges.
  • Subtask 3 (50 points): 2≤n≤1000002\le n\le 100000; 1≤m≤1000001\le m\le 100000; 1≤t1<t2<t0≤10000000001\le t_1<t_2<t_0\le 1000000000; 1≤ui,vi≤n1\le u_i,v_i\le n; ui≠viu_i\ne v_i; 1≤li1<li2≤1091\le l_{i1}<l_{i2}\le 10^9; for every 1≤i≤m1\le i\le m. The undirected graph is connected, with no parallel edges.

Examples

Input 1

2 1
10 4 8
1 2 3 7

Output 1

3

Explanation 1

A bus can depart at 1 and arrive at 4. Walking takes 7 minutes, so departure at 3 reaches the event at 10. The first bus after the call arrives at 11. The latest departure is 3.

Input 2

3 2
12 4 8
1 2 2 6
2 3 4 9

Output 2

2

Explanation 2

Leave at 2, reach intersection 2 at 4 by bus, wait until 8, and take the 4-minute bus to intersection 3 at 12. A later departure cannot reach intersection 2 by 4 by bus or by 8 on foot, so the deadline cannot be met.

Input 3

2 1
5 1 4
1 2 3 6

Output 3

-1

Explanation 3

Walking needs 6 minutes, exceeding the deadline even from time 0. A 3-minute bus cannot finish by time 1, and the first bus after the call arrives at 7. No feasible trip exists.