// credit from https://github.com/Autoratch/practice#include<bits/stdc++.h>usingnamespace std;#defineendl'\n'#defineMOD1e9+7#definepiipair<int, pair<int,int>>int n, m;
vector<int> pa;
priority_queue<pii, vector<pii>, greater<pii>> q;introot(int x){if(pa[x]== x)return x;elsereturn pa[x]=root(pa[x]);}intkruskal(){int ans =0;
pa.resize(n);for(int i =0; i < n; i++)
pa[i]= i;while(!q.empty()){int w = q.top().first, x = q.top().second.first, y = q.top().second.second;
q.pop();if(root(x)==root(y))continue;
ans += w;
pa[root(x)]= pa[root(y)];}return ans;}intmain(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;for(int i =0; i < m; i++){int a, b, d;
cin >> a >> b >> d;
q.push({d,{a, b}});}
cout <<kruskal();}
// credit from https://github.com/Autoratch/practice#include<bits/stdc++.h>usingnamespace std;#defineendl'\n'#defineMOD1e9+7int n, m, ans;
vector<bool> visited;
vector<vector<pair<int,int>>> adj;
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>>
q;intmain(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
adj.resize(n);
visited.resize(n);for(int i =0; i < m; i++){int a, b, d;
cin >> a >> b >> d;
adj[a].push_back({d, b});
adj[b].push_back({d, a});}
q.push({0,0});while(!q.empty()){int w = q.top().first, p = q.top().second;
q.pop();if(visited[p])continue;
visited[p]=true;
ans += w;for(int i =0; i < adj[p].size(); i++)if(!visited[adj[p][i].second])
q.push(adj[p][i]);}
cout << ans;}