N = int(input()) G = [[] for _ in range(N+1)] edge = [] for _ in range(N-1): a,b,c = map(int,input().split()) G[a].append((b,c)) G[b].append((a,c)) edge.append((c,a,b)) edge.sort(reverse = True,key = lambda x:x[0]) M,u,v = edge[0] _min = edge[0][0] import sys sys.setrecursionlimit(10 ** 8) parent = [0] * (N+1) parent[u] = (u,M) parent[v] = (v,M) """ def dfs(now,p): for v,c in G[now]: if v == p:continue parent[v] = (now,c) dfs(v,now) """ def dfs(now,p): stack = [(now,p)] while stack: now,p = stack.pop() for v,c in G[now]: if v == p:continue parent[v] = (now,c) stack.append((v,now)) dfs(u,v) dfs(v,u) memo = [0] * (N+1) memo[u] = 1 memo[v] = 1 for i in range(1,N-1): c,a,b = edge[i] if memo[a] == 1 and memo[b] == 1: continue tmp = _min if parent[b][0] == a: a,b = b,a d = a while memo[a] == 0: memo[a] = 1 if parent[a][1] < tmp: tmp = parent[a][1] a = parent[a][0] if a != u and a != v: break if M - tmp > c: break if u == a: u = d else: v = d _min = tmp while parent[v][0] != v: parent[v] = (parent[v][0],parent[v][1] - _min) v = parent[v][0] while parent[u][0] != u: parent[u] = (parent[u][0],parent[u][1] - _min) u = parent[u][0] parent[u] = (parent[u][0],parent[u][1] - _min) parent[v] = (parent[v][0],parent[v][1] - _min) ans = 0 for i in range(1,N+1): if parent[i][1] > ans: ans = parent[i][1] print(ans)