#include #include using namespace std; const int N=3010; int n,m,i,x,y,w,g[N],ans,f[N],ret; int F(int x){return f[x]==x?x:f[x]=F(f[x]);} int main(){ while(~scanf("%d%d",&n,&m)){ for(i=1;i<=n;i++)f[i]=i,g[i]=0; ret=n-1; while(m--){ scanf("%d%d%d",&x,&y,&w); if(x==y)continue; g[x]+=w; g[y]+=w; if(F(x)!=F(y)){ f[f[x]]=f[y]; ret--; } } ans=~0U>>1; for(i=1;i<=n;i++)ans=min(ans,g[i]); if(ret)ans=0; printf("%d\n",ans); } }