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;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int lcm(int a, int b) { return a * (b / gcd(a, b)); }
long long int pow1(long long int a, long long int b, long long int mod) {
  long long int ans = 1;
  while (b > 0) {
    if (b & 1) ans = (ans * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return ans;
}
int mina(int arr[], int n) {
  int x = arr[0], i, pos = 0;
  for (i = 1; i < n; i++) {
    if (arr[i] < x) {
      x = arr[i];
      pos = i;
    }
  }
  return x;
}
int maxa(int arr[], int n) {
  int x = arr[0], i, pos = 0;
  for (i = 1; i < n; i++) {
    if (arr[i] > x) {
      x = arr[i];
      pos = i;
    }
  }
  return x;
}
vector<vector<pair<int, int> > > e, v;
vector<int> complist;
int w[100005], child[100005], parts;
long long int up[100005], down[100005], h[100005], mod, phi, treesize;
int dppart[100005];
bool vis[100005];
long long int inv(long long int x) { return pow1(x, phi - 1, mod); }
long long int mult(long long int a, long long int b) { return (a * b) % mod; }
long long int add(long long int a, long long int b) {
  return (a + b + mod) % mod;
}
bool prime[110001];
vector<int> p;
void sieve(int n) {
  long long int i, j;
  prime[1] = false;
  for (i = 4; i <= n; i = i + 2) prime[i] = false;
  for (i = 3; i * i <= n; i = i + 2) {
    if (prime[i] == true) {
      for (j = i * 2; j <= n; j = j + i) {
        prime[j] = false;
      }
    }
  }
  for (i = 2; i < n; i++)
    if (prime[i] == true) p.push_back(i);
}
long long int eulerphi(long long int n) {
  long long int ans = 1, num = n;
  long long int i = 0, val;
  val = p[i];
  while (val * val <= n) {
    if (n % val == 0) {
      num /= val;
      ans *= (val - 1);
    }
    while (n % val == 0) n /= val;
    val = p[++i];
  }
  if (n > 1) {
    num /= n;
    ans *= (n - 1);
  }
  n = 1;
  ans *= num;
  return ans;
}
void dfs(int u, int par) {
  if (par == -1) complist.clear();
  complist.push_back(u);
  child[u] = 1;
  for (vector<pair<int, int> >::iterator itr = v[u].begin(); itr != v[u].end();
       itr++) {
    int v1 = (*itr).second;
    if (v1 != par && !vis[v1]) {
      dfs(v1, u);
      child[u] += child[v1];
    }
  }
  if (par == -1) treesize = child[u];
}
int findroot(int u, int par) {
  for (vector<pair<int, int> >::iterator itr = v[u].begin(); itr != v[u].end();
       itr++) {
    int v1 = (*itr).second;
    if (v1 != par && !vis[v1]) {
      if (child[v1] * 2 > treesize) return findroot(v1, u);
    }
  }
  return u;
}
void getval(int u, int par, int root) {
  if (par == root) {
    dppart[u] = parts;
    parts++;
  } else if (par != -1)
    dppart[u] = dppart[par];
  for (vector<pair<int, int> >::iterator itr = v[u].begin(); itr != v[u].end();
       itr++) {
    int ind = (*itr).first, v1 = (*itr).second;
    if (v1 != par && !vis[v1]) {
      down[v1] = add(mult(down[u], 10), w[ind]);
      up[v1] = add(up[u], mult(pow1(10, h[u], mod), w[ind]));
      h[v1] = h[u] + 1;
      getval(v1, u, root);
    }
  }
}
long long int solve(int cent) {
  int i;
  for (i = 0; i < complist.size(); i++)
    up[complist[i]] = down[complist[i]] = h[complist[i]] = 0;
  parts = 0;
  getval(cent, -1, cent);
  parts--;
  dppart[cent] = -1;
  map<long long int, long long int> total;
  vector<map<long long int, long long int> > vec;
  vec.resize(parts + 1);
  total[0]++;
  for (i = 0; i < complist.size(); i++) {
    int u = complist[i];
    if (u == cent) continue;
    total[up[u]]++;
    vec[dppart[u]][up[u]]++;
  }
  long long int ans = 0;
  for (i = 0; i < complist.size(); i++) {
    int u = complist[i];
    long long int height = h[u], part = dppart[u];
    if (u == cent)
      ans += (total[0] - 1);
    else {
      long long int val = ((-down[u]) % mod + mod) % mod;
      val = mult(val, inv(pow1(10, height, mod)));
      ans += (total[val] - vec[part][val]);
    }
  }
  return ans;
}
long long int compsolve(int u) {
  int i;
  dfs(u, -1);
  int cent = findroot(u, -1);
  vis[cent] = true;
  long long int ans = solve(cent);
  for (vector<pair<int, int> >::iterator itr = v[cent].begin();
       itr != v[cent].end(); itr++) {
    int ind = (*itr).first, v1 = (*itr).second;
    if (!vis[v1]) ans += compsolve(v1);
  }
  return ans;
}
int main() {
  memset(prime, true, sizeof(prime));
  sieve(100005);
  int i, j, x, y, w1;
  long long int n;
  scanf("%lld", &n);
  scanf("%lld", &mod);
  if (mod == 1) {
    cout << n * (n - 1) << endl;
    return 0;
  }
  phi = eulerphi(mod);
  v.resize(n + 3);
  e.resize(n + 3);
  for (i = 0; i < n - 1; i++) {
    scanf("%d", &x);
    scanf("%d", &y);
    scanf("%d", &w1);
    v[x].push_back(make_pair(i, y));
    v[y].push_back(make_pair(i, x));
    w[i] = w1;
  }
  printf("%lld\n", compsolve(0));
  return 0;
}
