#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 100005
int p[N];
struct Edge
{
int s, e;
int cost;
}edge[N];
bool cmp(struct Edge a, struct Edge b)
{
return a.cost < b.cost;
}
int getPar(int k)
{
if (p[k] == k)
return k;
return p[k] = getPar(p[k]);
}
int main()
{
int n, m, ans, i, k;
scanf("%d%d", &n, &m);
for (i = 0; i < m; i++)
scanf("%d%d%d", &edge[i].s, &edge[i].e, &edge[i].cost);
sort(edge, edge + m, cmp);
for (i = 1; i <= n; i++)
p[i] = i;
for (ans = 0, k = 0, i = 0; i < m && k < n-1; i++)
{
int ps = getPar(edge[i].s), pe = getPar(edge[i].e);
if (ps == pe)
continue;
p[ps] = pe;
ans += edge[i].cost;
k++;
}
printf("%d\n", ans);
return 0;
}