#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;
const int N = 1e5 + 10, M = 6e5 + 10, INF = 0x3f3f3f3f;
int n, m;
struct edge{
int a, b, c;
bool isu;
bool operator<(edge& e)
{
return c < e.c;
}
} E[M];
int h[N], e[M], ne[M], w[M], idx;
int depth[N];
int p[N];
int fa[N][17], dist1[N][17], dist2[N][17];
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}
int find(int u)
{
if (p[u] != u) p[u] = find(p[u]);
return p[u];
}
LL kruskal()
{
LL res = 0;
for (int i = 1; i <= n; i ++) p[i] = i;
sort(E, E + m);
for (int i = 0; i < m; i ++)
{
int a = E[i].a, b = E[i].b, c = E[i].c;
a = find(a), b = find(b);
if (a != b)
{
p[a] = b;
res += c;
E[i].isu = true;
}
}
return res;
}
void build()
{
for (int i = 0; i < m; i ++)
if (E[i].isu)
{
int a = E[i].a, b = E[i].b, c = E[i].c;
add(a, b, c), add(b, a, c);
}
}
void bfs()
{
memset(depth, 0x3f, sizeof depth);
int q[N], hh = 0, tt = -1;
depth[0] = 0, depth[1] = 1;
q[++ tt] = 1;
while (hh <= tt)
{
int t = q[hh ++];
for (int i = h[t]; ~i; i = ne[i])
{
int j = e[i];
if (depth[j] > depth[t] + 1)
{
depth[j] = depth[t] + 1;
q[++ tt] = j;
fa[j][0] = t;
dist1[j][0] = w[i], dist2[j][0] = -INF;
for (int k = 1; k <= 16; k ++)
{
int anc = fa[j][k - 1];
fa[j][k] = fa[anc][k - 1];
int distance[] = {dist1[j][k - 1], dist2[j][k - 1], dist1[anc][k - 1], dist2[anc][k - 1]};
int d1 = -INF, d2 = -INF;
for (int i = 0; i < 4; i ++)
{
if (distance[i] > d1) d2 = d1, d1 = distance[i];
else if(distance[i] > d2 && distance[i] != d1) d2 = distance[i];
}
dist1[j][k] = d1, dist2[j][k] = d2;
}
}
}
}
}
int lca(int a, int b, int w)
{
static int distance[N * 2];
int cnt = 0;
if (depth[a] < depth[b]) swap(a, b);
for (int k = 16; k >= 0; k --)
if (depth[fa[a][k]] >= depth[b])
{
distance[cnt ++] = dist1[a][k];
distance[cnt ++] = dist2[a][k];
a = fa[a][k];
}
if (a != b)
{
for (int k = 16; k >= 0; k --)
if (fa[a][k] != fa[b][k])
{
distance[cnt ++] = dist1[a][k];
distance[cnt ++] = dist2[a][k];
distance[cnt ++] = dist1[b][k];
distance[cnt ++] = dist2[b][k];
a = fa[a][k];
b = fa[b][k];
}
distance[cnt ++] = dist1[a][0];
distance[cnt ++] = dist1[b][0];
}
int d1 = -INF, d2 = -INF;
for (int i = 0; i < cnt; i ++)
{
int d = distance[i];
if (d > d1) d2 = d1, d1 = d;
else if (d != d1 && d > d2) d2 = d;
}
if (w > d1) return w - d1;
if (w > d2) return w - d2;
return INF;
}
int main(void)
{
cin >> n >> m;
memset(h, -1, sizeof h);
for (int i = 0; i < m; i ++)
cin >> E[i].a >> E[i].b >> E[i].c;
LL res = kruskal();
build();
bfs();
LL ans = 1e18;
for (int i = 0; i < m; i ++)
if (!E[i].isu)
{
int a = E[i].a, b = E[i].b, w = E[i].c;
ans = min(ans, res + lca(a, b, w));
}
printf("%lld", ans);
return 0;
}