#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<complex> #define N 400010 #define M 900010 #define INF 2147483647000000 #define ll long long using namespace std; ll read() { int x=0,f=1;char s=getchar(); while(s>‘9‘ || s<‘0‘){if(s==‘-‘)f=-1;s=getchar();} while(s<=‘9‘ && s>=‘0‘){x=x*10+s-‘0‘;s=getchar();} return x*f; } struct edge{ll to,next,v;}a[N<<1]; struct node{ll x,y,v;}e[M]; bool operator < (node a,node b){return a.v<b.v;} ll cnt,head[N],t=0; void add(ll x,ll y,ll v){a[++cnt].to=y;a[cnt].v=v;a[cnt].next=head[x];head[x]=cnt;} ll bz[N][21],deep[N],m1[N][21],m2[N][21],f[N],V[N]; ll find(ll x){if(x==f[x])return x;f[x]=find(f[x]);return f[x];} int n,m; void dfs(ll x,ll fa) { bz[x][0]=fa; for(int i=head[x];i;i=e[i].next) { ll v=a[i].to; if(v==fa)continue; deep[v]=deep[x]+1; m1[v][0]=a[i].d; m2[v][0]=-INF; dfs(v,x); } } void twofold() { for(int i=1;i<=20;i++) for(int j=1;j<=n;j++) { bz[j][i]=bz[bz[j][i-1]][i-1]; m1[j][i]=max(m1[j][i-1],m1[bz[j][i-1]][i-1]); m2[j][i]=max(m2[j][i-1],m2[bz[j][i-1]][i-1]); if(m1[j][i-1]>m1[bz[j][i-1]][i-1])m2[j][i]=max(m2[j][i],m1[bz[j][i-1]][i-1]); else if(m1) } } int main() { n=read();m=read(); for(int i=1;i<=m;i++)e[i].x=read(),e[i].y=read(),e[i].v=read(); sort(e+1,e+m+1); for(int i=1;i<=n;i++)f[i]=i; for(int i=1;i<=m;i++) { ll p=find(e[i].x),q=find(e[i].y); if(p!=q) { t+=e[i].v;f[p]=q;V[i]=1; add(e[i].x,e[i].y,e[i].v);add(e[i].y,e[i].x,e[i].v); } } deep[1]=1;m2[1][0]=-INF; dfs(1,-1);twofold(); ll ans=INF; for(int i=1;i<=m;i++) if(!V[i]) { ll x=a[i].x,y=a[i].y,d=a[i].d,l=lca(x,y); ans=min(ans,t-max(qmax(x,lca,d),qmax(lca,y,d))+d); } printf("%d\n",ans); return 0; }