#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf INT_MAX
#define fr(i,l,r) for (i=(l); i<=(r); i++)
#define rfr(i,l,r) for (i=(l); i>=(r); i--)
template<typename T>inline void read(T &n){
T w=1; n=0; char ch=getchar();
while (!isdigit(ch) && ch!=EOF){ if (ch=='-') w=-1; ch=getchar(); }
while (isdigit(ch) && ch!=EOF){ n=(n<<3)+(n<<1)+(ch&15); ch=getchar(); }
n*=w;
}
template<typename T>inline void write(T x){
if (x==0){ putchar('0'); return ; }
T tmp;
if (x>0) tmp=x;
else tmp=-x;
if (x<0) putchar('-');
char F[105];
long long cnt=0;
while (tmp){
F[++cnt]=tmp%10+48;
tmp/=10;
}
while (cnt) putchar(F[cnt--]);
}
#define Min(x,y) x = min(x,y)
#define Max(x,y) x = max(x,y)
//基础配置=================================================================================
const ll N = 200005, M = 500005;
ll n,m;
struct edge{
ll x,y,w;
edge(ll _x=0,ll _y=0,ll _w=0){
x = _x, y = _y, w = _w;
}
bool operator < (const edge o)const{
return w<o.w;
}
};
edge e[M+M];
ll find(ll x){
if (fa[x]==x) return x;
else return fa[x] = find(fa[x]);
}
ll fa[N];
int main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ll i,j;
read(n); read(m);
fr(i,1,m){
ll x,y,w; read(x); read(y); read(w);
e[i] = edge(x,y,w);
}
sort(e+1,e+1+m);
fr(i,1,n) fa[i] = i;
ll ans = 0;
fr(i,1,m){
ll x = find(e[i].x), y = find(e[i].y);
if (x!=y) fa[y] = x, ans += e[i].w;
}
write(ans);
return 0;
}
//g++ a.cpp -o a -Wall -Wl,--stack=512000000 -std=c++11 -O2