#include<bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define per(i, a, b) for(int i = a; i >= b; i--)
#define int long long
#define RD read()
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9') { if(ch == '-') f = -1; ch = getchar(); }
while('0' <= ch && ch <= '9') { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); }
return x * f;
}
int const N = 3e5 + 5;
int n, m;
struct edge {
int u, v, w;
bool chosen;
} e[N];
vector<pair<int, int> > G[N];
int fa[N];
int find(int x) {
if(fa[x] == x) return x;
return fa[x] = find(fa[x]);
}
int f[N][21], mx[N][21], mxs[N][21], dep[N];
int tmp[5];
int getMxs(int a, int b, int c, int d) {
tmp[0] = a, tmp[1] = b, tmp[2] = c, tmp[3] = d;
tmp[4] = -1;
sort(tmp, tmp + 4, [](int a, int b) { return a > b; });
int p = 1;
while(tmp[p] == tmp[p - 1] && p < 4) p++;
return tmp[p];
}
void dfs(int u, int pre) {
f[u][0] = pre;
dep[u] = dep[pre] + 1;
rep(i, 1, 20) {
f[u][i] = f[f[u][i - 1]][i - 1];
mx[u][i] = max(mx[u][i - 1], mx[f[u][i - 1]][i - 1]);
mxs[u][i] = getMxs(mx[u][i - 1], mx[f[u][i - 1]][i - 1],
mxs[u][i - 1], mxs[f[u][i - 1]][i - 1]);
}
for(pair<int, int> e : G[u]) if(e.first != pre) {
int v = e.first;
// printf("u = %lld, v = %lld, w = %lld\n", u, v, e.second);
mx[v][0] = e.second;
mxs[v][0] = -1;
dfs(v, u);
}
}
inline void upt(int &Mx, int &Mxs, int x) {
if(x > Mx) Mxs = Mx, Mx = x;
else if(x > Mxs && x != Mx) Mxs = x;
}
pair<int, int> ask(int x, int y) {
// printf("ask (%lld, %lld)\n", x, y);
int mxx = 0, mxxs = -1;
per(i, 20, 0) {
if(dep[f[x][i]] >= dep[y]) {
upt(mxx, mxxs, mx[x][i]);
upt(mxx, mxxs, mxs[x][i]);
x = f[x][i];
}
if(x == y) return {mxx, mxxs};
if(dep[x] == dep[y]) break;
}
per(i, 20, 0) if(f[x][i] != f[y][i]) {
upt(mxx, mxxs, mx[x][i]);
upt(mxx, mxxs, mxs[x][i]);
upt(mxx, mxxs, mx[y][i]);
upt(mxx, mxxs, mxs[y][i]);
x = f[x][i], y = f[x][i];
}
upt(mxx, mxxs, mx[x][0]);
upt(mxx, mxxs, mxs[x][0]);
return {mxx, mxxs};
}
signed main() {
n = RD, m = RD;
rep(i, 1, n) fa[i] = i;
rep(i, 1, m) e[i] = {RD, RD, RD, 0};
sort(e + 1, e + 1 + m, [](edge a, edge b) { return a.w < b.w; });
int tot = 0, sum = 0, ans = 2e18;
rep(i, 1, m) {
if(e[i].u == e[i].v) continue;
int fx = find(e[i].u), fy = find(e[i].v), w = e[i].w;
if(fx == fy) continue;
tot++, fa[fx] = fy;
e[i].chosen = 1;
G[e[i].u].push_back({e[i].v, w});
G[e[i].v].push_back({e[i].u, w});
sum += w;
if(tot == n - 1) break;
}
dfs(1, 0);
// printf("sum = %lld\n", sum);
rep(i, 1, m) {
if(e[i].chosen || e[i].u == e[i].v) continue;
int u = e[i].u, v = e[i].v, w = e[i].w;
int mxx = ask(u, v).first, mxxs = ask(u, v).second;
// printf("w = %lld, mxx = %lld, mxxs = %lld\n", w, mxx, mxxs);
if(mxx < w) {
ans = min(ans, sum - mxx + w);
} else if(mxx == w && mxxs < w) {
ans = min(ans, sum - mxxs + w);
}
}
printf("%lld\n", ans);
// printf("%lld\n", getMxs());
return 0;
}
/*
4 6
1 2 1
1 3 3
1 4 10
1 2 5
2 2 2
1 1 4
*/
调了好久qvq