#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>usingnamespacestd;
int n, m;
constint maxn = 1000;
int UF_set[maxn];
struct Edge{ //用数组实现邻接表int from, to;
int value;
}edges[maxn*(maxn-1)];
bool cmp(Edge a, Edge b){ //定义cmp函数为了sort函数, 从而实现对于边权的排序return a.value < b.value;
}
int find(int p){
return UF_set[p] == p ? p : UF_set[p] = find(UF_set[p]);
}
void init(){
for(int i = 0; i < maxn; ++i){ //initialization Union-Find set
UF_set[i] = i;
}
}
int Union(int a, int b, int idx, int ans){
int x = find(a);
int y = find(b);
if(x != y){
UF_set[x] = y;
ans += edges[idx].value;
}
return ans;
}
void readin(){
scanf("%d%d", &n, &m);
int a, b, c;
for(int i = 1; i <= m; ++i){
scanf("%d%d%d", &a, &b, &c);
edges[i].from = a;
edges[i].to = b;
edges[i].value = c;
}
}
int Kruskal(){
int ans = 0;
init();
sort(edges, edges+m, cmp);
for(int i = 1; i <= m; ++i){
ans = Union(edges[i].from, edges[i].to, i, ans);
}
return ans;
}
int main()
{
readin();
int ans = Kruskal();
cout << ans <<endl;
return0;
}