#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;

typedef pair<int,int> pii;
const int INF =  1000000000;

int par[10000],rnk[10000];

void init(int n){
  for(int i=0;i<n;i++){
    par[i] = i;
    rnk[i] = 1;
  }
}
int find(int x){
  if(par[x]==x) return x;
  else return par[x] = find(par[x]);
}
bool same(int x,int y){
  return find(x)==find(y);
}
void unite(int x,int y){
  x = find(x);
  y = find(y);
  if(rnk[x]>rnk[y]) par[y] = x;
  else{
    par[x] = y;
    if(rnk[x]==rnk[y]) rnk[y]++;
  }
  return;
}

int main(){
  int V,E;
  vector<pair<int,pii> > es;
  scanf("%d %d",&V,&E);
  for(int i=0;i<E;i++){
    int s,t,w;
    scanf("%d %d %d",&s,&t,&w);
    es.push_back(make_pair(w,pii(s,t)));
  }
  int ans = 0;
  int cost[10000];
  fill(cost,cost+V,INF);
  sort(es.begin(),es.end());
  cost[0] = 0;
  init(V);
  for(int i=0;i<es.size();i++){
    if(!same(es[i].second.first,es[i].second.second)){
      ans += es[i].first;
      unite(es[i].second.first,es[i].second.second);
    }
  }
  printf("%d\n",ans);
  return 0;
}
#include<bits/stdc++.h>
using namespace std;

struct Edge{
  int to,cost;
  bool operator<(const Edge&a)const{
    return cost>a.cost;
  }
};
struct Graph{
  Graph(int n):E(n),T(n),N(n){}
  vector<vector<Edge>>E,T;
  int N;
  void add_edge(int a,int b,int c){
    E[a].push_back({b,c});
    E[b].push_back({a,c});
  }
  void prim(int s){
    bool visited[N];
    priority_queue<pair<Edge,int>>Q;
    memset(visited,0,N);
    Q.push({{s,0},-1});
    while(Q.size()){
      Edge e=Q.top().first;
      int from=Q.top().second;Q.pop();
      if(visited[e.to])continue;
      visited[e.to]=1;
      if(from+1)T[from].push_back(e),T[e.to].push_back({from,e.cost});
      for(auto&a:E[e.to])
      if(!visited[a.to])Q.push({a,e.to});
    }
  }
  int get_cost(){
    int ret=0;
    for(auto&a:T)for(auto&b:a)ret+=b.cost;
    return ret/2;
  }
  vector<Edge>* get_MST(){
    return T.data();
  }
};

main(){
  int V,E,a,b,c;
  cin>>V>>E;
  Graph G(V);
  for(int i=0;i++<E;G.add_edge(a,b,c))cin>>a>>b>>c;
  G.prim(0);
  cout<<G.get_cost()<<endl;
}