#include <bits/stdc++.h>

using namespace std;

const int INF = 1 << 30;

template< typename T >
struct edge {
  int src, to;
  T cost;

  edge(int to, T cost) : src(-1), to(to), cost(cost) {}

  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}

  edge &operator=(const int &x) {
    to = x;
    return *this;
  }

  operator int() const { return to; }
};

template< typename T >
using Edges = vector< edge< T > >;
template< typename T >
using WeightedGraph = vector< Edges< T > >;
using UnWeightedGraph = vector< vector< int > >;
template< typename T >
using Matrix = vector< vector< T > >;

template< typename T >
T prim(WeightedGraph< T > &g) {
  using Pi = pair< T, int >;

  T total = 0;
  vector< bool > used(g.size(), false);
  priority_queue< Pi, vector< Pi >, greater< Pi > > que;
  que.emplace(0, 0);
  while(!que.empty()) {
    auto p = que.top();
    que.pop();
    if(used[p.second]) continue;
    used[p.second] = true;
    total += p.first;
    for(auto &e : g[p.second]) {
      que.emplace(e.cost, e.to);
    }
  }
  return total;
}

int main() {
  int V, E;
  scanf("%d %d", &V, &E);
  WeightedGraph< int > g(V);
  for(int i = 0; i < E; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    g[a].emplace_back(b, c);
    g[b].emplace_back(a, c);
  }
  printf("%d\n", prim(g));
}

//BINARY_BOY!!
//THINK TWICE CODE ONCE { TRUE }
// #####   ###  ##   #   ###   #####   #   #  #####	#####
// #    #   #   # #  #  #   #  #    #   # #   #	#	#	#	
// #####    #   #  # #  #####  #####     #    #####	#	#		
// #    #   #   #   ##  #   #  #   #     #    #	#	#	#
// #####   ###  #    #  #   #  #    #    #    #####	#####	
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int MAXN = 10000 + 5;
vector < pair < int  , pair < int , int > > > v ;
vector< int > v2 [ MAXN ];
int n , m, sum = 0, num [ MAXN ];
bool cmd(pair < int , pair<int , int> > p1 , pair < int , pair<int , int> > p2 )
{
	return p1.first < p2.first;
}
int main() {
	ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0);
	cin >> n >> m;
	for(int i = 0 ; i < m ; i++)
	{
		int a , b, c;
		cin >> a >> b >> c;
		v.push_back(make_pair(c , make_pair(a , b)));
	}
	sort(v.begin() , v.end() , cmd);
	for(int i = 0 ; i <= n ; i++)
	{
		v2[ i ].push_back(i);
		num [ i ] = i;
	}
	for(int i = 0 , j = 0 ; i < v.size() && j < n - 1; i++)
	{
		if(num [ v [ i ].second.first ] != num [ v [ i ].second.second ] )
		{
			sum += v [ i ].first;
			j++;
			if(v2 [ num [v [ i ].second.first ]].size() <  v2 [num[ v [ i ].second.second ]].size())
			{
				for(int f = 0 , l = v2 [ num [v [ i ].second.first ] ].size() , k = num [v [ i ].second.first]; f < l ; f++)
				{
					v2 [ num [v [ i ].second.second ] ].push_back(v2 [ k ][ f ]);
					num [v2 [ k ][ f ]] = num [v [ i ].second.second];
				}
			}
			else {
				for(int f = 0 , l = v2 [ num [v [ i ].second.second ] ].size() , k = num [v [ i ].second.second] ; f < l ; f++)
				{
					v2 [ num [v [ i ].second.first] ].push_back(v2 [ k ][ f ]);
					num [v2 [ k ][ f ]] = num [v [ i ].second.first];
				}
			}
		}
	}
	cout << sum << endl;
}