{"raw_statement":[{"iden":"problem statement","content":"We have $N$ boxes numbered $1$ to $N$, and $M$ balls numbered $1$ to $M$. Currently, Ball $i$ is in Box $A_i$.\nYou can do the following operation:\n\n*   Choose a box containing two or more balls, pick up one of the balls from that box, and put it into another box.\n\nSince the balls are very easy to break, you cannot move Ball $i$ more than $C_i$ times in total. Within this limit, you can do the operation any number of times.\nYour objective is to have Ball $i$ in Box $B_i$ for every $i$ ($1 \\leq i \\leq M$). Determine whether this objective is achievable. If it is, also find the minimum number of operations required to achieve it."},{"iden":"constraints","content":"*   $1 \\leq N \\leq 10^5$\n*   $1 \\leq M \\leq 10^5$\n*   $1 \\leq A_i,B_i \\leq N$\n*   $1 \\leq C_i \\leq 10^5$\n*   In the situation where the objective is achieved, every box contains one or more balls. That is, for every $i$ ($1 \\leq i \\leq N$), there exists $j$ such that $B_j=i$."},{"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$\\vdots$\n$A_M$ $B_M$ $C_M$"},{"iden":"sample input 1","content":"3 3\n1 2 1\n2 1 1\n1 3 2"},{"iden":"sample output 1","content":"3\n\nWe can achieve the objective in three operations, as follows:\n\n*   Pick up Ball $1$ from Box $1$ and put it into Box $2$.\n*   Pick up Ball $2$ from Box $2$ and put it into Box $1$.\n*   Pick up Ball $3$ from Box $1$ and put it into Box $3$."},{"iden":"sample input 2","content":"2 2\n1 2 1\n2 1 1"},{"iden":"sample output 2","content":"\\-1"},{"iden":"sample input 3","content":"5 5\n1 2 1\n2 1 1\n1 3 2\n4 5 1\n5 4 1"},{"iden":"sample output 3","content":"6"},{"iden":"sample input 4","content":"1 1\n1 1 1"},{"iden":"sample output 4","content":"0"}],"translated_statement":null,"sample_group":[],"show_order":["default"],"formal_statement":null,"simple_statement":null,"has_page_source":true}