eolymp
bolt
Try our new interface for solving problems
Problems

Journey

Journey

The army of Rzeczpospolita is moving from the city Kostroma to the village Domnino. Two hetmans, Stefan and Konstantin, lead the army.

Stefan procured the roadmap of Kostroma province, and every night he routes the army from one village to the other along some road. Konstantin bought the map of secret trails between villages in advance, and every day he leads the march along the one of such trails. Each hetman asks their guide Ivan Susanin for a route before each march.

The length of each road is indicated on Stefan’s map. So Stefan knows the minimal distance from each village to the Domnino village according to his map. Similarly Konstantin knows the minimal distance from each village to Domnino village along trails on his map.

Ivan Susanin does not want to be disclosed as a secret agent, so each time he chooses a road (for Stefan) or a trail (for Konstantin) so that the minimal distance to the Domnino village according to the map owned by the asking hetman is strictly decreasing.

prb2267

Help Ivan to find the longest possible route to the Domnino village.

Input

The first line contains three integer numbers n, s and t - number of villages in Kostroma province, and numbers of start and Domnino village (2n1000; 1s, tn). Villages are numbered from 1 to n. Start and Domnino villages are distinct.

Two blocks follow, the first one describing Stefan’s map, and the second one describing Konstantin’s map.

The first line of each block contains an integer number m - the number of roads/trails between villages (n - 1m100000). Each of the following m lines contains three integer numbers a, b, and l - describing the road/trail between villages a and b of length l (1a, bn; 1l106).

Rzeczpospolita army can move in any direction along a road or a trail. It’s guaranteed that one can travel from any village to any other using each of the maps. The army starts its movement in the evening from the village number and moves one road each night and one trail each day.

Output

Output the total length of the longest route that Ivan Susanin can arrange for Rzeczpospolita army before reaching the Domnino village (along the roads and trails). If Ivan Susanin can route the army forever without reaching the Domnino village, output the number "-1".

Time limit 1 second
Memory limit 256 MiB
Input example #1
5 1 5
5
1 2 2
1 4 2
2 3 1
3 4 1
5 3 1
4
1 2 2
2 4 2
2 3 1
2 5 2
Output example #1
-1
Input example #2
3 1 3
4
1 2 10
2 3 10
1 3 20
2 3 30
4
2 1 10
1 3 10
1 1 10
2 3 10
Output example #2
20
Author Mikhail Dvorkin, Dmitry Gozman
Source 2010 NEERC Northern Subregional St Petersburg, October 30