#include "bits/stdc++.h"

using namespace std;

class UnionFind
{
    vector<int> par;

public:
    UnionFind(int n) : par(n, -1) {}

    int root(int x)
    {
        if (par[x] < 0)
            return x;
        else
            return par[x] = root(par[x]);
    }

    bool merge(int x, int y)
    {
        x = root(x);
        y = root(y);
        if (x == y)
            return false;
        if (par[x] > par[y])
            swap(x, y);
        par[x] += par[y];
        par[y] = x;
        return true;
    }

    int size(int x) { return -par[root(x)]; }
};

void solve()
{
    int n, m;
    cin >> n >> m;
    UnionFind uf(n);
    priority_queue<pair<long, pair<int, int>>> es;
    while (m--)
    {
        int s, t;
        long w;
        cin >> s >> t >> w;
        es.push(make_pair(-w, make_pair(s, t)));
    }
    long ans = 0;
    while (!es.empty())
    {
        auto p = es.top();
        es.pop();
        if (uf.merge(p.second.first, p.second.second))
        {
            ans -= p.first;
        }
    }
    cout << ans << endl;
}

int main()
{
    solve();
    return 0;
}

