#include <iostream>
#include <vector>
#include <queue>
#include <utility>
using namespace std;
const int INF = (1e9);

int prim(vector< vector< pair<int,int> > > &G){
  vector<bool> connected(G.size(),false);
  priority_queue< pair<int,int> > wait;
  wait.emplace(0,0);
  int ret = 0;
  while(!wait.empty()){
    int c = -wait.top().first, v = wait.top().second;
    wait.pop();
    if(connected[v]) continue;
    connected[v] = true;
    ret += c;
    for(size_t i = 0; i < G[v].size(); ++i){
      int c_ = G[v][i].first, v_ = G[v][i].second;
      if(!connected[v_]){
        connected[v] = true;
        wait.emplace(-c_,v_);
      }
    }
  }
  return ret;
}

int main(){
  int V, E;
  cin >> V >> E;
  vector< vector< pair<int,int> > > G(V);
  for(int i = 0; i < E; ++i){
    int s, t, d;
    cin >> s >> t >> d;
    G[s].emplace_back(d,t);
    G[t].emplace_back(d,s);
  }
  cout << prim(G) << endl;
  return 0;
}

