Under construction

Still being built. Things may change

Bus Trip

Time limit1sMemory limit128 MB

Summary
Find a bus route plan from town 1 to town P arriving by time T that guarantees valid connections while minimizing worst-case total waiting time.
Level

Medium7 of 10

Topics
Shortest path, Graph, Dynamic programming
Solved
No attempts yet

Problem

There are NN towns and MM one-way direct bus routes (with no intermediate stops) connecting them. The towns are numbered from 11 to NN. A traveler is in town 11 at time 00 and must reach town PP. Someone will meet him at the bus station in town PP at exactly time TT; if he arrives earlier, he has to wait.

For each bus route ii we know its source town sis_i and destination town tit_i. We also know its departure and arrival times, but only approximately: the bus departs from sis_i at some moment in the range [ai,bi][a_i, b_i] and arrives at tit_i at some moment in the range [ci,di][c_i, d_i] (both endpoints included).

The traveler dislikes waiting, so he wants a travel plan that minimizes the maximum possible total waiting time while still guaranteeing that he never misses a connection. A connection is guaranteed only if, whenever he changes buses, the latest possible arrival time of the incoming bus is no later than the earliest possible departure time of the outgoing bus.

When computing waiting time, always assume the earliest possible arrival time and the latest possible departure time.

Write a program that finds a suitable plan for the traveler.

Input

The first line contains four integers NN (1≤N≤500001 \le N \le 50000), MM (1≤M≤1000001 \le M \le 100000), PP (1≤P≤N1 \le P \le N), and TT (0≤T≤1090 \le T \le 10^9).

Each of the next MM lines describes one bus route with six integers sis_i, tit_i, aia_i, bib_i, cic_i, did_i, where sis_i and tit_i are the source and destination towns and ai,bi,ci,dia_i, b_i, c_i, d_i describe the departure and arrival ranges as explained above (1≤si≤N1 \le s_i \le N, 1≤ti≤N1 \le t_i \le N, 0≤ai≤bi<ci≤di≤1090 \le a_i \le b_i < c_i \le d_i \le 10^9).

Output

Print a single line with the maximum possible total waiting time of the best travel plan. If it is impossible to guarantee arrival in town PP by time TT, print −1-1 instead.

Examples3

  1. Example 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
    
    Expected output
    32
    
  2. Example 2

    Input
    3 2 2 100
    1 3 0 0 49 51
    3 2 50 51 100 100
    
    Expected output
    -1
    
  3. Example 3

    Input
    3 2 3 100
    1 2 0 0 10 10
    2 3 10 10 20 20
    
    Expected output
    80