#define ll long long int
#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
using namespace std;
struct graph {
int a, b;
int w;
bool operator<(const graph& item) const {
if (w > item.w) {
return true;
}
else return false;
}
};
int N, M;
int arr[100003];
int parent[100003];
priority_queue<struct graph> pq;
int getParent(int x) {
if (x == parent[x]) return x;
else return parent[x] = getParent(parent[x]);
}
void setUnion(int a, int b) {
int x = getParent(a);
int y = getParent(b);
if (x < y) {
parent[y] = x;
}
else {
parent[x] = y;
}
return;
}
bool isUnion(int a, int b) {
if (getParent(a) == getParent(b)) return true;
else return false;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> N >> M;
for (int i = 0; i < M; i++) {
int a, b, w;
cin >> a >> b >> w;
struct graph item = { a, b, w };
pq.push(item);
}
for (int i = 1; i <= N; i++) parent[i] = i;
int cnt = 0;
int last_w = 0;
while (!pq.empty()) {
struct graph item = pq.top();
pq.pop();
if (!isUnion(item.a, item.b)) {
cnt += item.w;
last_w = item.w;
setUnion(item.a, item.b);
}
}
cout << cnt - last_w << "\n";
return 0;
}