eolymp
bolt
Try our new interface for solving problems
Problems

Bus Trip

Bus Trip

There are \textbf{N} towns, and \textbf{M} one-way direct bus routes (no intermediate stops) between them. The towns are numbered from \textbf{1} to \textbf{N}. A traveler who is located in the town \textbf{1} at time \textbf{0} needs to arrive in the town \textbf{P}. He will be picked from the bus station at town \textbf{P} exactly at time \textbf{T}. If he arrives earlier he will have to wait. For each bus route \textbf{i}, we know the source and destination towns \textbf{s_i} and \textbf{t_i}, of course. We also know the departure and arrival times, but only approximately: we know that the bus departs from \textbf{s_i} within the range \[\textbf{a_i}, \textbf{b_i}\] and arrives at \textbf{t_i}within the range \[\textbf{c_i}, \textbf{d_i}\] (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. \InputFile The first line contains the integer numbers \textbf{N} (\textbf{1} ≤ \textbf{N} ≤ \textbf{50000}), \textbf{M} (\textbf{1} ≤ \textbf{M} ≤ \textbf{100000}), \textbf{P} (\textbf{1} ≤ \textbf{P} ≤ \textbf{N}), and \textbf{T} (\textbf{0} ≤ \textbf{T} ≤ \textbf{1000000000}). The following \textbf{M} lines describe the bus routes. Each line contains the integer numbers \textbf{s_i}, \textbf{t_i}, \textbf{a_i}, \textbf{b_i}, \textbf{c_i}, \textbf{d_i}, where \textbf{s_i} and \textbf{t_i} are the source and destination towns of the bus route \textbf{i}, and \textbf{a_i}, \textbf{b_i}, \textbf{c_i}, \textbf{d_i} describe the departure and arrival times as explained above (\textbf{1} ≤ \textbf{s_i} ≤ \textbf{N}, \textbf{1} ≤ \textbf{t_i} ≤ \textbf{N}, \textbf{0} ≤ \textbf{a_i} ≤ \textbf{b_i} < \textbf{c_i} ≤ \textbf{d_i} ≤ \textbf{1000000000}). \OutputFile The only line of the output file 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 \textbf{P} by time \textbf{T}, the line should contain \textbf{--1}.
Time limit 1 second
Memory limit 16 MiB
Input example #1
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 example #1
32