#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<algorithm>
#include<cstdlib>
using namespace std;
#define re register
#define maxn 100010
#define int long long
const long long inf = 0x7fffffff;
int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return x*f;
}
struct nn
{
int s,t,v;
bool p;
}z[300030];
struct node
{
int s,t,v,next;
}edge[maxn<<1];
int head [maxn];
int cnt;
void add (int s,int t,int v)
{
edge [++cnt ].s=s;
edge [cnt ].t=t;
edge [cnt ].v=v;
edge [cnt ].next=head [s];
head [s]=cnt ;
}
int fa[maxn];
int f[maxn][23],g[maxn][23],h[maxn][23];
int dep[maxn];
void dfs(int u,int ff,int vv)
{
// cout<<u<<' ';
f[u][0]=ff;
h[u][0]=vv,g[u][0]=-inf;
dep[u]=dep[ff]+1;
for(re int i=1;i<=20;++i)
f[u][i]=f[f[u][i-1]][i-1];
for(re int i=1;i<=20;++i)
h[u][i]=max(h[u][i-1],h[f[u][i-1]][i-1]);
for(re int i=1;i<=20;++i)
if(h[u][i-1]==h[f[u][i-1]][i-1])
g[u][i]=max(g[u][i-1],g[f[u][i-1]][i-1]);
else
if(h[u][i-1]<h[f[u][i-1]][i-1])
g[u][i]=max(h[u][i-1],g[f[u][i-1]][i-1]);
else
g[u][i]=max(g[u][i-1],h[f[u][i-1]][i-1]);
for(re int i=head[u];i;i=edge[i].next)
{
int tt=edge[i].t;
if(tt==ff) continue;
dfs(tt,u,edge[i].v);
}
}
int find(int x)
{
if(fa[x]==x) return x;
return fa[x]=find(fa[x]);
}
bool cmp(nn x,nn y)
{
return x.v<y.v;
}
void query(int p1,int p2,int &x,int &y)
{
int ans=0,gg=-1;
if(dep[p1]<dep[p2]) swap(p1,p2);
for(re int i=20;i>=0;--i)
if(dep[f[p1][i]]>=dep[p2])
{
if(h[p1][i]==ans)
gg=max(gg,g[p1][i]);
else
if(h[p1][i]<ans)
gg=max(gg,h[p1][i]);
else
gg=max(ans,g[p1][i]),ans=max(ans,h[p1][i]);
p1=f[p1][i];
}
if(p1==p2)
{
x=ans,y=gg;
return ;
}
for(re int i=20;i>=0;--i)
if(f[p1][i]!=f[p2][i])
{
if(h[p1][i]==ans)
gg=max(gg,g[p1][i]);
else
if(h[p1][i]<ans)
gg=max(gg,h[p1][i]);
else
gg=max(ans,g[p1][i]),ans=max(ans,h[p1][i]);
p1=f[p1][i];
if(h[p2][i]==ans)
gg=max(gg,g[p2][i]);
else
if(h[p2][i]<ans)
gg=max(gg,h[p2][i]);
else
gg=max(ans,g[p2][i]),ans=max(ans,h[p2][i]);
p2=f[p2][i];
}
if(h[p1][0]==ans)
gg=max(gg,g[p1][0]);
else
if(h[p1][0]<ans)
gg=max(gg,h[p1][0]);
else
gg=max(ans,g[p1][0]),ans=max(ans,h[p1][0]);
if(h[p2][0]==ans)
gg=max(gg,g[p2][0]);
else
if(h[p2][0]<ans)
gg=max(gg,h[p2][0]);
else
gg=max(ans,g[p2][0]),ans=max(ans,h[p2][0]);
x=ans,y=gg;
}
int aaa=0;
int root=0;
int tot=0x7fffffff;
int n,m;
bool p=false;
signed main()
{
// freopen("a.in","r",stdin);
// freopen("a.out","w",stdout);
// printf("%dM\n",( sizeof(a) >> 20 ));
cin>>n>>m;
for(re int i=1;i<=m;++i)
{
int a,b,c;
a=read(),b=read(),c=read();
z[i].s=a,z[i].t=b,z[i].v=c;
}
for(re int i=1;i<=n;++i) fa[i]=i;
sort(z+1,z+m+1,cmp);
int amo=0;
for(re int i=1;i<=m;++i)
{
int fx=find(z[i].s),fy=find(z[i].t);
if(amo==n-1)
break;
if(fx!=fy)
{
if(!p) root=z[i].s,p=true;
amo++;
aaa+=z[i].v;
fa[fx]=fy;
// cout<<z[i].s<<' '<<z[i].t<<endl;
add(z[i].s,z[i].t,z[i].v);
add(z[i].t,z[i].s,z[i].v);
z[i].p=true;
}
}
// printf("[%lld]\n",root);
dfs(root,0,-inf);
// puts("");
for(re int i=1;i<=m;++i)
{
if(z[i].p) continue;
int v1=0,v2=0;
query(z[i].s,z[i].t,v1,v2);
if(z[i].v==v1)
tot=min(tot,aaa-v2+v1);
else
tot=min(tot,aaa-v1+z[i].v);
}
// cout<<(int)inf<<endl;
cout<<tot;
// cout<<aaa<<' '<<tot;
// while(1)
// {
// int a,b;
// cin>>a>>b;
// int v1,v2;
// query(a,b,v1,v2);
// // cout<<v1<<' '<<v2<<endl;
// }
return 0;
}