# import sys; input = sys.stdin.buffer.readline # sys.setrecursionlimit(10**7) from collections import defaultdict from collections import deque INF = float("inf") def getlist(): return list(map(int, input().split())) class Graph(object): def __init__(self): self.graph = defaultdict(list) def __len__(self): return len(self.graph) def add_edge(self, a, b): self.graph[a].append(b) class BFS(object): def __init__(self, graph, s, N): self.g = graph.graph self.Q = deque(); self.Q.append(s) self.dist = [INF] * N; self.dist[s] = 0 self.prev = [None] * N; self.prev[s] = -1 while self.Q: v = self.Q.popleft() for i in self.g[v]: if self.dist[i] == INF: self.dist[i] = self.dist[v] + 1 self.prev[i] = v self.Q.append(i) def DFS(G, W, visit, node): for i in G.graph[node]: if visit[i] != 1: visit[i] = 1 DFS(G, W, visit, i) W[node] += W[i] #処理内容 def main(): N = int(input()) G = Graph() edge = defaultdict(int) for i in range(N - 1): a, b, w = getlist() a, b = list(sorted([a, b])) a -= 1; b -= 1 edge[a * (10 ** 6) + b] = w G.add_edge(a, b) G.add_edge(b, a) #DFS W = [1] * N visit = [0] * N visit[0] = 1 DFS(G, W, visit, 0) # print(W) BF = BFS(G, 0, N) ans = 0 for i in range(1, N): p = BF.prev[i]; q = i weight = W[q] * (N - W[q]) p, q = list(sorted([p, q])) w = edge[p * (10 ** 6) + q] ans += weight * w print(ans * 2) if __name__ == '__main__': main()