from sys import stdin n, *indata = map(int, stdin.read().split()) offset = 0 a = [] b = [] c = [] g = [[] for i in range(n+1)] for i in range(n-1): s, t, d = indata[offset + 3*i]-1,indata[offset + 3*i+1]-1,indata[offset + 3*i+2] a.append(s) b.append(t) c.append(d) g[s].append((t,d)) g[t].append((s,d)) left = -1 right = max(c) + 2 while right - left >= 2: mid = (right + left) // 2 mustin = [0 for i in range(n)] start = -1 for i in range(n-1): if c[i] > mid: mustin[a[i]] = 1 mustin[b[i]] = 1 start = i if start == -1: right = mid continue mustvisit = sum(mustin) visited = 0 used = [c[start]] check = [False for i in range(n)] check[a[start]] = True check[b[start]] = True que = [(a[start],0,-1)] while que: now, inout, come = que.pop() if inout == 0: check[now] = True if come != -1: used.append(come) visited += mustin[now] if visited == mustvisit: break que.append((now,1,come)) for i, cost in g[now]: if not check[i]: que.append((i,0,cost)) else: if mustin[now]: break else: used.pop() que = [(b[start],0,-1)] while que: now, inout, come = que.pop() if inout == 0: check[now] = True if come != -1: used.append(come) visited += mustin[now] if visited == mustvisit: break que.append((now,1,come)) for i, cost in g[now]: if not check[i]: que.append((i,0,cost)) else: if mustin[now]: break else: used.pop() if visited != mustvisit: left = mid else: val = max(used) - min(used) if val <= mid: right = mid else: left = mid print("{}".format(right))