#include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(ll (i)=0;(i)<(n);(i)++)
using namespace std;

struct uft{
    vector<ll> par;
    uft(ll n) {
        par.resize(n);
        rep(i,n)par[i]=i;
    }

    ll find(ll a){
        if(par[a]==a){
            return a;
        }else {
            return find(par[a]);
        }
    }

    void unite(ll a,ll b){
        par[find(a)]=find(b);
    }

    bool same(ll a,ll b){
        return find(a) == find(b);
    }
};

struct edge{
    ll s,t,w;
};

bool sortw(edge a,edge b) {
    return a.w < b.w;
}

int main() {
    ll v,e;
    cin>>v>>e;
    vector<edge> edges;
    rep(i,e){
        ll s,t,w;
        cin>>s>>t>>w;
        edges.push_back(edge{s,t,w});
    }
    sort(edges.begin(),edges.end(),sortw);
    ll sum=0;
    uft uni(v);
    rep(i,e){
        if(!uni.same(edges[i].s,edges[i].t)) {
            uni.unite(edges[i].s,edges[i].t);
            sum+=edges[i].w;
        }
    }
    cout<<sum<<endl;
}

