#include using namespace std; using ll = long long ; using P = pair; using Vec = vector; #define FOR(i, m, n) for(ll (i) = (m); (i) < (n); (i)++) #define FORN(i, m, n) for(ll (i) = (m); (i) <= (n); (i)++) #define FORR(i, m, n) for(ll (i) = (m); (i) >= (n); (i)--) #define rep(i, n) FOR(i, 0, n) #define repn(i, n) FORN(i, 1, n) #define repr(i, n) FORR(i, n, 0) #define repnr(i, n) FORR(i, n, 1) #define co(n) cout << (n) << endl #define cosp(n) cout << (n) << ' ' #define setp(n) cout << fixed << setprecision(n); #define all(s) (s).begin(), (s).end() #define pb push_back #define mp make_pair #define fs first #define sc second const ll INF = 1e9+1; const ll LINF = 1e18+1; const ll MOD = 1e9+7; //const ll MOD = 998244353; const double PI = acos(-1); const double EPS = 1e-9; vector > tree; Vec child_num; ll ans = 0; ll n = 0; void dfs(ll node, ll par){ ll cost = 0; for(P i : tree[node]){ if(i.fs != par) dfs(i.fs, node); else cost = i.sc; } if(node != par){ child_num[par] += child_num[node]; ans += child_num[node]*(n-child_num[node])*cost*2; } } int main(void){ cin >> n; tree.resize(n); child_num.resize(n); fill(all(child_num), 1); rep(i, n-1){ ll u, v, w; cin >> u >> v >> w; u--, v--; tree[u].pb(mp(v, w)); tree[v].pb(mp(u, w)); } dfs(0, 0); co(ans); return 0; }