#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct node{
int u, v, w;
}a[1000005];
int f[1000005], n, m, ans, c, num;
void add(int x, int y, int z)
{
c ++;
a[c].u = x, a[c].v = y, a[c].w = z;
}
bool cmp(node x, node y)
{
return x.w < y.w;
}
int find(int x)
{
return f[x] == x ? f[x] : f[x] = find(f[x]);
}
void kruskal()
{
for (int i = 1; i <= c; i ++)
{
int x = find(a[i].u), y = find(a[i].v);
if (x == y)
{
continue;
}
f[x] = y;
ans += a[i].w;
if (++ num == n - 1)
{
break;
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie (0);
cout.tie (0);
cin >> n >> m;
for (int i = 1; i <= n; i ++)
{
f[i] = i;
}
while(m --)
{
int x, y, z;
cin >> x >> y >> z;
add(x, y, z);
}
sort(a + 1, a + c + 1, cmp);
kruskal();
cout << ans << endl;
return 0;
}