#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
constexpr int N=1e4+10;
constexpr int mod=1e9+7;
struct p
{
int l;
int r;
int dis;
bool operator <(const p& x) const{return x.dis<dis;}
}t;
bool v[N];
void solve()
{
int n,m;
priority_queue<p> pq;
cin >> n >> m;
std::vector<int> f[N];
vector<int> ds[N];
for(int i=1;i<=m;i++)
{
int l,r,d;
cin >> l >> r >> d;
f[l].emplace_back(r);
f[r].emplace_back(l);
ds[l].emplace_back(d);
ds[r].emplace_back(d);
}
v[1]=true;
for(auto i=f[1].begin();i!=f[1].end();i++)
{
int num=i-f[1].begin();
pq.push(p{1,*i,*(ds[1].begin()+num)});
}
int ans=0;
while(!pq.empty())
{
t=pq.top();
pq.pop();
if(!v[t.r])
{
ans+=t.dis;
v[t.r]=true;
for(auto i=f[t.r].begin();i!=f[t.r].end();i++)
{
int num=i-f[t.r].begin();
pq.push(p{t.r,*i,*(ds[t.r].begin()+num)});
}
}
}
cout << ans << endl;
}
signed main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
solve();
return 0;
}