# [NOIP2015]运输计划

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

x = 0; int f = 0; char s = getchar();
while (s < '0' || '9' < s) f |= s == '-', s = getchar();
while ('0' <= s && s <= '9') x = x * 10 + s - 48, s = getchar();
x = f ? -x : x;
}

const int N = 3e5 + 10, M = 19;

int n, m, t;
int tot = 1, Head[N], ver[N << 1], Next[N << 1], Leng[N];
int fa[N][M], s[N][M], dep[N], dis[N];
struct query {
int x, y, dis, lca;
} q[N];

void add(int x, int y) {
tot++;
ver[tot] = y;
}

void dfs(int x) {
for (int i = 1; (1 << i) < dep[x]; i++) {
int y = fa[x][i - 1];
fa[x][i] = fa[y][i - 1];
s[x][i] = max(s[x][i - 1], s[y][i - 1]);
}
for (int i = Head[x]; i; i = Next[i]) {
int y = ver[i];
if (fa[x][0] == y) continue;
fa[y][0] = x;
s[y][0] = Leng[i >> 1];
dep[y] = dep[x] + 1;
dis[y] = dis[x] + Leng[i >> 1];
dfs(y);
}
}

int getlca(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
for (int i = t; i >= 0; i--)
if (dep[fa[x][i]] >= dep[y])
x = fa[x][i];
if (x == y) return x;
for (int i = t; i >= 0; i--)
if (fa[x][i] != fa[y][i])
x = fa[x][i], y = fa[y][i];
x = fa[x][0];
return x;
}

bool cmp(query p1, query p2) {
return p1.dis > p2.dis;
}

int getmax(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
int ans = 0;
for (int i = t; i >= 0; i--)
if (dep[fa[x][i]] >= dep[y])
ans = max(ans, s[x][i]), x = fa[x][i];
if (x == y) return ans;
for (int i = t; i >= 0; i--)
if (fa[x][i] != fa[y][i]) {
ans = max(ans, max(s[x][i], s[y][i]));
x = fa[x][i], y = fa[y][i];
}
ans = max(ans, max(s[x][0], s[y][0]));
return ans;
}

bool cmp1(int p1, int p2) {
return dep[p1] > dep[p2];
}

int main() {
cin >> n >> m;
t = log(n) / log(2);
for (int i = 1; i < n; i++) {
int x, y, z;
Leng[tot >> 1] = z;
}
dep[1] = 1; dfs(1);
int mx = 0;
for (int i = 0, j = 1; j <= m; j++) {
if (x == y) continue;
mx = ++i;
q[i].x = x, q[i].y = y;
q[i].lca = getlca(q[i].x, q[i].y);
q[i].dis = dis[q[i].x] - 2 * dis[q[i].lca] + dis[q[i].y];
}
m = mx;
if (!m) { puts("0"); return 0; }
sort(q + 1, q + m + 1, cmp);
mx = getmax(q[1].x, q[1].y);
int ans = max(q[2].dis, q[1].dis - mx), x = q[1].x, y = q[1].y, lc = q[1].lca;
for (int i = 2; i <= m; i++) {
int tx = x, ty = y, p[5];
p[1] = getlca(tx, q[i].x);
p[2] = getlca(tx, q[i].y);
p[3] = getlca(ty, q[i].x);
p[4] = getlca(ty, q[i].y);
sort(p + 1, p + 4 + 1, cmp1);
x = p[1], y = p[2];
if (x == y || dep[x] <= max(dep[q[i].lca], dep[lc])) break;
mx = getmax(x, y);
lc = getlca(x, y);
ans = min(ans, max(q[1].dis - mx, q[i + 1].dis));
}
cout << ans << endl;
return 0;
}


08-16 52

02-12 548
03-29 195
10-08 23
05-28 29
08-24 33
04-13 18
10-22 154
10-06 579
09-02 384
11-11 5763
09-10 647
08-20
03-30