#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


template<typename F>
struct FixPoint : F{
  FixPoint(F&& f):F(forward<F>(f)){}
  template<typename... Args>
  decltype(auto) operator()(Args&&... args) const{
    return F::operator()(*this,forward<Args>(args)...);
  }
};
template<typename F>
inline decltype(auto) MFP(F&& f){
  return FixPoint<F>{forward<F>(f)};
}

//INSERT ABOVE HERE
signed main(){
  Int n;
  cin>>n;
  using P = pair<Int, Int>;
  vector< vector< P > > G(n);
  for(Int i=1;i<n;i++){
    Int u,v,w;
    cin>>u>>v>>w;
    u--;v--;
    G[u].emplace_back(v,w);
    G[v].emplace_back(u,w);
  }

  Int ans=0;
  vector<Int> sz(n,0);
  MFP([&](auto dfs,Int v,Int p)->void{
        sz[v]=1;
        for(auto e:G[v]){
          Int u,c;
          tie(u,c)=e;
          if(u==p) continue;
          dfs(u,v);
          sz[v]+=sz[u];
          ans+=(n-sz[u])*sz[u]*c*2;
        }
      })(0,-1);
  cout<<ans<<endl;
  return 0;
}