import sys # sys.setrecursionlimit(200005) int1 = lambda x: int(x)-1 pDB = lambda *x: print(*x, end="\n", file=sys.stderr) p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr) def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() # dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] # dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] # inf = (1 << 63)-1 inf = (1 << 31)-1 # md = 10**9+7 md = 998244353 n = II() to = [[] for _ in range(n)] cc = [0] ci = [(0, 0)] for i in range(1, n): u, v, c = LI1() to[u].append((v, i)) to[v].append((u, i)) cc.append(c+1) ci.append((c+1, i)) ci.sort() mx = ci[-1][0] # pDB(ci) ep = [-1]*n ed = [0]*n st = [(0, 0)] while st: u, pi = st.pop() for v, i in to[u]: if i == pi: continue ep[i] = pi ed[i] = ed[pi]+1 st.append((v, i)) # pDB(ep, ed) def binary_search(l, r, ok, minimize): if minimize: l -= 1 else: r += 1 while l+1 < r: m = (l+r)//2 if ok(m) ^ minimize: l = m else: r = m if minimize: return r return l from bisect import bisect_right from collections import Counter def ok(m): si = bisect_right(ci, (m, n+1)) use = set() di = [] dcnt = Counter() for _, i in ci[si:]: use.add(i) d = ed[i] di.append((d, i)) if dcnt[d] == 2: return False dcnt[d] += 1 di.sort(key=lambda x: -x[0]) if len(di) > 2 and di[1][0] == di[2][0]: if di[0][0] != di[1][0]+1: return False i, j = di[0][1], di[2][1] if ep[i] != j: di[1], di[2] = di[2], di[1] j = di[2][1] if ep[i] != j: return False d0, i = di[0] d1, j = di[1] mn = cc[i] def move(i, mn): if i in use: use.remove(i) i = ep[i] mn = min(mn, cc[i]) return i, mn for _ in range(d0-d1): i, mn = move(i, mn) if mx-mn > m: return False if i == j: a = di[-1][1] while i != a: i, mn = move(i, mn) if mx-mn > m: return False use.remove(a) else: while 1: i, mn = move(i, mn) j, mn = move(j, mn) if i==j:break if mx-mn > m: return False return len(use) == 0 ans = binary_search(0, ci[-2][0], ok, True) print(ans)