#include<bits/stdc++.h>
#define INF 0x7f7f7f7f
using namespace std;
int N,M,lowcost[10005],closet[10005],c[5005][5005];
bool s[5005];
void Prim(int n)
{
memset(s,false,sizeof s);
s[1]=true;
for(int i=2;i<=n;i++)
{
lowcost[i]=c[1][i];
closet[i]=1;
}
for(int i=1;i<n;i++)
{
int temp=INF;
int t=1;
for(int j=1;j<=n;j++)
{
if(!s[j]&&lowcost[j]<temp)
{
t=j;
temp=lowcost[j];
}
}
if(t==1)break;
s[t]=true;
for(int j=1;j<=n;j++)
{
if(!s[j]&&c[t][j]<lowcost[j])
{
lowcost[j]=c[t][j];
closet[j]=t;
}
}
}
}
int main()
{
int ans=0;
cin>>N>>M;
for(int i=1;i<=M;i++)
{
int x,y,z;
cin>>x>>y>>z;
c[x][y]=z;
c[y][x]=z;
}
Prim(N);
for(int i=1;i<=10005;i++)
{
ans+=lowcost[i];
}
cout<<ans;
return 0;
}