#include <iostream>
#include <vector>
#include <algorithm>
typedef std::pair<int, int> pair_int;
std::vector<pair_int> g[10001];
bool visit[10001];
int main()
{
int n, m, res = 0;
std::cin >> n >> m;
for (int i = 0; i < m; i++) {
int a, b, c;
std::cin >> a >> b >> c;
g[a].push_back(pair_int(b, c));
g[b].push_back(pair_int(a, c));
}
visit[1] = true;
for (int i = 1; i < n; i++) {
int x, y, min = 217483647;
for (int j = 1; j <= n; j++) {
if (visit[j]) {
for (int k = 0; k < g[j].size(); k++) {
if (!visit[g[j][k].first] && g[j][k].second < min)
min = g[j][k].second, x = j, y = g[j][k].first;
}
}
}
visit[y] = true;
res += min;
}
std::cout << res;
return 0;
}