#include<iostream>
#include<algorithm>
using namespace std;
int n,w,a,b,c;
int f[100001];
struct node{
int u,v,bz;
}oh[100001];
int find(int x)
{
if(f[x]==x) return f[x];
return f[x]=find(f[x]);
}
bool cmp(node a,node b)
{
return a.bz<b.bz;
}
int main()
{
cin>>n>>w;
for(int i=1;i<=w;i++)
{
cin>>a>>b>>c;
oh[i].u=a;
oh[i].v=b;
oh[i].bz=c;
if(i<=n-1)
{
cout<<-1<<endl;
continue;
}
for(int j=1;j<=n;j++) f[j]=j;
sort(oh+1,oh+i+1,cmp);
int cnt=0,ans=0,fl=0;
for(int j=1;j<=w;j++)
{
int fx=find(oh[j].u),fy=find(oh[j].v);
if(fx!=fy)
{
f[fx]=fy;
cnt++;
ans+=oh[j].bz;
}
if(cnt==n-1)
{
fl=1;
break;
}
}
if(fl==1) cout<<ans<<endl;
else cout<<-1<<endl;
}
}