#include <bits/stdc++.h>
using namespace std;

#define debug(x) cout <<"DEBUG"<<" "<< #x << ":" << x << '\n'

// n回繰り返し
#define rep(i,n) for(int i = 0; i < ((int)(n)); i++)   // 0-indexed昇順
#define rep1(i,n) for(int i = 1; i <= ((int)(n)); i++) // 1-indexed昇順
#define rrep(i,n) for(int i = ((int)(n)-1); i >= 0; i--)  // 0-indexed降順
#define rrep1(i,n) for(int i = ((int)(n)); i >= 1; i--)   // 1-indexed降順
#define all(x) (x).begin(),(x).end()

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vii;  // 2次元配列
typedef vector<ll> vll;
typedef vector<double> vd;
typedef pair<int, int> PII;

template<typename T>
using vec = vector<T>;
template<class T>
using maxheap = std::priority_queue<T>;
template<class T>
using minheap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template<class T,class U> inline bool chmax(T &a, const U &b){if(a<b){a=b;return 1;}return 0;}
template<class T,class U> inline bool chmin(T &a, const U &b){if(a>b){a=b;return 1;}return 0;}

const ld Pi = std::acos(-1.0L);
const ll infll = (1LL<<62)-1;
const int inf = (1<<30)-1;
const int mod = 1000000007;
// const int mod = 998244353;

/*GRAPH_TEMPLATE=============================================*/
template<typename T>
class Edge {
public:
    int src;  // source 状態記録「null = -1」
    int to;  // 辺の行き先
    T cost;  // 辺の重み
    Edge(int t, T w) : src(-1), to(t), cost(w) {}
    Edge(int t, T w, int src) : src(src), to(t), cost(w) {}
};
template<typename T>
using W_Graph = std::vector<std::vector<Edge<T>>>;   //重み付きグラフ
using Graph = std::vector<std::vector<int>>;         //通常グラフ
template<typename T>
using Matrix = std::vector<std::vector<T>>;          //隣接行列(使わなさそう...)


/*FUNCs=================================================*/
/*Minimum Spanning Tree======================*/
/*Prim's Algorithm===========================*/
// 最小全域木を構築する 返り値はpair<総コスト, MST>
template<class T>
std::pair<T, W_Graph<T>> Prim(W_Graph<T> &G) {
    T RetCost = 0;
    W_Graph<T> MST(G.size());
    std::vector<bool> used(G.size(), false);

    using P = std::tuple<T, int, int>;
    std::priority_queue<P, std::vector<P>, std::greater<P>> que;
    que.emplace(0, 0, 0);

    while(!que.empty()) {
        T cost;
        int v;  // parent
        int u;  // vertex focused on
        tie(cost, u, v) = que.top();
        que.pop();
        if(used[u]) continue;
        used[u] = true;
        RetCost += cost;
        if(u!=v) {
            MST[v].push_back(Edge<T>(u, cost));
            MST[u].push_back(Edge<T>(v, cost));
        }
        for(auto &edge : G[u]) {
            que.emplace(edge.cost, edge.to, u);
        }
    }
    return(make_pair(RetCost, MST));
}


/*MAIN==================================================*/
signed main(){
    int _StartTime_ = clock();
    cin.tie(nullptr);
    ios::sync_with_stdio(false);   //cin cout 高速化
    // cout << fixed << setprecision(15);

    int v, e; cin >> v >> e;
    W_Graph<int> g(v);
    rep(_, e) {
        int s,t,d; cin >> s >> t >> d;
        g[s].push_back(Edge<int>(t,d));
        g[t].push_back(Edge<int>(s,d));
    }

    auto p = Prim(g);
    cout << p.first << '\n';

    // printf("ExecutionTime: %d /ms\n",1000*(int)((clock()-_StartTime_)/CLOCKS_PER_SEC));
}


Measured memory_bytes: 10320