#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 510, M = 1e4+10;
int n,m;
int tot,h[N],d[N][N][2],fa[N];
struct Edge{
int to,nxt,w;
}e[M << 1];
struct node{
int a,b,w,f;
bool operator<(const node&b)
{
return w < b.w;
}
}edge[M << 1];
int find(int x)
{
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
void add(int from,int to,int w)
{
e[tot].to = to, e[tot].w = w, e[tot].nxt = h[from];
h[from] = tot ++;
}
void dfs(int rt,int u,int fa,int m1,int m2)
{
d[rt][u][0] = m1;
d[rt][u][1] = m2;
for(int i = h[u]; ~i; i = e[i].nxt)
{
int to = e[i].to;
if(to == fa) continue;
m1 = d[rt][u][0], m2 = d[rt][u][1];
if(e[i].w > m1) m2 = m1,m1 = e[i].w;
else if(e[i].w < m1 && e[i].w > m2) m2 = e[i].w;
dfs(rt,to,u,m1,m2);
}
}
int main()
{
cin >> n >> m;
memset(h, -1, sizeof h); tot = 0;
for(int i = 1; i <= n; ++i) fa[i] = i;
for(int i = 0; i < m; ++i)
{
int a,b,w;
scanf("%d%d%d",&a,&b,&w);
edge[i] = {a,b,w};
}
sort(edge,edge+m);
LL sum = 0;
for(int i = 0; i < m; ++i)
{
int a = edge[i].a, b = edge[i].b, w = edge[i].w;
int f1 = find(a), f2 = find(b);
if(f1 != f2)
{
fa[f1] = f2;
sum += w;
add(a,b,w); add(b,a,w);
edge[i].f = true;
}
}
for(int i = 1; i <= n; ++i) dfs(i,i,-1,0,0);
LL res = 1e18;
for(int i = 0; i < m; ++i)
{
if(!edge[i].f)
{
int a = edge[i].a, b = edge[i].b, w = edge[i].w;
LL t;
if(w > d[a][b][0]) t = sum + w - d[a][b][0];
else if(w > d[a][b][1]) t = sum + w - d[a][b][1];
res = min(res,t);
}
}
printf("%lld\n",res);
return 0;
}