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.util.*;
import java.io.*;

public class E716 {
	static int m;
	static ArrayList<int[]>[] adjList;
	static boolean[] vis;
	static int[] size, depth;
	static long[] suf, pre, tenInv;

	public static long solve(int u) {
		getSizes(u, -1);
		int cent = getCentroid(u, -1, size[u]);
		getPre(cent, -1, 1, 0);
		getSuf(cent, -1, 0);
		ArrayList<ArrayList<Integer>> subtrees = new ArrayList<ArrayList<Integer>>();

		HashMap<Long, Integer> precnt = new HashMap<Long, Integer>();
		precnt.put(0l, 1);
		for (int[] nxt : adjList[cent]) {
			if (!vis[nxt[0]]) {
				ArrayList<Integer> nodes = new ArrayList<Integer>();
				getSubtree(nxt[0], cent, nodes);
				subtrees.add(nodes);
				for (int x : nodes) {
					precnt.put(pre[x], precnt.getOrDefault(pre[x], 0) + 1);
				}
			}
		}
//		System.err.println(cent + " " + Arrays.toString(pre));
		ArrayList<Integer> c = new ArrayList<Integer>();
		c.add(cent);
		subtrees.add(c);
		long ans = 0;
		for (ArrayList<Integer> subtree : subtrees) {
			HashMap<Long, Integer> thisprecnt = new HashMap<Long, Integer>();
			for (int node : subtree) {
				thisprecnt.put(pre[node], thisprecnt.getOrDefault(pre[node], 0) + 1);
			}
			for (int node : subtree) {
				long suffix = suf[node];
//				System.err.println("Suffix : " + suffix);
				long ten = tenInv[depth[node]];
				long req = (((ten * -suffix) % m) + m) % m;
				ans += precnt.getOrDefault(req, 0);
				ans -= thisprecnt.getOrDefault(req, 0);
//				System.err.println(cent + " " + ans + " " + node + " " + req);
//				System.err.println(precnt);
			}
		}

//		System.err.println(Arrays.toString(size));
		vis[cent] = true;
		for (int[] nxt : adjList[cent]) {
			if (!vis[nxt[0]])
				ans += solve(nxt[0]);
		}
		return ans;

	}

	public static void getSubtree(int u, int p, ArrayList<Integer> al) {
		al.add(u);
		for (int[] nxt : adjList[u]) {
			if (nxt[0] != p && !vis[nxt[0]]) {
				getSubtree(nxt[0], u, al);
			}
		}
	}

	public static void getPre(int u, int p, long mult, int d) {
		pre[u] = ((p == -1 ? 0 : pre[p]) + mult * d) % m;
		depth[u] = (p == -1 ? -1 : depth[p]) + 1;
		long newmult = (p == -1) ? mult : (mult * 10) % m;
		for (int[] nxt : adjList[u]) {
			if (nxt[0] != p && !vis[nxt[0]]) {
				getPre(nxt[0], u, newmult, nxt[1]);
			}
		}
	}

	public static void getSuf(int u, int p, long path) {
		suf[u] = path;
		for (int[] nxt : adjList[u]) {
			if (nxt[0] != p && !vis[nxt[0]]) {
				getSuf(nxt[0], u, (path * 10 + nxt[1]) % m);
			}
		}
	}

	public static void getSizes(int u, int p) {
		size[u] = 1;
		for (int[] nxt : adjList[u]) {
			if (nxt[0] != p && !vis[nxt[0]]) {
				getSizes(nxt[0], u);
				size[u] += size[nxt[0]];
			}
		}
	}

	public static int getCentroid(int u, int p, int compSize) {
		for (int[] nxt : adjList[u]) {
			if (nxt[0] != p && !vis[nxt[0]] && size[nxt[0]] > compSize / 2) {
				return getCentroid(nxt[0], u, compSize);
			}
		}
		return u;
	}

	public static long modPow(long a, long e) {
		if (e == 0) {
			return 1;
		}
		if (e % 2 == 1) {
			return (a * modPow(a, e - 1)) % m;
		}
		long pre = modPow(a, e / 2);
		return (pre * pre) % m;
	}

	static int phi(int n) {
		int result = n;
		for (int i = 2; i * i <= n; i++) {
			if (n % i == 0) {
				while (n % i == 0)
					n /= i;
				result -= result / i;
			}
		}
		if (n > 1)
			result -= result / n;
		return result;
	}

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		PrintWriter pw = new PrintWriter(System.out);
		int n = sc.nextInt();
		m = sc.nextInt();
		adjList = new ArrayList[n];
		for (int i = 0; i < n; i++) {
			adjList[i] = new ArrayList<int[]>();
		}
		for (int i = 0; i < n - 1; i++) {
			int u = sc.nextInt();
			int v = sc.nextInt();
			int w = sc.nextInt();
			adjList[u].add(new int[] { v, w });
			adjList[v].add(new int[] { u, w });
		}
		vis = new boolean[n];

		size = new int[n];
		depth = new int[n];
		suf = new long[n];
		pre = new long[n];
		tenInv = new long[n];
		tenInv[0] = 1;
		long tenmodInv = modPow(10, phi(m) - 1);
		for (int i = 1; i < n; i++) {
			tenInv[i] = (tenInv[i - 1] * tenmodInv) % m;
		}

		pw.println(solve(0));
		pw.close();
	}

	static class Scanner {
		BufferedReader br;
		StringTokenizer st;

		public Scanner(InputStream s) {
			br = new BufferedReader(new InputStreamReader(s));
		}

		public Scanner(FileReader f) {
			br = new BufferedReader(f);
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

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

		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public int[] nextIntArr(int n) throws IOException {
			int[] arr = new int[n];
			for (int i = 0; i < n; i++) {
				arr[i] = Integer.parseInt(next());
			}
			return arr;
		}

	}

}
