#include<stdio.h>
#define INF 99999
#define MAX 10
int main(){
    int n,m;
    scanf("%d %d",&n,&m);
    if(n<=0){
        printf("Invalid input\n");
        return 0;
    }
    int graph[MAX][MAX]={0}
    int u,v,w;
    for(int i=0;i<m;i++){
        scanf("%d %d %d",&u,&v,&w);
        graph[u][v]=w;
        graph[v][u]=w;
    }
    int selected[max]={0};
    int edgecount=0;
    int totalweight=0;
    selected[0]=1;
    while(edgecount<n-1){
        int min=INF;
        int x=-1,y=-1;
        for(int i=0;i<n;i++){
            if(selected[i]){
                for(intj=0;j<n;j++){
                    if(!selected[j]&&graph[i][j]){
                        if(graph[i][j]<min){
                            min=graph[i][j];
                            x=i;
                            y=j;
                        }
                    }
                }
            }
        }
        if(x!=-1&&y!=-1){
            totalweight+=graph[x][y];
            selected[y]=1;
            edgecount++;
        }else{
            break;
        }
    }
    printf("%d\n",totalweight);
    return0;
}