#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 5;
const long long LGN = 20;
const long long MOD = 1e9 + 7;
long long n, m, ans;
vector<pair<long long, long long> > g[N];
vector<long long> cd[N];
vector<long long> sz(N), dead(N), par(N), level(N), i10(N), p10(N);
map<long long, long long> aU, aD, um, dm;
long long down[LGN][N], up[LGN][N];
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  a %= b;
  return gcd(b, a);
}
long long mod(long long val, long long md) {
  val %= md;
  if (val < 0) val += md;
  return val;
}
long long dfs(long long u, long long v = -1) {
  sz[u] = 1;
  for (auto it : g[u]) {
    long long i = it.first;
    if (i != v && !dead[i]) sz[u] += dfs(i, u);
  }
  return sz[u];
}
long long findCentroid(long long u, long long sn, long long p = -1) {
  for (auto it : g[u]) {
    long long i = it.first;
    if (i != p && !dead[i] && sz[i] > sn / 2) return findCentroid(i, sn, u);
  }
  return u;
}
void precompute(long long c, long long lvl, long long p, long long cnt,
                long long centroid) {
  for (auto it : g[c]) {
    long long i = it.first;
    if (i != p && !dead[i]) {
      down[lvl][i] = mod(down[lvl][c] + it.second * i10[cnt], m);
      up[lvl][i] = mod(up[lvl][c] + it.second * p10[cnt], m);
      um[up[lvl][i]]++;
      dm[down[lvl][i]]++;
      precompute(i, lvl, c, cnt + 1, centroid);
    }
  }
}
long long decompose(long long u, long long v) {
  long long centroid = findCentroid(u, dfs(u));
  dead[centroid] = 1;
  par[centroid] = v;
  if (v != -1) {
    level[centroid] = level[v] + 1;
    cd[centroid].push_back(v);
    cd[v].push_back(centroid);
  }
  long long lvl = level[centroid];
  aU.clear();
  aD.clear();
  aU[0]++;
  aD[0]++;
  for (auto it : g[centroid]) {
    long long i = it.first;
    if (!dead[i]) {
      down[lvl][i] = mod(it.second, m);
      up[lvl][i] = mod(it.second, m);
      um.clear();
      dm.clear();
      um[up[lvl][i]]++;
      dm[down[lvl][i]]++;
      precompute(i, lvl, centroid, 1, centroid);
      for (auto j : um) {
        long long need = mod(-10 * j.first, m);
        if (aD.find(need) != aD.end()) {
          ans += aD[need] * j.second;
        }
      }
      for (auto j : dm) {
        long long need = mod(-i10[1] * j.first, m);
        if (aU.find(need) != aU.end()) {
          ans += aU[need] * j.second;
        }
      }
      for (auto j : um) aU[mod(j.first, m)] += j.second;
      for (auto j : dm) aD[mod(j.first, m)] += j.second;
    }
  }
  for (auto it : g[centroid]) {
    long long i = it.first;
    if (!dead[i]) decompose(i, centroid);
  }
  return centroid;
}
long long expo(long long base, long long x, long long M = MOD) {
  long long res = 1;
  base = base % M;
  while (x > 0) {
    if (x & 1) res = (res * base) % M;
    x = x >> 1;
    base = (base * base) % M;
  }
  return res;
}
long long phi(long long x) {
  long long res = x;
  for (long long i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      while (x % i == 0) x /= i;
      res /= i;
      res *= (i - 1);
    }
  }
  if (x != 1) res /= x, res *= (x - 1);
  return res;
}
void solve() {
  cin >> n >> m;
  i10[0] = 1;
  i10[1] = expo(10, phi(m) - 1, m);
  p10[0] = 1;
  p10[1] = mod(10, m);
  for (long long i = 2; i < N; i++) {
    i10[i] = mod(i10[i - 1] * i10[1], m);
    p10[i] = mod(p10[i - 1] * p10[1], m);
  }
  for (long long i = 0; i < n - 1; i++) {
    long long u, v, w;
    cin >> u >> v >> w;
    g[u].push_back({v, w});
    g[v].push_back({u, w});
  }
  long long root = decompose(0, -1);
  cout << ans << endl;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  clock_t begin = clock();
  solve();
  clock_t end = clock();
  cerr << double(end - begin) / CLOCKS_PER_SEC << endl;
  return 0;
}

### Prompt

Create a solution in cpp for the following problem:

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>

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 5;
const long long LGN = 20;
const long long MOD = 1e9 + 7;
long long n, m, ans;
vector<pair<long long, long long> > g[N];
vector<long long> cd[N];
vector<long long> sz(N), dead(N), par(N), level(N), i10(N), p10(N);
map<long long, long long> aU, aD, um, dm;
long long down[LGN][N], up[LGN][N];
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  a %= b;
  return gcd(b, a);
}
long long mod(long long val, long long md) {
  val %= md;
  if (val < 0) val += md;
  return val;
}
long long dfs(long long u, long long v = -1) {
  sz[u] = 1;
  for (auto it : g[u]) {
    long long i = it.first;
    if (i != v && !dead[i]) sz[u] += dfs(i, u);
  }
  return sz[u];
}
long long findCentroid(long long u, long long sn, long long p = -1) {
  for (auto it : g[u]) {
    long long i = it.first;
    if (i != p && !dead[i] && sz[i] > sn / 2) return findCentroid(i, sn, u);
  }
  return u;
}
void precompute(long long c, long long lvl, long long p, long long cnt,
                long long centroid) {
  for (auto it : g[c]) {
    long long i = it.first;
    if (i != p && !dead[i]) {
      down[lvl][i] = mod(down[lvl][c] + it.second * i10[cnt], m);
      up[lvl][i] = mod(up[lvl][c] + it.second * p10[cnt], m);
      um[up[lvl][i]]++;
      dm[down[lvl][i]]++;
      precompute(i, lvl, c, cnt + 1, centroid);
    }
  }
}
long long decompose(long long u, long long v) {
  long long centroid = findCentroid(u, dfs(u));
  dead[centroid] = 1;
  par[centroid] = v;
  if (v != -1) {
    level[centroid] = level[v] + 1;
    cd[centroid].push_back(v);
    cd[v].push_back(centroid);
  }
  long long lvl = level[centroid];
  aU.clear();
  aD.clear();
  aU[0]++;
  aD[0]++;
  for (auto it : g[centroid]) {
    long long i = it.first;
    if (!dead[i]) {
      down[lvl][i] = mod(it.second, m);
      up[lvl][i] = mod(it.second, m);
      um.clear();
      dm.clear();
      um[up[lvl][i]]++;
      dm[down[lvl][i]]++;
      precompute(i, lvl, centroid, 1, centroid);
      for (auto j : um) {
        long long need = mod(-10 * j.first, m);
        if (aD.find(need) != aD.end()) {
          ans += aD[need] * j.second;
        }
      }
      for (auto j : dm) {
        long long need = mod(-i10[1] * j.first, m);
        if (aU.find(need) != aU.end()) {
          ans += aU[need] * j.second;
        }
      }
      for (auto j : um) aU[mod(j.first, m)] += j.second;
      for (auto j : dm) aD[mod(j.first, m)] += j.second;
    }
  }
  for (auto it : g[centroid]) {
    long long i = it.first;
    if (!dead[i]) decompose(i, centroid);
  }
  return centroid;
}
long long expo(long long base, long long x, long long M = MOD) {
  long long res = 1;
  base = base % M;
  while (x > 0) {
    if (x & 1) res = (res * base) % M;
    x = x >> 1;
    base = (base * base) % M;
  }
  return res;
}
long long phi(long long x) {
  long long res = x;
  for (long long i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      while (x % i == 0) x /= i;
      res /= i;
      res *= (i - 1);
    }
  }
  if (x != 1) res /= x, res *= (x - 1);
  return res;
}
void solve() {
  cin >> n >> m;
  i10[0] = 1;
  i10[1] = expo(10, phi(m) - 1, m);
  p10[0] = 1;
  p10[1] = mod(10, m);
  for (long long i = 2; i < N; i++) {
    i10[i] = mod(i10[i - 1] * i10[1], m);
    p10[i] = mod(p10[i - 1] * p10[1], m);
  }
  for (long long i = 0; i < n - 1; i++) {
    long long u, v, w;
    cin >> u >> v >> w;
    g[u].push_back({v, w});
    g[v].push_back({u, w});
  }
  long long root = decompose(0, -1);
  cout << ans << endl;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  clock_t begin = clock();
  solve();
  clock_t end = clock();
  cerr << double(end - begin) / CLOCKS_PER_SEC << endl;
  return 0;
}

```