In ICPCCamp, there arencities
andmdirected
roads between cities. Thei-th
road going from theai-th
city to thebi-th
city iscikilometers
long. For each pair of cities(u,v),
there can be more than one roads fromutov.
Bobo wants to make big news by solving the famous Hamiltonian Path problem. That is, he would like to visit all thencities
one by one so that the total distance travelled is minimized.
Formally, Bobo likes to findndistinct integersp1,p2,…,pnto
minimizew(p1,p2)+w(p2,p3)+⋯+w(pn−1,pn)wherew(x,y)is
the length of road from thex-th
city to they-th
city.
Input
The input contains at most30sets.
For each set:
The first line contains2integersn,m(2≤n≤105,0≤m≤105).
Thei-th
of the followingmlines
contains3integersai,bi,c
i( 1≤ai<bi≤n,1≤ci≤104).
Output
For each set, an integer denotes the minimum total distance. If there exists no plan, output -1 instead.