ZS the Coder has a large tree. It can be represented as an undirected connected graph of n vertices numbered from 0 to n - 1 and n - 1 edges between them. There is a single nonzero digit written on each edge.

One day, ZS the Coder was bored and decided to investigate some properties of the tree. He chose a positive integer M, which is coprime to 10, i.e. <image>.

ZS consider an ordered pair of distinct vertices (u, v) interesting when if he would follow the shortest path from vertex u to vertex v and write down all the digits he encounters on his path in the same order, he will get a decimal representaion of an integer divisible by M.

Formally, ZS consider an ordered pair of distinct vertices (u, v) interesting if the following states true:

  * Let a1 = u, a2, ..., ak = v be the sequence of vertices on the shortest path from u to v in the order of encountering them; 
  * Let di (1 ≤ i < k) be the digit written on the edge between vertices ai and ai + 1; 
  * The integer <image> is divisible by M. 



Help ZS the Coder find the number of interesting pairs!

Input

The first line of the input contains two integers, n and M (2 ≤ n ≤ 100 000, 1 ≤ M ≤ 109, <image>) — the number of vertices and the number ZS has chosen respectively.

The next n - 1 lines contain three integers each. i-th of them contains ui, vi and wi, denoting an edge between vertices ui and vi with digit wi written on it (0 ≤ ui, vi < n, 1 ≤ wi ≤ 9).

Output

Print a single integer — the number of interesting (by ZS the Coder's consideration) pairs.

Examples

Input

6 7
0 1 2
4 2 4
2 0 1
3 0 9
2 5 7


Output

7


Input

5 11
1 2 3
2 0 3
3 0 3
4 3 3


Output

8

Note

In the first sample case, the interesting pairs are (0, 4), (1, 2), (1, 5), (3, 2), (2, 5), (5, 2), (3, 5). The numbers that are formed by these pairs are 14, 21, 217, 91, 7, 7, 917 respectively, which are all multiples of 7. Note that (2, 5) and (5, 2) are considered different. 

<image>

In the second sample case, the interesting pairs are (4, 0), (0, 4), (3, 2), (2, 3), (0, 1), (1, 0), (4, 1), (1, 4), and 6 of these pairs give the number 33 while 2 of them give the number 3333, which are all multiples of 11.

<image>
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.IOException;
import java.util.HashMap;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Map;
import java.math.BigInteger;
import java.io.BufferedReader;
import java.util.Queue;
import java.util.ArrayDeque;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		FastScanner in = new FastScanner(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskC solver = new TaskC();
		solver.solve(1, in, out);
		out.close();
	}

	static class TaskC {
		long ans;
		int n;
		int MOD;
		int inv10;
		int[] pow10;
		int[] powInv10;
		int[] firstEdge;
		int[] nextEdge;
		int[] edgeDst;
		int[] edgeDigit;
		int numEdges;
		boolean[] usedInDecomposition;
		int[] subtreeSize;
		int[] generation;
		int lastGeneration;
		int[] parent;
		int[] centroidDecompositionParent;
		boolean[] inCurrentComp;
		int[] depth;
		int[] forwardVal;
		int[] backwardVal;

		public void solve(int testNumber, FastScanner in, PrintWriter out) {
			n = in.nextInt();
			MOD = in.nextInt();
			inv10 = BigInteger.valueOf(10 % MOD).modInverse(BigInteger.valueOf(MOD)).intValue();
			pow10 = new int[n + 2];
			pow10[0] = 1 % MOD;
			pow10[1] = 10 % MOD;
			powInv10 = new int[n + 2];
			powInv10[0] = 1 % MOD;
			powInv10[1] = inv10;
			for (int i = 2; i < pow10.length; i++) {
				pow10[i] = (int) (10L * pow10[i - 1] % MOD);
				powInv10[i] = (int) ((long) inv10 * powInv10[i - 1] % MOD);
			}

			readTree(in, n);
			buildCentroidDecomposition();
			out.println(ans);
		}

		private void readTree(FastScanner in, int n) {
			numEdges = 0;
			firstEdge = new int[n];
			Arrays.fill(firstEdge, -1);
			nextEdge = new int[2 * (n - 1)];
			edgeDst = new int[2 * (n - 1)];
			edgeDigit = new int[2 * (n - 1)];
			for (int i = 0; i < n - 1; i++) {
				int a = in.nextInt();
				int b = in.nextInt();
				int c = in.nextInt();
				addEdge(a, b, c);
				addEdge(b, a, c);
			}
		}

		private void addEdge(int a, int b, int c) {
			int e = numEdges++;
			nextEdge[e] = firstEdge[a];
			firstEdge[a] = e;
			edgeDst[e] = b;
			edgeDigit[e] = c;
		}

		private int buildCentroidDecomposition() {
			usedInDecomposition = new boolean[n];
			subtreeSize = new int[n];
			parent = new int[n];
			generation = new int[n];
			lastGeneration = 0;
			centroidDecompositionParent = new int[n];
			inCurrentComp = new boolean[n];
			depth = new int[n];
			forwardVal = new int[n];
			backwardVal = new int[n];
			int root = decompose(0);
			centroidDecompositionParent[root] = -1;
			return root;
		}

		private int decompose(int s) {
			List<Integer> comp = new ArrayList<>();
			parent[s] = -1;
			++lastGeneration;
			generation[s] = lastGeneration;
			Queue<Integer> q = new ArrayDeque<>();
			q.offer(s);
			while (!q.isEmpty()) {
				int v = q.poll();
				comp.add(v);
				subtreeSize[v] = 1;
				for (int e = firstEdge[v]; e >= 0; e = nextEdge[e]) {
					int u = edgeDst[e];
					if (usedInDecomposition[u]) {
						continue;
					}
					if (u == parent[v]) {
						continue;
					}
					if (generation[u] != lastGeneration) {
						parent[u] = v;
						q.offer(u);
					}
				}
			}
			for (int i = comp.size() - 1; i > 0; i--) {
				int v = comp.get(i);
				subtreeSize[parent[v]] += subtreeSize[v];
			}
			int c = -1;
			for (int v : comp) {
				boolean good = true;
				for (int e = firstEdge[v]; e >= 0; e = nextEdge[e]) {
					int u = edgeDst[e];
					if (u == parent[v]) {
						continue;
					}
					if (usedInDecomposition[u]) {
						continue;
					}
					if (2 * subtreeSize[u] > comp.size()) {
						good = false;
					}
				}
				if (2 * (comp.size() - subtreeSize[v]) > comp.size()) {
					good = false;
				}
				if (good) {
					c = v;
					break;
				}
			}
			if (c < 0) {
				throw new AssertionError();
			}
			usedInDecomposition[c] = true;
			// c is a centroid of the subtree containing vertex s
			for (int e = firstEdge[c]; e >= 0; e = nextEdge[e]) {
				int u = edgeDst[e];
				if (usedInDecomposition[u]) {
					continue;
				}
				centroidDecompositionParent[decompose(u)] = c;
			}

			for (int v : comp) {
				inCurrentComp[v] = true;
			}
			processComp(c);
			for (int v : comp) {
				inCurrentComp[v] = false;
			}
			return c;
		}

		private void processComp(int root) {
			Map<Integer, Integer> remsForward = new HashMap<>();
			Map<Integer, Integer> remsBackward = new HashMap<>();
			addToMap(remsForward, 0, 1);
			addToMap(remsBackward, 0, 1);
			for (int e = firstEdge[root]; e >= 0; e = nextEdge[e]) {
				int child = edgeDst[e];
				if (!inCurrentComp[child]) {
					continue;
				}
				int d = edgeDigit[e] % MOD;
				List<Integer> subtree = new ArrayList<>();
				collect(child, root, 1, d, d, subtree);
				for (int v : subtree) {
					int b = backwardVal[v];
					int f = (int) (-forwardVal[v] * (long) powInv10[depth[v]] % MOD);
					if (f < 0) {
						f += MOD;
					}

					// all paths that start at v
					if (remsForward.containsKey(b)) {
						ans += remsForward.get(b);
					}
					// all paths that end at v
					if (remsBackward.containsKey(f)) {
						ans += remsBackward.get(f);
					}
				}
				for (int v : subtree) {
					int b = backwardVal[v];
					int f = (int) (-forwardVal[v] * (long) powInv10[depth[v]] % MOD);
					if (f < 0) {
						f += MOD;
					}
					addToMap(remsForward, f, 1);
					addToMap(remsBackward, b, 1);
				}
			}
		}

		private void collect(int v, int p, int curDepth, int curForward, int curBackward, List<Integer> subtree) {
			depth[v] = curDepth;
			forwardVal[v] = curForward;
			backwardVal[v] = curBackward;
			subtree.add(v);
			for (int e = firstEdge[v]; e >= 0; e = nextEdge[e]) {
				int u = edgeDst[e];
				if (!inCurrentComp[u]) {
					continue;
				}
				if (u == p) {
					continue;
				}
				int d = edgeDigit[e];
				int nxtForward = (int) ((10L * curForward + d) % MOD);
				int nxtBackward = (int) ((curBackward + (long) pow10[curDepth] * d) % MOD);
				collect(u, v, curDepth + 1, nxtForward, nxtBackward, subtree);
			}
		}

		private void addToMap(Map<Integer, Integer> map, int key, int add) {
			if (!map.containsKey(key)) {
				map.put(key, add);
			} else {
				int v = map.get(key);
				map.put(key, v + add);
			}
		}

	}

	static class FastScanner {
		private BufferedReader in;
		private StringTokenizer st;

		public FastScanner(InputStream stream) {
			in = new BufferedReader(new InputStreamReader(stream));
		}

		public String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					String rl = in.readLine();
					if (rl == null) {
						return null;
					}
					st = new StringTokenizer(rl);
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return st.nextToken();
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}

	}
}

