#include<stdio.h>
#include<stdlib.h>
#define LARGE 10000
#define INF 100000000
#define MIN(a,b) ((a) < (b) ? (a):(b))

int **a;
int V,E;

typedef struct node{
	int id;
	int cost;
}node;

void swap(node h[],int x,int y){
	node temp=h[x];
	h[x]=h[y];
	h[y]=temp;
	return;
}


void heapsort(node heap[],int num){

	for(int i=num-1;i>0;i--){
		if(i%2==0){
			if(heap[i].cost < heap[i/2-1].cost)swap(heap,i,i/2-1);
		}else {
			if(heap[i].cost < heap[i/2].cost)swap(heap,i,i/2);
		}
	}
	
	return;

}

int update_heap(node heap[],int num){
	int u=heap[0].id;
	swap(heap,0,num-1);
	num--;
	
	for(int i=0;i<num;i++){
		heap[i].cost=MIN(heap[i].cost,a[u][heap[i].id]);
	}
	
	return num;
}


int main(){
	int i,j;
	int s,t,d;
	int num=0;
	int ans=0;
	node heap[LARGE];
	
	scanf("%d %d",&V,&E);
	
	a=malloc(sizeof(int *)*V);
	for(i=0;i<V;i++){
		a[i]=malloc(sizeof(int)*V);
		for(j=0;j<V;j++){
			a[i][j]=INF;
		}
	}
	
	for(i=0;i<E;i++){
		scanf("%d %d %d",&s,&t,&d);
		a[s][t]=d;
		a[t][s]=d;
	}
	
	for(i=1;i<V;i++){
		heap[num].cost=a[0][i];
		heap[num++].id=i;
	}
	
	heapsort(heap,num);
	
	while(num!=0){
		ans+=heap[0].cost;
		num=update_heap(heap,num);
		heapsort(heap,num);
	}
	
	printf("%d\n",ans);
	
	return 0;
	
}
