{"raw_statement":[{"iden":"problem statement","content":"In the Republic of AtCoder, there are $N$ towns numbered $1$ through $N$ and $M$ roads numbered $1$ through $M$.  \nRoad $i$ is a one-way road from Town $A_i$ to Town $B_i$, and it takes $C_i$ minutes to go through. It is possible that $A_i = B_i$, and there may be multiple roads connecting the same pair of towns.  \nTakahashi 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.  \nFor 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."},{"iden":"constraints","content":"*   $1 \\le N \\le 2000$\n*   $1 \\le M \\le 2000$\n*   $1 \\le A_i \\le N$\n*   $1 \\le B_i \\le N$\n*   $1 \\le C_i \\le 10^5$\n*   All values in input are integers."},{"iden":"input","content":"Input is given from Standard Input in the following format:\n\n$N$ $M$\n$A_1$ $B_1$ $C_1$\n$A_2$ $B_2$ $C_2$\n$A_3$ $B_3$ $C_3$\n$\\hspace{25pt} \\vdots$\n$A_M$ $B_M$ $C_M$"},{"iden":"sample input 1","content":"4 4\n1 2 5\n2 3 10\n3 1 15\n4 3 20"},{"iden":"sample output 1","content":"30\n30\n30\n-1\n\nBy Roads $1, 2, 3$, Towns $1, 2, 3$ forms a ring that takes $30$ minutes to go around.  \nFrom Town $4$, we can go to Towns $1, 2, 3$, but then we cannot return to Town $4$."},{"iden":"sample input 2","content":"4 6\n1 2 5\n1 3 10\n2 4 5\n3 4 10\n4 1 10\n1 1 10"},{"iden":"sample output 2","content":"10\n20\n30\n20\n\nThere may be a road such that $A_i = B_i$.  \nHere, we can use just Road $6$ to depart from Town $1$ and return to that town."},{"iden":"sample input 3","content":"4 7\n1 2 10\n2 3 30\n1 4 15\n3 4 25\n3 4 20\n4 3 20\n4 3 30"},{"iden":"sample output 3","content":"\\-1\n-1\n40\n40\n\nNote that there may be multiple roads connecting the same pair of towns."}],"translated_statement":null,"sample_group":[],"show_order":["default"],"formal_statement":null,"simple_statement":null,"has_page_source":true}