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

#BS0000423. Đến đúng giờ (Arriving on Time)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Đến đúng giờ (Arriving on Time)

Arriving on Time

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

You are working at a hotel in Zürich before a meeting. You must reach the meeting venue by time ss, but wish to remain at the hotel as long as possible. All times are measured in seconds from the present, time 00.

The city has nn tram stops numbered 00 through n−1n-1. The hotel is at stop 00 and the meeting venue is at stop n−1n-1.

A tram line runs from stop uu to stop vv. Its first departure is at t0t_0, with subsequent departures spaced pp seconds apart. Each ride takes exactly dd seconds. Thus departures occur at t0,t0+p,t0+2p,…t_0,t_0+p,t_0+2p,\ldots.

You may wait at a stop. Transfers take no time, so arriving exactly when another tram departs allows you to board it. Find the latest integer time to leave the hotel and still reach the venue no later than ss.

Input

The first line contains integers n,m,sn,m,s, the numbers of stops and lines and the meeting deadline.

Each of the next mm lines contains five integers u,v,t0,p,du,v,t_0,p,d: departure stop, arrival stop, first departure time, departure interval, and ride duration, in seconds where applicable.

Output

Print the latest departure time from the hotel as an integer in seconds from now. If timely arrival is impossible even when leaving at time 00, print impossible.

Subtasks

  • Subtask 1 (20 points): 2≤n≤302\le n\le 30; 1≤m≤1001\le m\le 100; 1≤s≤1001\le s\le 100; 0≤u,v<n0\le u,v<n, u≠vu\ne v; 0≤t0≤1090\le t_0\le 10^9; 1≤p,d≤1091\le p,d\le 10^9. Each line travels only from uu to vv.
  • Subtask 2 (30 points): 2≤n≤1002\le n\le 100; 1≤m≤5001\le m\le 500; 1≤s≤1091\le s\le 10^9; 0≤u,v<n0\le u,v<n, u≠vu\ne v; 0≤t0≤1090\le t_0\le 10^9; 1≤p,d≤1091\le p,d\le 10^9. Each line travels only from uu to vv.
  • Subtask 3 (50 points): 2≤n≤1052\le n\le 10^5; 1≤m≤2⋅1051\le m\le 2\cdot 10^5; 1≤s≤1091\le s\le 10^9; 0≤u,v<n0\le u,v<n, u≠vu\ne v; 0≤t0≤1090\le t_0\le 10^9; 1≤p,d≤1091\le p,d\le 10^9. Each line travels only from uu to vv.

Examples

Input

2 1 10
0 1 1 2 6

Output

3

Explanation

The meeting starts at second 10. Trams from 0 to 1 depart at 1, 3, 5, ... and take 6 seconds. The departure at 3 arrives at 9; the next at 5 arrives at 11, too late. Hence 3 is the latest hotel departure.

Input

2 1 5
0 1 1 1 5

Output

impossible

Explanation

The deadline is 5. The earliest tram departs at 1 and takes 5 seconds, reaching the venue at 6. No trip meets the deadline.

Input

3 3 12
0 1 0 3 2
1 2 2 4 2
0 2 0 1 10

Output

6

Explanation

Leave at 6, reach stop 1 at 8, wait until 10, and reach stop 2 at exactly 12. Leaving after 6 means the first available ride to stop 1 departs at 9 and arrives at 11, missing the departure at 10. The direct line takes 10 seconds and requires leaving by 2. Thus 6 is optimal.