#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/all/GRL_2_A"
#include "../../cpp/template/template.cpp"
#include "../../cpp/graph/MST/kruskal.cpp"
int main(){
int v,e;cin>>v>>e;
Kruskal A(v);
for(int i = 0; e > i; i++){
long long s,t,w;cin>>s>>t>>w;
A.push(s,t,w);
}
A.build();
cout << A.cost << endl;
}
#line 1 "cpp/z_test/aoj-GRL_2_A.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/all/GRL_2_A"
#line 2 "cpp/template/template.cpp"
//yukicoder@cpp17
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <climits>
#include <cassert>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <random>
#include <bitset>
#include <complex>
#include <utility>
#include <numeric>
#include <functional>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
const ll MOD = 998244353;
const ll MODx = 1000000007;
const int INF = (1<<30)-1;
const ll LINF = (1LL<<62LL)-1;
const double EPS = (1e-10);
P ar4[4] = {{0,1},{0,-1},{1,0},{-1,0}};
P ar8[8] = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }
/*
確認ポイント
cout << fixed << setprecision(n) << 小数計算//n桁の小数表記になる
計算量は変わらないが楽できるシリーズ
min(max)_element(iter,iter)で一番小さい(大きい)値のポインタが帰ってくる
count(iter,iter,int)でintがiterからiterの間にいくつあったかを取得できる
*/
/* comment outed because can cause bugs
__attribute__((constructor))
void initial() {
cin.tie(0);
ios::sync_with_stdio(false);
}
*/
#line 4 "cpp/z_test/aoj-GRL_2_A.test.cpp"
#line 1 "cpp/data-structure/union-find.cpp"
struct UnionFind {
int n;
vector<int> par;
vector<int> size_;
UnionFind(int n_) : n(n_), par((size_t)n_), size_((size_t)n_,1){
for(int i = 0; n > i; i++)par[i] = i;
}
int root(int x){
if(par[x] == x)return x;
return par[x] = root(par[x]);
}
void unite(int a,int b){
int ra = root(a);
int rb = root(b);
if(ra==rb)return;
if(size(ra) > size(rb)) swap(ra,rb);
par[ra] = rb;
size_[rb] += size_[ra];
}
bool same(int a, int b){
return root(a) == root(b);
}
int size(int a){
return size_[root(a)];
}
void debug(){
for(int i = 0; n > i; i++){
cout << size_[root(i)] << " ";
}
cout << endl;
return;
}
};
#line 2 "cpp/graph/MST/kruskal.cpp"
struct Kruskal {
struct edge {
int v1,v2,cost;
bool operator<(const edge &p){
return (*this).cost < p.cost;
}
bool operator==(const edge &p){
return (*this).cost == p.cost;
}
bool operator>(const edge &p){
return (*this).cost > p.cost;
}
};
int n;
int cost = -1;
vector<edge> K;
Kruskal(int n_):n(n_) {}
void push(int v1, int v2, int cost){
K.push_back({v1,v2,cost});
}
void build(){
vector<edge> after_K;
vector<edge> remunant_K;
sort(K.begin(),K.end());
UnionFind C(n);
cost = 0;
for(int i = 0; K.size() > i; i++){
if(C.same(K[i].v1,K[i].v2)){
remunant_K.push_back(K[i]);
continue;
}
cost += K[i].cost;
after_K.push_back(K[i]);
C.unite(K[i].v1,K[i].v2);
}
if(after_K.size() != n-1)cost = -1;
}
};
#line 6 "cpp/z_test/aoj-GRL_2_A.test.cpp"
int main(){
int v,e;cin>>v>>e;
Kruskal A(v);
for(int i = 0; e > i; i++){
long long s,t,w;cin>>s>>t>>w;
A.push(s,t,w);
}
A.build();
cout << A.cost << endl;
}