#include using namespace std; typedef long long ll; typedef pair l_l; typedef pair i_i; template inline bool chmax(T &a, T b) { if(a < b) { a = b; return true; } return false; } template inline bool chmin(T &a, T b) { if(a > b) { a = b; return true; } return false; } const long double EPS = 1e-10; const long long INF = 1e18; const long double PI = acos(-1.0L); //const ll mod = 1000000007; ll N, K; vector paths[200100]; vector Del; ll SIZE[200100]; void print(set st) { cerr << "{"; for(auto val : st) { cerr << val << " "; } cerr << "}"; } void dfs(int from, int now) { SIZE[now] = 1; for(auto tmp : paths[now]) { int to = tmp.first; if(to == from) continue; if(Del[to]) continue; dfs(now, to); SIZE[now] += SIZE[to]; } } int centroid(int now, int sz) { for(auto tmp : paths[now]) { int to = tmp.first; if(Del[to]) continue; if(SIZE[to] > SIZE[now]) continue; if(SIZE[to] * 2 >= sz) return centroid(to, sz); } return now; } ll ans = 0; void g(int now, int from, map, ll> &mp, set st) { /* cerr << from << " -> " << now << " "; print(st); cerr << endl; */ mp[st]++; for(auto tmp : paths[now]) { int to = tmp.first; if(Del[to]) continue; if(to == from) continue; set st2 = st; st2.insert(tmp.second); if(st2.size() >= 3) continue; g(to, now, mp, st2); } } void f(int now) { dfs(-1, now); if(SIZE[now] == 1) return; int c = centroid(now, SIZE[now]); vector, ll>> V; map, ll> Total; for(auto tmp : paths[c]) { int to = tmp.first; if(Del[to]) continue; V.push_back({}); g(to, c, V.back(), {tmp.second}); for(auto tmp : V.back()) { Total[tmp.first] += tmp.second; } } ll OneNum = 0; ll OneAns = 0; for(auto tmp : Total) { /* print(tmp.first); cerr << " " << tmp.second << endl; */ if(tmp.first.size() == 1) { OneNum += tmp.second; OneAns -= tmp.second * tmp.second; } else { ans += tmp.second * (tmp.second + 1); for(auto tmp2 : tmp.first) { set st = {tmp2}; auto itr = Total.lower_bound(st); if((*itr).first == st) { ans += (*itr).second * tmp.second; } } } //cerr << ans << endl; } //cerr << "end: Total" << endl; for(auto now : V) { for(auto tmp : now) { if(tmp.first.size() == 1) { } else { ans -= tmp.second * tmp.second; for(auto tmp2 : tmp.first) { set st = {tmp2}; auto itr = now.lower_bound(st); if((*itr).first == st) { ans -= (*itr).second * tmp.second; } } } } //cerr << ans << endl; } OneAns += OneNum * OneNum; ans += OneAns / 2; Del[c] = true; //cerr << "-------------" << c << " " << ans << "--------" << endl; for(auto tmp : paths[c]) { int to = tmp.first; if(Del[to]) continue; f(to); } } int main() { //cout.precision(10); cin.tie(0); ios::sync_with_stdio(false); cin >> N >> K; Del.resize(N); for(int i = 0; i < N - 1; i++) { ll u, v, c; cin >> u >> v >> c; u--; v--; paths[u].push_back({v, c}); paths[v].push_back({u, c}); } f(0); cout << ans << endl; return 0; }