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>
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
struct Edge {
  int to, next, w;
} edge[maxn * 2];
struct DIS {
  long long key;
  int dep;
} dis[maxn];
bool ok[maxn], vis[maxn];
int head[maxn], size[maxn], maxv[maxn], prime[maxn];
int tot, NumOfPrime, id;
long long f[maxn];
long long fm, mod;
map<long long, long long> mp;
long long qpow(long long x, long long y) {
  long long tmp;
  if (!y) return 1;
  if (y == 1) return x % mod;
  tmp = qpow(x, y / 2);
  if (y & 1)
    return tmp * tmp % mod * x % mod;
  else
    return tmp * tmp % mod;
}
void init() {
  memset(vis, 0, sizeof(vis));
  memset(head, -1, sizeof(head));
  tot = 0;
}
void addedge(int u, int v, int w) {
  edge[tot].to = v;
  edge[tot].w = w;
  edge[tot].next = head[u];
  head[u] = tot++;
}
void prepare(int n) {
  f[0] = 1 % mod;
  f[1] = qpow(10, fm - 1);
  for (int i = 2; i <= n; i++) f[i] = f[i - 1] * f[1] % mod;
}
void getPrime() {
  NumOfPrime = 0;
  for (int i = 2; i < maxn; i++) {
    if (!ok[i]) {
      prime[NumOfPrime++] = i;
      for (int j = i + i; j < maxn; j += i) ok[j] = 1;
    }
  }
}
long long Eluer(long long m) {
  long long x = m;
  long long ans = m;
  for (int i = 0; i < NumOfPrime; i++) {
    if (x % prime[i] == 0) {
      while (x % prime[i] == 0) x /= prime[i];
      ans /= prime[i];
      ans *= (prime[i] - 1);
    }
  }
  if (x != 1) {
    ans /= x;
    ans *= (x - 1);
  }
  return ans;
}
void dfsSize(int u, int parent) {
  size[u] = 1;
  maxv[u] = 0;
  for (int i = head[u]; i != -1; i = edge[i].next) {
    int v = edge[i].to;
    if (vis[v] || v == parent) continue;
    dfsSize(v, u);
    size[u] += size[v];
    maxv[u] = max(maxv[u], size[v]);
  }
}
void dfsRoot(int u, int parent, int n, int &root, int &Max) {
  maxv[u] = max(maxv[u], n - size[u]);
  if (maxv[u] < Max) {
    Max = maxv[u];
    root = u;
  }
  for (int i = head[u]; i != -1; i = edge[i].next) {
    int v = edge[i].to;
    if (vis[v] || v == parent) continue;
    dfsRoot(v, u, n, root, Max);
  }
}
void dfsDis(int u, int parent, long long a, long long b, long long d, int dep) {
  mp[b]++;
  dis[id].dep = dep;
  dis[id].key = a;
  id++;
  for (int i = head[u]; i != -1; i = edge[i].next) {
    int v = edge[i].to, w;
    if (vis[v] || v == parent) continue;
    w = edge[i].w;
    dfsDis(v, u, (a * 10 + w) % mod, (w * d + b) % mod, d * 10 % mod, dep + 1);
  }
}
void dfsClear(int u, int parent, long long b, long long d) {
  mp.erase(b);
  for (int i = head[u]; i != -1; i = edge[i].next) {
    int v = edge[i].to, w;
    if (vis[v] || v == parent) continue;
    w = edge[i].w;
    dfsClear(v, u, (w * d + b) % mod, d * 10 % mod);
  }
}
long long calc(int u, long long a, long long b, long long d, int dep) {
  long long ret = 0;
  id = 0;
  dfsDis(u, 0, a, b, d, dep);
  for (int i = 0; i < id; i++) {
    long long x = (mod - dis[i].key) % mod;
    int y = dis[i].dep;
    ret += mp[f[y] * x % mod];
  }
  return ret;
}
long long dfs(int u) {
  long long ans = 0;
  int root = u, Max;
  dfsSize(u, 0);
  Max = size[u];
  dfsRoot(u, 0, Max, root, Max);
  vis[root] = 1;
  ans += calc(root, 0, 0, 1, 0) - 1;
  dfsClear(root, 0, 0, 1);
  for (int i = head[root]; i != -1; i = edge[i].next) {
    int v = edge[i].to, w;
    if (vis[v]) continue;
    w = edge[i].w;
    ans -= calc(v, w, w, 10, 1);
    dfsClear(v, 0, w, 10);
  }
  for (int i = head[root]; i != -1; i = edge[i].next) {
    int v = edge[i].to;
    if (vis[v]) continue;
    ans += dfs(v);
  }
  return ans;
}
int main() {
  int n;
  getPrime();
  while (scanf("%d%lld", &n, &mod) != EOF) {
    long long ans;
    init();
    fm = Eluer(mod);
    prepare(n);
    for (int i = 0; i < n - 1; i++) {
      int x, y, z;
      scanf("%d%d%d", &x, &y, &z);
      x++;
      y++;
      z %= mod;
      addedge(x, y, z);
      addedge(y, x, z);
    }
    ans = dfs(1);
    printf("%lld\n", ans);
  }
  return 0;
}
