void init() { scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w); }
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
void kruskal() { sort(e + 1, e + m + 1); for (int i = 1; i <= n; i++) fa[i] = i; for (int i = 1; i <= m; i++) { int u = e[i].u, v = e[i].v; int fu = find(u), fv = find(v); if (fu != fv) { e[i].is = 1; anspre += e[i].w; add(u, v, e[i].w); tot++; if (tot == n - 1) break; fa[fu] = fv; } } }
void dfs(int x, int ff) { f[x][0] = ff; R[x] = R[ff] + 1; for (int i = fir[x]; i; i = nxt[i]) if (to[i] != ff) { st[to[i]][0] = val[i]; stc[to[i]][0] = val[i]; dfs(to[i], x); } }
void make_st() { for (int i = 1; i <= 30; i++) for (int j = 1; j <= n; j++) { f[j][i] = f[f[j][i - 1]][i - 1]; int u = st[j][i - 1], v = st[f[j][i - 1]][i - 1]; st[j][i] = max(u, v); if (u && v && u != v) stc[j][i] = min(u, v); if (stc[j][i - 1]) stc[j][i] = max(stc[j][i], stc[j][i - 1]); if (stc[f[j][i - 1]][i - 1]) stc[j][i] = max(stc[j][i], stc[f[j][i - 1]][i - 1]); } }
int swapp(LL x, LL &zd, LL &cd) { if (x == zd) return 0; if (x > zd) { cd = max(cd, zd); zd = x; } else cd = max(cd, x); }
int lca(int x, int y) { rem = 0, rec = 0; if (R[x] for (int i = 30; i >= 0; i--) { if (R[f[x][i]] >= R[y]) { if (rem && st[x][i] > rem) rec = max(rem, rec); rem = max(rem, st[x][i]); if (i != 0) rec = max(rec, stc[x][i]); x = f[x][i]; } } if (x == y) return 1; for (int i = 30; i >= 0; i--) { if (f[x][i] != f[y][i]) { swapp(stc[y][i], rem, rec); swapp(stc[x][i], rem, rec); swapp(st[x][i], rem, rec); swapp(st[y][i], rem, rec); x = f[x][i]; y = f[y][i]; } } swapp(stc[y][0], rem, rec); swapp(stc[x][0], rem, rec); swapp(st[x][0], rem, rec); swapp(st[y][0], rem, rec); return 1; }
void work() { ans = 1e18; for (int i = 1; i <= m; i++) if (e[i].is != 1) { int x = e[i].u, y = e[i].v; lca(x, y); if (e[i].w != rem) ans = min(ans, anspre - rem + e[i].w); else if (e[i].w != rec) ans = min(ans, anspre - rec + e[i].w); } printf("%lld\n", ans); }