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;
#pragma comment(linker, "/STACK:102400000,102400000")
inline void RI(int &x) {
  char c;
  while ((c = getchar()) < '0' || c > '9')
    ;
  x = c - '0';
  while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0';
}
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double dnf = 1e18;
const double eps = 1e-12;
const long long mod = 10007;
const int maxn = 100010;
struct Side {
  int to, next;
  long long w;
} side[maxn << 1];
int n, m;
int top, node[maxn];
bool del[maxn];
long long ans, f[maxn], inv[maxn];
void add_side(int u, int v, long long w) {
  side[top] = (Side){v, node[u], w};
  node[u] = top++;
}
void gcd(int a, int b, int &d, int &x, int &y) {
  if (!b) {
    d = a;
    x = 1;
    y = 0;
  } else {
    gcd(b, a % b, d, y, x);
    y -= x * (a / b);
  }
}
int get_inv(int a, int n) {
  int d, x, y;
  gcd(a, n, d, x, y);
  return d == 1 ? (x + n) % n : -1;
}
int total_size, size[maxn], max_size, idx, new_root;
map<int, int> sub[maxn], all;
vector<pair<int, int> > vv[maxn];
void get_size(int u, int pre) {
  size[u] = 1;
  for (int i = node[u]; i != -1; i = side[i].next) {
    int v = side[i].to;
    if (del[v] || v == pre) continue;
    get_size(v, u);
    size[u] += size[v];
  }
}
void dfs_root(int u, int pre) {
  int mx = 0, sum = 1;
  for (int i = node[u]; i != -1; i = side[i].next) {
    int v = side[i].to;
    if (del[v] || v == pre) continue;
    dfs_root(v, u);
    mx = max(mx, size[v]);
    sum += size[v];
  }
  mx = max(mx, total_size - sum);
  if (mx < max_size) max_size = mx, new_root = u;
}
int get_root(int root) {
  new_root = -1;
  max_size = (1 << 30);
  get_size(root, -1);
  total_size = size[root];
  dfs_root(root, -1);
  return new_root;
}
void get_deep1(int u, int pre, int deep, long long w) {
  sub[idx][w]++;
  for (int i = node[u]; i != -1; i = side[i].next) {
    int v = side[i].to;
    if (v == pre || del[v]) continue;
    long long ss = (w + f[deep] * side[i].w) % m;
    get_deep1(v, u, deep + 1, ss);
  }
}
void get_deep2(int u, int pre, int deep, long long w) {
  vv[idx].push_back(make_pair(deep, w));
  for (int i = node[u]; i != -1; i = side[i].next) {
    int v = side[i].to;
    if (v == pre || del[v]) continue;
    get_deep2(v, u, deep + 1, (w * 10 + side[i].w) % m);
  }
}
long long get_ans(vector<pair<int, int> > a, map<int, int> mp) {
  long long ans = 0;
  for (int i = 0; i < a.size(); i++) {
    long long t = (m - a[i].second) * inv[a[i].first] % m;
    if (mp.count(t)) ans += mp[t];
  }
  return ans;
}
void solve(int root) {
  root = get_root(root);
  del[root] = 1;
  if (total_size == 1) return;
  idx = 0;
  sub[idx].clear();
  sub[idx++][0] = 1;
  all.clear();
  all[0] = 1;
  for (int i = node[root]; i != -1; i = side[i].next) {
    int v = side[i].to;
    if (del[v]) continue;
    sub[idx].clear();
    get_deep1(v, root, 1, side[i].w % m);
    for (map<int, int>::iterator it = sub[idx].begin(); it != sub[idx].end();
         it++)
      all[it->first] += it->second;
    idx++;
  }
  idx = 0;
  vv[idx].clear();
  vv[idx++].push_back(make_pair(0, 0));
  for (int i = node[root]; i != -1; i = side[i].next) {
    int v = side[i].to;
    if (del[v]) continue;
    vv[idx].clear();
    get_deep2(v, root, 1, side[i].w % m);
    idx++;
  }
  for (int i = 0; i < idx; i++)
    ans += get_ans(vv[i], all) - get_ans(vv[i], sub[i]);
  for (int i = node[root]; i != -1; i = side[i].next) {
    int v = side[i].to;
    if (del[v]) continue;
    solve(v);
  }
}
int main() {
  while (cin >> n >> m) {
    inv[0] = f[0] = 1;
    for (int i = 1; i < maxn; i++) {
      f[i] = f[i - 1] * 10 % m;
      inv[i] = get_inv(f[i], m);
    }
    top = 0;
    memset(node, -1, sizeof(node));
    for (int i = 0; i < n - 1; i++) {
      int u, v, w;
      scanf("%d%d%d", &u, &v, &w);
      add_side(u, v, w);
      add_side(v, u, w);
    }
    memset(del, 0, sizeof(del));
    ans = 0;
    solve(0);
    cout << ans << endl;
  }
}
