import sys input = sys.stdin.readline sys.setrecursionlimit(10 ** 10) 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,w]) edges[v].append([u,w]) path.append([u,v,w]) subtree_size = [-1] * N def dfs(v, p): for nv, w in edges[v]: if nv == p: continue dfs(nv, v) subtree_size[v] = 1 for c, w in edges[v]: if c == p: continue subtree_size[v] += subtree_size[c] 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()