{"trustable":true,"prependHtml":"\u003cscript\u003e window.katexOptions \u003d { disable: true }; \u003c/script\u003e\n\u003cscript type\u003d\"text/x-mathjax-config\"\u003e\n MathJax.Hub.Config({\n tex2jax: {\n inlineMath: [[\u0027$$$\u0027,\u0027$$$\u0027], [\u0027$\u0027,\u0027$\u0027]],\n displayMath: [[\u0027$$$$$$\u0027,\u0027$$$$$$\u0027], [\u0027$$\u0027,\u0027$$\u0027]]\n }\n });\n\u003c/script\u003e\n\u003cscript async src\u003d\"https://mathjax.codeforces.org/MathJax.js?config\u003dTeX-AMS-MML_HTMLorMML\" type\u003d\"text/javascript\"\u003e\u003c/script\u003e","sections":[{"title":"","value":{"format":"HTML","content":"\u003cdiv class\u003d\"panel_content\"\u003eCaocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn\u0027t give up. Caocao\u0027s army still was not good at water battles, so he came up with another idea. He built many islands in the Changjiang river, and based on those islands, Caocao\u0027s army could easily attack Zhou Yu\u0027s troop. Caocao also built bridges connecting islands. If all islands were connected by bridges, Caocao\u0027s army could be deployed very conveniently among those islands. Zhou Yu couldn\u0027t stand with that, so he wanted to destroy some Caocao\u0027s bridges so one or more islands would be seperated from other islands. But Zhou Yu had only one bomb which was left by Zhuge Liang, so he could only destroy one bridge. Zhou Yu must send someone carrying the bomb to destroy the bridge. There might be guards on bridges. The soldier number of the bombing team couldn\u0027t be less than the guard number of a bridge, or the mission would fail. Please figure out as least how many soldiers Zhou Yu have to sent to complete the island seperating mission.\u003c/div\u003e"}},{"title":"Input","value":{"format":"HTML","content":"There are no more than 12 test cases.\u003cbr\u003e\u003cbr\u003eIn each test case:\u003cbr\u003e\u003cbr\u003eThe first line contains two integers, N and M, meaning that there are N islands and M bridges. All the islands are numbered from 1 to N. ( 2 \u0026lt;\u003d N \u0026lt;\u003d 1000, 0 \u0026lt; M \u0026lt;\u003d N\u003csup\u003e2\u003c/sup\u003e )\u003cbr\u003e\u003cbr\u003eNext M lines describes M bridges. Each line contains three integers U,V and W, meaning that there is a bridge connecting island U and island V, and there are W guards on that bridge. ( U ≠ V and 0 \u0026lt;\u003d W \u0026lt;\u003d 10,000 )\u003cbr\u003e\u003cbr\u003eThe input ends with N \u003d 0 and M \u003d 0."}},{"title":"Output","value":{"format":"HTML","content":"For each test case, print the minimum soldier number Zhou Yu had to send to complete the mission. If Zhou Yu couldn\u0027t succeed any way, print -1 instead."}},{"title":"Sample","value":{"format":"HTML","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e3 3\r\n1 2 7\r\n2 3 4\r\n3 1 4\r\n3 2\r\n1 2 7\r\n2 3 4\r\n0 0\r\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e-1\r\n4\r\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}}]}