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 N = 200100;
int t, n;
int MOD;
vector<pair<long long int, long long int> > v[N];
long long int kol[N];
long long int gotov[N], sad, a, b;
long long int c;
long long int sol;
vector<long long int> p;
long long int FI;
long long int inv[N], inv1[N];
long long int powmod(long long int a, long long int x) {
  long long int rez = 1;
  while (x) {
    if (x % 2) {
      rez = ((rez % MOD) * (a % MOD)) % MOD;
    }
    a = ((a % MOD) * (a % MOD)) % MOD;
    x /= 2;
  }
  return rez % MOD;
}
long long int divide(long long int a, long long int dub) {
  return a * inv[dub] % MOD;
}
int djeca(int node, int par) {
  kol[node] = 0;
  for (int i = 0; i < v[node].size(); i++) {
    if (v[node][i].first != par && !gotov[v[node][i].first])
      kol[node] += djeca(v[node][i].first, node);
  }
  kol[node]++;
  return kol[node];
}
int centr(int node, int par, int sz) {
  for (int i = 0; i < v[node].size(); i++) {
    if (v[node][i].first != par && !gotov[v[node][i].first] &&
        kol[v[node][i].first] > sz / 2) {
      return centr(v[node][i].first, node, sz);
    }
  }
  return node;
}
map<long long int, long long int> m;
map<long long int, long long int> pom[N];
void dfs(int node, int par, long long int x, long long int y, int d, int turn) {
  int di = ((-y) % MOD + MOD) % MOD;
  di = divide(di, d) % MOD;
  if (par != -1) sol += (m[di] - pom[turn][di]);
  if (!x && par != -1) sol++;
  if (!y && par != -1) sol++;
  for (int i = 0; i < v[node].size(); i++) {
    if (v[node][i].first != par && !gotov[v[node][i].first]) {
      int kamo;
      if (par == -1)
        kamo = i;
      else
        kamo = turn;
      long long int novix = ((v[node][i].second * inv1[d]) % MOD + x) % MOD;
      long long int noviy = ((y * 10) % MOD + v[node][i].second) % MOD;
      dfs(v[node][i].first, node, novix, noviy, d + 1, kamo);
      if (par == -1) pom[i].clear();
    }
  }
  return;
}
void broji(int node, int par, long long int x, int d, int turn) {
  if (par != -1) {
    m[x % MOD]++;
    pom[turn][x % MOD]++;
  }
  for (int i = 0; i < v[node].size(); i++) {
    if (v[node][i].first != par && !gotov[v[node][i].first]) {
      int kamo;
      if (par == -1)
        kamo = i;
      else
        kamo = turn;
      long long int novix = ((v[node][i].second * inv1[d]) % MOD + x) % MOD;
      broji(v[node][i].first, node, novix, d + 1, kamo);
    }
  }
  return;
}
void decomp(int node, int par) {
  kol[node] = djeca(node, par);
  int sad = centr(node, par, kol[node]);
  broji(sad, -1, 0, 0, 0);
  dfs(sad, -1, 0, 0, 0, 0);
  m.clear();
  gotov[sad] = 1;
  for (int i = 0; i < v[sad].size(); i++) {
    if (!gotov[v[sad][i].first]) decomp(v[sad][i].first, node);
  }
  return;
}
void solve() {
  cin >> n >> MOD;
  for (int i = 0; i < n - 1; i++) {
    cin >> a >> b >> c;
    v[a].push_back(make_pair(b, c));
    v[b].push_back(make_pair(a, c));
  }
  long long int k = MOD;
  long long int result = MOD;
  for (int p = 2; p * p <= k; ++p) {
    if (k % p == 0) {
      while (k % p == 0) k /= p;
      result -= result / p;
    }
  }
  if (k > 1) result -= result / k;
  FI = result;
  for (int i = 0; i <= n; i++) {
    inv1[i] = powmod(10, i);
  }
  for (int i = 0; i <= n; i++) {
    inv[i] = powmod(inv1[i], FI - 1);
  }
  decomp(0, -1);
  cout << sol << endl;
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}
