#Q92. 「一本通 3.4 练习 2」布局 Layout

    ID: 2175 Type: Default 1000ms 512MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>早于 2010USACO Contest差分约束系统一本通提高

「一本通 3.4 练习 2」布局 Layout

Description

Original source: USACO 2005 Dec. Gold

FJ has NN cows (2N1000)(2\le N\le 1000), numbered 1N1\ldots N. The cows will line up in order of their numbers (multiple cows may occupy the same position). In other words, assuming the ii-th cow is at position P ⁣  iP_{\!\;i}, then P1P2P ⁣  NP_{\,1}\le P_{\,2}\le \ldots\le P_{\!\;N}.

Some cows are good buddies and want the distance between them to be less than or equal to a certain number. Some cows are rivals and want the distance between them to be greater than or equal to a certain number.

Given MLM_L pairs of good buddies, along with the maximum distance they desire between each other; and MDM_D pairs of rivals, along with the minimum distance they desire between each other (1ML,(1\le M_L, MD104)M_D\le 10^4).

Calculate: If all the above conditions are satisfied, what is the maximum possible distance between cow 11 and cow NN (P ⁣  NP1P_{\!\;N}-P_{\,1}).

Input Format

The first line: Three integers N,ML,MDN, M_L, M_D, separated by spaces.
Lines 2ML+12\dots M_L+1: Each line contains three integers A,B,DA, B, D, separated by spaces, indicating PBPADP_B-P_A\le D.
Lines ML+2ML+MD+1M_L+2\dots M_L+M_D+1: Each line contains three integers A,B,DA, B, D, separated by spaces, indicating PBPADP_B-P_A\ge D.
It is guaranteed that 1A<BN,1\le A<B\le N, 1D1061\le D\le 10^6.

Output Format

One line with an integer. If there is no valid arrangement, output -1\texttt{-1}. If there is a valid arrangement but the distance between cow 11 and cow NN can be infinitely large, output -2\texttt{-2}. Otherwise, output the maximum distance between cow 11 and cow NN.

Sample 1

The four cows are located at 0,7,10,270,7,10,27 respectively.

4 2 1
1 3 10
2 4 20
2 3 3

27

Data Range and Hints

For all data, $2\le N\le 1000,1\le M_L,M_D\le 10^4,1\le L,D\le 10^6$.