import sys input = sys.stdin.readline sys.setrecursionlimit(10 ** 6) def main(): N = int(input()) edges = [[] for _ in range(N)] path = [] for _ in range(N-1): u, v, w = map(int, input().split()) u -= 1 v -= 1 edges[u].append(v) edges[v].append(u) path.append([u,v,w]) subtree_size = [0] * N def dfs(v, p): res = 1 for nv in edges[v]: if nv == p: continue res += dfs(nv, v) subtree_size[v] = res return res dfs(0,-1) ans = 0 for u, v, w in path: if subtree_size[u] > subtree_size[v]: ans += 2 * subtree_size[v] * (N - subtree_size[v]) * w else: ans += 2 * subtree_size[u] * (N - subtree_size[u]) * w print(ans) if __name__ == "__main__": main()