#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct node
{
int s, e;
int w;
}s[100005];
int c[105];
bool cmp(struct node a, struct node b)
{
return a.w < b.w;
}
int fin(int x)
{
int r = x;
while(r != c[r]) r = c[r];
int i = x, j;
while(c[i] != r)
{
j = c[i];
c[i] =r;
i = j;
}
return r;
}
int merge(int x, int y)
{
if(fin(x) != fin(y))
{
c[fin(x)] = fin(y);
return 1;
}
return 0;
}
int main()
{
int n,m,a,b,w;
while(scanf("%d%d",&n,&m) != EOF)
{
for(int i = 0; i < m; i ++)
{
scanf("%d%d%d",&a,&b,&w);
s[i].s = a;
s[i].e = b;
s[i].w = w;
}
sort(s,s+m,cmp);
int ans = 0, cnt = 0;
for(int i = 0; i <= n; i ++)c[i] = i;
for(int i = 0; i < m; i ++)
{
if(merge(s[i].s,s[i].e)== 1)
{
ans += s[i].w;
cnt ++;
}
if(cnt == n - 1) break;
}
printf("%d
",ans);
}
return 0;
}