#include using namespace std; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, k; cin >> n >> k; vector> g(n), col(n); for (int _ = n - 1; _--; ) { int u, v, c; cin >> u >> v >> c; --u, --v; g[u].push_back(v), col[u].push_back(c); g[v].push_back(u), col[v].push_back(c); } vector be(n, true); vector sz(n), par(n); auto centroids = [&](int r, int cur_n) { vector res; auto dfs = [&](auto&& self, int v, int p) -> void { sz[v] = 1, par[v] = p; bool ok = true; for (int u : g[v]) { if (be[u] and u != p) { self(self, u, v); sz[v] += sz[u]; ok &= 2 * sz[u] <= cur_n; } } if (ok and 2 * sz[v] >= cur_n) { res.push_back(v); } }; dfs(dfs, r, -1); return res; }; vector c1; vector> c2; auto dfs = [&](auto&& self, int v, int p, const vector& cs) -> void { if (cs.size() == 1) { c1.push_back(cs[0]); } else { c2.emplace_back(cs[0], cs[1]); } for (int id = 0; id < (int)g[v].size(); ++id) { int u = g[v][id], c = col[v][id]; if (be[u] and u != p) { if (cs.size() == 1) { if (c < cs[0]) { self(self, u, v, {c, cs[0]}); } else if (cs[0] < c) { self(self, u, v, {cs[0], c}); } else { self(self, u, v, cs); } } else if (c == cs[0] or c == cs[1]) { self(self, u, v, cs); } } } }; auto f11 = [&](int s, int t) { long long res = 0; for (int l = s, r = s; l < t; ++l) { while (r < t and c1[r] <= c1[l]) { ++r; } res += t - r; } return res; }; auto f12 = [&](int s1, int t1, int s2, int t2) { vector cs(2 * (t2 - s2)); for (int i = 0; i < (int)cs.size(); i += 2) { tie(cs[i], cs[i + 1]) = c2[s2 + i / 2]; } sort(begin(cs), end(cs)); long long res = 0; int l = s1, r = s1; for (int e : cs) { while (l < t1 and c1[l] < e) { ++l; } while (r < t1 and c1[r] <= e) { ++r; } res += r - l; } return res; }; auto f22 = [&](int s, int t) { long long res = 0; for (int l = s, r = s; l < t; ++l) { while (r < t and c2[r] <= c2[l]) { ++r; } res += r - l - 1; } return res; }; long long res = 0; auto rec = [&](auto&& self, int c, int cur_n) -> void { c = centroids(c, cur_n)[0]; be[c] = false; for (int v : g[c]) { if (be[v]) { self(self, v, par[v] == c ? sz[v] : cur_n - sz[c]); } } be[c] = true; c1.clear(), c2.clear(); for (int id = 0; id < (int)g[c].size(); ++id) { int v = g[c][id]; if (be[v]) { int n1 = c1.size(), n2 = c2.size(); dfs(dfs, v, c, {col[c][id]}); sort(begin(c1) + n1, end(c1)); sort(begin(c2) + n2, end(c2)); res -= f12(n1, c1.size(), n2, c2.size()); res -= f22(n2, c2.size()); } } sort(begin(c1), end(c1)); sort(begin(c2), end(c2)); int n1 = c1.size(), n2 = c2.size(); res += c2.size(); res += f11(0, n1); res += f12(0, n1, 0, n2); res += f22(0, n2); }; rec(rec, 0, n); cout << res << '\n'; }