#include <stdio.h>
#define INF 1010001000
#define min(a, b) a<b?a:b

int cost[10000][10000];
int used[10000], mincost[10000];
int v, e;

int prim(void);

int main(void)
{
    int i, j, f, t, d;
    scanf("%d %d", &v, &e);
    for (i = 0; i < v; i++) {
        for (j = 0; j < v; j++) {
            cost[i][j] = INF;
        }
    }
    for (i = 0; i < e; i++) {
        scanf("%d %d %d", &f, &t, &d);
        cost[f][t] = d;
        cost[t][f] = d;
    }
    printf("%d\n", prim());
    return 0;
}

int prim(void)
{
    int i, j, res, p;
    for (i = 0; i < v; i++) {
        mincost[i] = INF;
        used[i] = 0;
    }
    mincost[0] = 0;
    res = 0;

    while (1) {
        p = -1;
        for (i = 0; i < v; i++) {
            if (!used[i] && (p == -1 || mincost[i] < mincost[p])) {
                p = i;
            }
        }
        if (p == -1) {
            break;
        }
        used[p] = 1;
        res += mincost[p];

        for (i = 0; i < v; i++) {
            mincost[i] = min(mincost[i], cost[p][i]);
        }
    }
    return res;
}