import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } int N, K; int[] A, B, C; int[][] G; long ans; void solveCentroidDecomp() { auto sz = new int[N]; auto del = new bool[N]; void dfsSz(int u, int p) { sz[u] = 1; foreach (i; G[u]) { const v = A[i] ^ B[i] ^ u; if (v != p) { dfsSz(v, u); sz[u] += sz[v]; } } } dfsSz(0, -1); // r: centroid void solveSubtree(int r) { debug { writefln("solveSubtree r = %s (del = %s)", r, del); } long[long] freq1Sum, freq2Sum; long[long] freq1, freq2; void dfs(int u, int p, int[] cs) { if (cs.length == 1) { ++freq1Sum[cs[0]]; ++freq1[cs[0]]; } else { const key = cs[0] + cs[1] * cast(long)(K); ++freq2Sum[key]; ++freq2[key]; } foreach (i; G[u]) { const v = A[i] ^ B[i] ^ u; if (!del[v] && v != p) { auto ccs = cs ~ C[i]; ccs = ccs.sort.uniq.array; if (ccs.length <= 2) { dfs(v, u, ccs); } } } } foreach (i; G[r]) { const v = A[i] ^ B[i] ^ r; if (!del[v]) { freq1.clear; freq2.clear; dfs(v, r, [C[i]]); const num1 = freq1.values.sum; foreach (c, val; freq1) { // 1-1 ans -= val * (num1 - val); } // 2-1, 2-2 foreach (key, val; freq2) { const c0 = key % K; const c1 = key / K; // 2-1 ans -= 2 * val * (((c0 in freq1) ? freq1[c0] : 0) + ((c1 in freq1) ? freq1[c1] : 0)); // 2-2 ans -= val * val; } } } const num1Sum = freq1Sum.values.sum; foreach (c, val; freq1Sum) { // 1-1 ans += val * (num1Sum - val); } // 2-1, 2-2 foreach (key, val; freq2Sum) { const c0 = key % K; const c1 = key / K; // 2-0 ans += 2 * val; // 2-1 ans += 2 * val * (((c0 in freq1Sum) ? freq1Sum[c0] : 0) + ((c1 in freq1Sum) ? freq1Sum[c1] : 0)); // 2-2 ans += val * val; } } void solveRec(int u) { for (; ; ) { int vm = -1; foreach (i; G[u]) { const v = A[i] ^ B[i] ^ u; if (!del[v]) { if (vm == -1 || sz[vm] < sz[v]) { vm = v; } } } if (vm == -1 || sz[u] >= 2 * sz[vm]) { solveSubtree(u); del[u] = true; foreach (i; G[u]) { const v = A[i] ^ B[i] ^ u; if (!del[v]) { solveRec(v); } } break; } else { sz[u] -= sz[vm]; sz[vm] += sz[u]; u = vm; } } } solveRec(0); } void main() { try { for (; ; ) { N = readInt(); K = readInt(); A = new int[N - 1]; B = new int[N - 1]; C = new int[N - 1]; foreach (i; 0 .. N - 1) { A[i] = readInt() - 1; B[i] = readInt() - 1; C[i] = readInt() - 1; } G = new int[][N]; foreach (i; 0 .. N - 1) { G[A[i]] ~= i; G[B[i]] ~= i; } ans = 0; solveCentroidDecomp(); ans /= 2; writeln(ans); } } catch (EOFException e) { } }