#include<stdio.h>
#define MAXN 1005
#include<iostream>
#include<algorithm>
#define inf 10000000
using namespace std;
int _m[MAXN][MAXN];
int low_cost[MAXN];
int pre[MAXN];
unsigned prime(int n);
int DFS(int i,int sum,int p);
bool mark[MAXN];
int main()
{
//freopen("acm.acm","r",stdin);
int p;
int edge;
unsigned tem;
int i;
int j;
int u;
int v;
while(cin>>p>>edge)
{
memset(mark,false,sizeof(mark));
for(i = ; i < p; ++ i)
{
for(j = ; j < p; ++ j)
_m[i][j] = inf;
}
for(i = ; i < edge; ++ i)
{
cin>>u>>v;
-- u;
-- v;
cin>>tem;
if(_m[u][v] == inf)
{
_m[u][v] = tem;
_m[u][v] *= -;
_m[v][u] = _m[u][v];
}
else
if(tem > _m[u][v]*(-))
{
_m[u][v] = tem;
_m[u][v] *= -;
_m[v][u] = _m[u][v];
}
}
mark[] = true;
tem = -prime(p);
if(DFS(,,p) == p)
cout<<tem<<endl;
else
cout<<-<<endl;
}
}
int DFS(int i,int sum,int n)
{
int j;
for(j = ; j < n; ++ j)
{
if(i != j && _m[i][j] != inf && !mark[j])
{
mark[j] = true;
sum = DFS(j,sum+,n);
}
}
return sum;
}
unsigned prime(int n)
{
int i;
int j;
int k;
unsigned sum = ;
int min;
for(i = ; i < n; ++ i)
{
low_cost[i] = _m[][i];
pre[i] = ;
}
for(i = ; i < n; ++ i)
{
min = inf;
for(j = ; j < n; ++ j)
{
if(low_cost[j]&&low_cost[j] < min)
{
k = j;
min = low_cost[j];
}
}
sum += low_cost[k];
low_cost[k] = ;
for(j = ; j < n; ++ j)
{
if(_m[k][j] < low_cost[j] && low_cost[j])
{
low_cost[j] = _m[k][j];
pre[j] = k;
}
}
}
return sum;
}