#include<bits/stdc++.h>
using namespace std;
const int N=6010;
int fa[N];
struct node {
int u,v,w;
} E[N*2];
int n,m;
inline int read() {
int x=0;
char c=getchar();
while(c<'0'||c>'9')c=getchar();
while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();
return x;
}
int find(int& x) {
return fa[x]==x?x:fa[x]=find(fa[x]);
}
bool cmp(node a,node b) {
return a.w<b.w;
}
int main() {
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
n=read();
m=read();
for(register int i=1; i<=m; i++) {
E[i].u=read();
E[i].v=read();
E[i].w=read();
if(i<n-1) {
cout<<-1<<"\n";
continue;
}
else {
int tot=0;
int answer=0;
int flag=1;
sort(E+1,E+i+1,cmp);
for(register int j=1; j<=n; j++)
fa[j]=j;
for(register int j=1; j<=i&&flag; j++) {
int fu=find(E[j].u);
int fv=find(E[j].v);
if(fu!=fv) {
fa[fu]=fv;
tot++,answer+=E[j].w;
}
// if(tot==n-1)
// flag=0;
(tot==n-1)&&(flag=0);
}
// if(tot!=n-1)
// cout<<-1<<"\n";
// else
// cout<<answer<<"\n";
(tot!=n-1)?(cout<<-1<<"\n"):(cout<<answer<<"\n");
}
}
return 0;
}
/*
|| |||
||| ||| || || || ||| ||| |||
|| || || || ||| ||| ||| || || || |||| |||
|| |||| || |||| ||| ||| || || || |||| ||| |||||| |||||||||||||| ||||||| |||||||||||||||||||||| |||||||
|| ||| || ||| ||| ||| ||||||||| || ||||||| || || |||| |||||||| ||||| ||||| |||| |||||||||||||||||||||| ||||| ||||
|||||| | |||||| | ||| || | || ||| | || |||| ||||||||||||||||||| || |||| |||| ||| ||| ||| || || |||| |||
|||||||| |||||||| ||| ||| |||||| || || || || || |||||||||||| ||||| ||| |||| ||| ||| ||| || | |||| |||
||||||||| || ||||||||| || ||| |||||||||| ||||| || || || |||||| || |||||||||| ||| |||| ||| ||| ||| ||| | | ||| |||
|| || ||| || || ||| |||| ||||| ||| ||||| || || |||||||||||||||||||| ||| |||| || ||| |||| ||| ||| ||||| || |||| |||
|| |||| || |||| ||||| || || || || || || |||||||| || || || ||| ||| ||| ||| ||| |||| | ||| |||
|||| |||||| |||| |||||| ||| || || ||| | ||||| || || | || ||||| || ||| ||| ||| ||| || ||| |||
||| |||| ||| |||| || || || ||||||||| || || || | | || ||| || ||| ||| ||| ||| || ||| |||
||||| ||||| || | || | ||||||||| || || ||||||| ||||||||||| || ||| ||| ||| |||| | ||| |||
||| ||||| ||| ||||| || || || ||| || ||| || || || ||||| |||||| || || |||| ||| ||| |||| || |||| |||
|||| || ||| |||| || ||| || || || |||| || ||| || || || ||| || || || || |||| || ||| || ||| | |||| ||
||||| || ||||| ||||| || ||||| ||||| || ||| || ||| || || || || || || ||| ||| ||| || ||||| ||| |||
|||| || |||||||| |||| || |||||||| |||||| || ||| ||||||| || || || || |||| || ||| ||| |||| |||| ||| |||
|| || ||||||| || || ||||||| ||||| | || || ||| |||| || || || || ||| | || |||| ||| |||| ||| |||| |||
|| || || || || || ||| |||| ||| ||| ||||| ||| | || | ||||| ||||||||| || ||| |||||||||
|||| |||| | ||| | || |||| ||| || |||| |||| || || ||||
||| ||| ||| ||| ||| || ||| ||||| |||||
|| || | | || || || ||||| |||||
| ||||||| |||||||
|||| ||||
*/
测评记录 被卡常了求助