This page is still under construction.

Parts of this page are still being built. What you see may change.

The Hero

Time limit1sMemory limit128 MB

Summary
Find the shortest sailing time from island 1 to island n, waiting on islands to dodge trap intervals that forbid presence on active days.
Level

Medium7 of 10

Topics
Shortest path, Intervals
Solved
No attempts yet

Problem

Byteotheus, the most famous hero of Byteotia, has won another battle. While his crew loads the ship with the spoils, he sits in his cabin and plans the way back to his home island, The Bitaca. That is not an easy task. Many gods envy his popularity and would gladly take him down a peg. A few of them favour him, the goddess Bythena above all. It was Bythena who sent Byteotheus a dream last night to warn him about the dangers ahead.

The Byteonian Sea has nn islands, numbered from 1 to nn. The ship is now at island 1 and its destination is The Bitaca, island nn. Some pairs of islands are joined by one-way sea routes, numbered from 1 to mm. Route ii leads from island aia_i to island bib_i and takes exactly did_i days. At most 10 routes start at any single island.

If the ship leaves island aia_i along route ii at dawn on day jj, it reaches island bib_i at dawn on day j+dij + d_i. The ship may stay at any island as long as it likes. Before reaching the next island it cannot leave the chosen route, and it cannot stay at sea longer than the route takes. The earliest moment Byteotheus can leave island 1 is dawn on day 1.

Bythena's warning was precise. She gave Byteotheus the exact list of the pp traps the gods have prepared. Each trap sits on one island and is active during one period. Trap ii is on island wiw_i and is active from day sis_i through day kik_i, day kik_i included. If the ship is on an island while a trap there is active, nobody survives. The Bitaca has no traps, and no trap on island 1 is active on day 1.

If the ship reaches an island at dawn on day xx and leaves it at dawn on day yy, it counts as being on that island on every day from xx to yy, both included. A trap on that island active on any of those days kills the crew.

Byteotheus wants a way home that avoids every trap, and he also wants to know how much longer the traps make his voyage. Find the smallest number of days he needs to reach The Bitaca safely.

Input

The first line contains two integers nn and mm (2≤n≤100 0002 \le n \le 100\,000, 1≤m≤1 000 0001 \le m \le 1\,000\,000), the number of islands and the number of sea routes. Each of the next mm lines describes one route with three integers aia_i, bib_i, did_i (1≤ai,bi≤n1 \le a_i, b_i \le n, ai≠bia_i \ne b_i, 1≤di≤1091 \le d_i \le 10^9), meaning that route ii leads from island aia_i to island bib_i and takes did_i days. All routes are one way. At most 10 routes start at any single island.

The next line contains one integer pp (0≤p≤100 0000 \le p \le 100\,000), the number of traps. Each of the next pp lines describes one trap with three integers wiw_i, sis_i, kik_i (1≤wi<n1 \le w_i < n, 1≤si≤ki≤1091 \le s_i \le k_i \le 10^9), meaning that trap ii is on island wiw_i and is active from day sis_i through day kik_i. If wi=1w_i = 1, then si>1s_i > 1.

Output

If no route avoids every trap, print NIE on a single line. That is Polish for no. Otherwise print one integer dd, the smallest number of days the voyage takes. The ship then reaches The Bitaca at dawn on day d+1d + 1.

Hint

In the first example Byteotheus leaves island 1 at dawn on day 1 and reaches island 2 on day 4. He waits one day, sails for island 3 and arrives on day 6, then turns back to island 2 at once. On day 8 he leaves island 2 for island 4, arrives on day 10, and reaches The Bitaca on day 11.

Examples1

  1. Example 1

    Input
    5 6
    1 2 3
    1 4 13
    2 3 1
    2 4 2
    3 2 2
    4 5 1
    5
    1 2 4
    1 8 8
    2 6 7
    2 10 11
    4 6 7
    
    Expected output
    10