eolymp
bolt
Try our new interface for solving problems
Problems

Traffic Lights

Traffic Lights

Time limit 1 second
Memory limit 128 MiB

Kenosha, the city nearest Farmer John, has m roads conveniently numbered 1..m that connect n junctions which are conveniently numbered 1..n. No two roads connect the same pair of junctions. No road connects a junction to itself. The integer travel time T[ij] (1T[ij]100) between junctions i and j is the same for both directions (i.e., T[ij] = T[ji]).

Each junction has a single traffic light with two colors: blue or purple. The color of each light alternates periodically: blue for certain duration and then purple for another duration. Traffic is permitted to commence travel down the road between any two junctions, if and only if the lights at both junctions are the same color at the moment of departing from one junction for the other. The lights do not necessarily have to be the same on the whole trip down the road.

If a vehicle arrives at a junction just at the moment the lights switch it must consider the new colors of lights. Vehicles are allowed to wait at the junctions. You are given the city map which shows:

  • The travel times T[ij] for all roads

  • The durations of the two colors at junction i: DB[i] (1DB[i]100) for the blue light and DP[i] (1DP[i]100) for the purple light

  • The initial color C[i] of the light at junction i (a letter 'B' or 'P' with the obvious meaning) and the remaining time R[i] (1R[i]100) for this color to change

Find the minimum time one needs to get from a given source s (1sn) to a given destination d (1dn, ds).

Consider the map below with four junctions and five roads. FJ wants to travel from junction 1 to junction 4. The first light is blue; the rest are purple.

prb2587-1

The minimum time is 127 utilizing the path 1-2-4.

Initially, the light at junction 1 is blue. Since the light at junction 2 is purple, vehicle waits at junction 1 for 2 seconds then travels 4 seconds to junction 2.

At time 6, the light at junction 2 switches to blue whereas the light at junction 4 has 32 more seconds to switch to blue. However, after 32 seconds, the light at junction 2 switches to purple and the light at junction 4 switches to blue at the same time. So the vehicle needs to wait 13 seconds more for junction 2 to switch to blue then the lights have the same color and vehicle travels 76 seconds to the destination junction 4.

The total time is 2 + 4 + 32 + 13 + 76 = 127 seconds.

Below is a more graphical presentation of this travel plan:

prb2587-2

Input data

First line contains two integers s and d.

Second line contains two integers n (2n300) and m (1m14000).

The i-th of the next n lines describes junction i with a character and three integers: C[i], R[i], DB[i] and DP[i].

The k-th of the next m lines describes road k with three integers i, j and T[ij].

Output data

Print the time taken by a minimum-time path from the source junction to the destination junction. If there is no path, output 0.

Examples

Input example #1
1 4
4 5
B 2 16 99
P 6 32 13
P 2 87 4
P 38 96 49
1 2 4
1 3 40
2 3 75
2 4 76
3 4 77
Output example #1
127