#include<stdio.h>
#include<string.h>
#include<math.h>
int main(void)
{
	int a,s,d[101],f[101],g[101],h,i,j,size[101],ban[101];
	int z,x,q;
	scanf("%d %d",&a,&s);
	for(i=0;i<a;i++){
		ban[i]=i;
		size[i]=1;
	}
	for(i=0;i<s;i++) scanf("%d %d %d",&d[i],&f[i],&g[i]);
	for(i=0;i<s;i++){
		if(d[i]>f[i]) h=d[i]; d[i]=f[i]; f[i]=h;
		for(j=i+1;j<s;j++){
			if(g[i]>g[j]){
				h=g[i]; g[i]=g[j]; g[j]=h;
				h=d[i]; d[i]=d[j]; d[j]=h;
				h=f[i]; f[i]=f[j]; f[j]=h;
			}
		}
	}
	for(i=0;i<s;i++){
		z=d[i]; x=f[i];
		while(ban[z]!=z) z=ban[z];
		while(ban[x]!=x) x=ban[x];
		if(z!=x){
			q+=g[i];
			if(size[z]>size[x]) ban[z]=x;
			else if(size[z]<size[x]) ban[x]=z;
			else{
				ban[x]=z;
				size[x]++;
			}
		}
	}
	printf("%d\n",q);
	return 0;
}