1486. Bus Trip

单点时限: 2.0 sec

内存限制: 256 MB

There are N towns, and M one-way direct bus routes (no intermediate stops) between them. The towns are numbered from 1 to N. A traveler who is located in the town 1 at time 0 needs to arrive in the town P. He will be picked from the bus station at town P exactly at time T. If he arrives earlier he will have to wait.

For each bus route i, we know the source and destination towns si and ti, of course. We also know the departure and arrival times, but only approximately: we know that the bus departs from si within the range [ai, bi] and arrives at ti within the range [ci, di] (endpoints included in both cases).

The traveler does not like waiting, and therefore is looking for a travel plan which minimizes the maximal possible waiting time while still guaranteeing that he’ll not miss connecting buses (that is, every time he changes buses, the latest possible arrival of the incoming bus must not be later than the earliest possible departure time of the out¬going bus).

When counting waiting time we have to assume the earliest possible arrival time and the latest possible departure time.

Write a program to help the traveler to find a suitable plan.

输入格式

The first line contains the integer numbers N (1 <= N <= 50,000), M (1 <= M <= 100,000), P ( 1 <= P <= N), and T (0 <= T <= 1,000,000,000).

The following M lines describe the bus routes. Each line contains the integer numbers si, ti, ai, bi, ci, di, where si and ti are the source and destination towns of the bus route i, and ai, bi, ci, di describe the departure and arrival times as explained above (1 <= si <= N, 1 <= ti <= N, 0 <= ai <= bi < ci <= di <= 1,000,000,000).

输出格式

The only line of the output should contain the maximal possible total waiting time for the most suitable possible travel plan. If it is not possible to guarantee arrival in town P by time T, the line should contain –1.

样例

Input
3 6 2 100
1 3 10 20 30 40
3 2 32 35 95 95
1 1 1 1 7 8
1 3 8 8 9 9
2 2 98 98 99 99
1 2 0 0 99 101
Output
32
Hint:
The most pessimistic case for the optimal travel plan for the above example is as follows:
Time Action
0…1 Wait in town 1
1…7 Take the bus line 3 from town 1 to town 1
7…8 Wait in town 1
8…9 Take the bus line 4 from town 1 to town 3
9…35 Wait in town 3
35…95 Take the bus line 2 from town 3 to town 2
95…98 Wait in town 2
98…99 Take the bus line 5 from town 2 to town 2
99…100 Wait in town 2

1 人解决,2 人已尝试。

1 份提交通过,共有 4 份提交。

9.8 EMB 奖励。

创建: 17 年,3 月前.

修改: 7 年,2 月前.

最后提交: 1 年前.

来源: BOI 2005

题目标签