#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
typedef long long lli;

class UnionFind {
private:
  int n;
  int *par, *rank, *num;

public:
  UnionFind(int _n) : n(_n) {
    par = new int[n];
    rank = new int[n];
    num = new int[n];
    for(int i = 0; i < n; ++i) {
      par[i] = i;
      rank[i] = 0;
      num[i] = 1;
    }
  }

  ~UnionFind() {
    delete [] par;
    delete [] rank;
    delete [] num;
  }

  int find(int x) {
    return par[x] == x ? x : par[x] = find(par[x]);
  }

  int getNum(int x) {
    return num[find(x)];
  }

  bool same(int x, int y) {
    return find(x) == find(y);
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if(rank[x] < rank[y]) {
      par[x] = y;
      num[y] += num[x];
    } else {
      par[y] = x;
      num[x] += num[y];
      if(rank[x] == rank[y]) ++rank[x];
    }
  }
};

int main() {
  int N;
  while(cin >> N) {
    vector<pair<int, pair<int,int> > > v;
    for(int i = 0; i < N-1; ++i) {
      int a, b, c;
      cin >> a >> b >> c;
      v.push_back(make_pair(c, make_pair(a,b)));
    }
    sort(v.begin(), v.end());
    UnionFind uf(N);
    lli res = 0;
    for(int i = 0; i < v.size(); ++i) {
      int a = v[i].second.first;
      int b = v[i].second.second;
      int c = v[i].first;
      res += (lli)uf.getNum(a) * (lli)uf.getNum(b) * (lli)c;
      uf.unite(a,b);
    }
    cout << res << endl;
  }
  return 0;
}