#abc191e. [abc191_e]Come Back Quickly

[abc191_e]Come Back Quickly

Problem Statement

In the Republic of AtCoder, there are NN towns numbered 11 through NN and MM roads numbered 11 through MM.
Road ii is a one-way road from Town AiA_i to Town BiB_i, and it takes CiC_i minutes to go through. It is possible that Ai=BiA_i = B_i, and there may be multiple roads connecting the same pair of towns.
Takahashi is thinking about taking a walk in the country. We will call a walk valid when it goes through one or more roads and returns to the town it starts at.
For each town, determine whether there is a valid walk that starts at that town. Additionally, if the answer is yes, find the minimum time such a walk requires.

Constraints

  • 1leNle20001 \\le N \\le 2000
  • 1leMle20001 \\le M \\le 2000
  • 1leAileN1 \\le A_i \\le N
  • 1leBileN1 \\le B_i \\le N
  • 1leCile1051 \\le C_i \\le 10^5
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN MM A1A_1 B1B_1 C1C_1 A2A_2 B2B_2 C2C_2 A3A_3 B3B_3 C3C_3 hspace25ptvdots\\hspace{25pt} \\vdots AMA_M BMB_M CMC_M

Output

Print NN lines. The ii-th line (1leileN)(1 \\le i \\le N) should contain the following:

  • if there is a valid walk that starts at Town ii, the minimum time required by such a walk;
  • otherwise, -1.

Sample Input 1

4 4
1 2 5
2 3 10
3 1 15
4 3 20

Sample Output 1

30
30
30
-1

By Roads 1,2,31, 2, 3, Towns 1,2,31, 2, 3 forms a ring that takes 3030 minutes to go around.
From Town 44, we can go to Towns 1,2,31, 2, 3, but then we cannot return to Town 44.


Sample Input 2

4 6
1 2 5
1 3 10
2 4 5
3 4 10
4 1 10
1 1 10

Sample Output 2

10
20
30
20

There may be a road such that Ai=BiA_i = B_i.
Here, we can use just Road 66 to depart from Town 11 and return to that town.


Sample Input 3

4 7
1 2 10
2 3 30
1 4 15
3 4 25
3 4 20
4 3 20
4 3 30

Sample Output 3

-1
-1
40
40

Note that there may be multiple roads connecting the same pair of towns.