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.io.PrintStream;
import java.util.Arrays;
import java.io.BufferedInputStream;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.HashMap;
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;
		BufferedInputStream in = new BufferedInputStream(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskE solver = new TaskE();
		solver.solve(1, in, out);
		out.close();
	}

	static class TaskE {
		int[][][] gw;
		int[][] g;
		int n;
		long m;
		int[] fromtRoot;
		int[] toRoot;
		long ans = 0;
		int[] qu;
		boolean[] visited;
		int[] p;
		int[] depth;

		long pow10(int n) {
			if (n == 0) return 1;
			long temp = pow10(n / 2);
			temp = (temp * temp) % m;
			if (n % 2 == 1) {
				temp *= 10l;
				temp %= m;
			}

			return temp;

		}

		void count(int x) {
			int lq = 0, fq = 0;
			HashMap<Integer, Integer> lastTo = new HashMap<>();
			for (int child : g[x])
				if (!visited[child]) {
					lq = fq = 0;
					qu[lq++] = child;
					while (fq < lq) {
						int front = qu[fq++];
						for (int u : g[front])
							if (!visited[u] && u != p[front]) {
								qu[lq++] = u;
							}
					}

					for (int i = 0; i < lq; ++i) {
						int u = qu[i];
						int B = (int) pow10(depth[u]);
						long A = (m - fromtRoot[u]) % m;
						int X = (int) ((MathUtils.modInverse(B, (int) m) * A) % m);
						Integer cnt = lastTo.get(X);
						if (cnt != null) {
							ans += cnt;
						}
						if (X < 0) throw new RuntimeException();
					}


					for (int i = 0; i < lq; ++i) {
						int u = qu[i];
						Integer cnt = lastTo.get(toRoot[u]);
						if (cnt == null) cnt = 1;
						else cnt++;
						lastTo.put(toRoot[u], cnt);
					}


				}
		}

		void calc(int x) {

			fromtRoot[x] = 0;
			toRoot[x] = 0;
			p[x] = -1;
			depth[x] = 0;


			int lq = 0, fq = 0;
			qu[lq++] = x;
			while (fq < lq) {
				int front = qu[fq++];
				for (int[] ed : gw[front]) {
					int u = ed[0];
					if (u != p[front] && !visited[u]) {
						p[u] = front;
						qu[lq++] = u;
						depth[u] = depth[front] + 1;


						fromtRoot[u] = (int) ((10l * fromtRoot[front] + ed[1]) % m);
						toRoot[u] = (int) ((pow10(depth[u] - 1) * ed[1] + toRoot[front]) % m);
						if (fromtRoot[u] == 0) ans++;
						if (toRoot[u] == 0) ans++;
						if (fromtRoot[u] < 0) throw new RuntimeException();
						if (toRoot[u] < 0) throw new RuntimeException();
//					System.err.println(u + " " + fromtRoot[u] + " " + toRoot[u]);
					}
				}
			}

			count(x);
			MathUtils.reverseArray(g[x]);
			count(x);


		}

		public void solve(int testNumber, BufferedInputStream _in, PrintWriter out) {

			InputReader in = new InputReader(_in);
			long startTime = System.currentTimeMillis();
			n = in.nextInt();
			m = in.nextInt();
			//m = 51;

			int[] from = new int[n - 1];
			int[] to = new int[n - 1];
			int[] w = new int[n - 1];
			fromtRoot = new int[n];
			toRoot = new int[n];
			qu = new int[n];
			p = new int[n];
			visited = new boolean[n];
			depth = new int[n];
			for (int i = 0; i < n - 1; ++i) {
				from[i] = in.nextInt();
				to[i] = in.nextInt();
				w[i] = in.nextInt();
				//	w[i] = in.nextInt() % 9 + 1;
			}

			gw = Graphs.packWU(n, from, to, w);
			g = Graphs.packU(n, from, to);

			int[] ctpar = new int[n];
			Arrays.fill(ctpar, -1);

			Graphs._Centroid _centroid = new Graphs._Centroid();
			_centroid.g = g;
			_centroid.ctpar = ctpar;
			_centroid.des = new int[n];
			_centroid.ord = new int[n];
			_centroid.sed = new boolean[n];
			_centroid.par = new int[n];
			_centroid.cent = new int[n];
			//buildCentroidTree(g, 0, new boolean[n], new int[n], new int[n], new int[n], ctpar);
			_centroid.buildCentroidTree(0);

			for (int i = 0; i < n; ++i) {
				int x = _centroid.cent[i];
				if (visited[x]) throw new RuntimeException();
				calc(x);
				visited[x] = true;
			}
			out.println(ans);

			in.close();
			System.err.println("Time: " + (System.currentTimeMillis() - startTime) + " miliseconds");
			//out.println((MathUtils.modInverse(11, 13) * 6 ) % 13);
		}

	}

	static class MathUtils {
		static long inv(long a, long b) {
			return 1 < a ? b - inv(b % a, a) * b / a : 1;
		}

		public static int modInverse(int a, int m) {
//		long B = fecmaPow(a, m - 2, m);
//		if (B < 0) {
//			B += (Math.abs(B) / m + 1) * m;
//			B %= m;
//		}
//		return (int) B;

			return (int) inv(a, m);

		}

		public static void swap(int[] a, int i, int j) {
			int t = a[i];
			a[i] = a[j];
			a[j] = t;
		}

		public static void reverseArray(int[] a) {
			int size = a.length;
			for (int i = 0, mid = size >> 1, j = size - 1; i < mid; i++, j--) {
				swap(a, i, j);
			}
		}

	}

	static class InputReader {
		private InputStream stream;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;

		public InputReader(InputStream stream) {
			this.stream = stream;
		}

		public int read() {
			if (numChars == -1)
				throw new InputMismatchException();
			if (curChar >= numChars) {
				curChar = 0;
				try {
					numChars = stream.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (numChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		public int nextInt() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			int res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		public static boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		public void close() {

		}

	}

	static class Graphs {
		public static int[][] packU(int n, int[] from, int[] to) {
			int[][] g = new int[n][];
			int[] p = new int[n];
			for (int f : from)
				p[f]++;
			for (int t : to)
				p[t]++;
			for (int i = 0; i < n; i++)
				g[i] = new int[p[i]];
			for (int i = 0; i < from.length; i++) {
				g[from[i]][--p[from[i]]] = to[i];
				g[to[i]][--p[to[i]]] = from[i];
			}
			return g;
		}

		public static int[][][] packWU(int n, int[] from, int[] to, int[] w) {
			int[][][] g = new int[n][][];
			int[] p = new int[n];
			for (int f : from)
				p[f]++;
			for (int t : to)
				p[t]++;
			for (int i = 0; i < n; i++)
				g[i] = new int[p[i]][2];
			for (int i = 0; i < from.length; i++) {
				--p[from[i]];
				g[from[i]][p[from[i]]][0] = to[i];
				g[from[i]][p[from[i]]][1] = w[i];
				--p[to[i]];
				g[to[i]][p[to[i]]][0] = from[i];
				g[to[i]][p[to[i]]][1] = w[i];
			}
			return g;
		}

		static class _Centroid {
			int[][] g;
			boolean[] sed;
			int[] par;
			int[] ord;
			int[] des;
			int[] ctpar;
			int[] cent;
			int ncent = 0;

			int buildCentroidTree(int root) {
				// parent and level-order
				ord[0] = root;
				par[root] = -1;
				int r = 1;
				for (int p = 0; p < r; p++) {
					int cur = ord[p];
					for (int nex : g[cur]) {
						if (par[cur] != nex && !sed[nex]) {
							ord[r++] = nex;
							par[nex] = cur;
						}
					}
				}
				// if(r == 1)return;

				// DP and find a separator
				int sep = -1; // always exists
				outer:
				for (int i = r - 1; i >= 0; i--) {
					int cur = ord[i];
					des[cur] = 1;
					for (int e : g[cur]) {
						if (par[cur] != e && !sed[e]) des[cur] += des[e];
					}
					if (r - des[cur] <= r / 2) {
						for (int e : g[cur]) {
							if (par[cur] != e && !sed[e] && des[e] >= r / 2 + 1) continue outer;
						}
						sep = cur;
						break;
					}
				}
				cent[ncent++] = sep;
				sed[sep] = true;
				for (int e : g[sep]) {
					if (!sed[e]) ctpar[buildCentroidTree(e)] = sep;
				}
				return sep;
			}

		}

	}
}

