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 = set() start = -1 for i in range(n-1): if c[i] > mid: mustin.add(a[i]) mustin.add(b[i]) start = i if start == -1: right = mid continue mustvisit = len(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) if now in mustin: visited += 1 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 now in mustin: 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) if now in mustin: visited += 1 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 now in mustin: 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))