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;
long long Set(long long n, long long pos) { return n = n | (1LL << pos); }
long long reset(long long n, long long pos) { return n = n & ~(1LL << pos); }
bool check(long long n, long long pos) { return (bool)(n & (1LL << pos)); }
long long mul(long long a, long long b) {
  long long c;
  c = (a % 1000000007 * b % 1000000007) % 1000000007;
  return c;
}
long long add(long long a, long long b) {
  long long c;
  c = (a % 1000000007 + b % 1000000007) % 1000000007;
  return c;
}
long long sub(long long a, long long b) {
  long long c;
  c = ((a % 1000000007 - b % 1000000007) % 1000000007 + 1000000007) %
      1000000007;
  return c;
}
long long power(long long x, long long y, long long p) {
  long long res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long s2, s1, t2, t1;
long long egcd(long long a, long long b) {
  long long temp;
  if (a % b == 0) {
    return s1;
  }
  temp = s2 - (a / b) * s1;
  s2 = s1;
  s1 = temp;
  temp = t2 - (a / b) * t1;
  t2 = t1;
  t1 = temp;
  return egcd(b, a % b);
}
vector<pair<long long, long long> > graph[100005];
long long son_size[100005], mxson[100005], root;
bool vis[100005];
long long n, m, ans, th[100005];
map<long long, long long> global_up_val;
map<long long, long long> local_up_val[100005];
void in_edges() {
  long long u, v, c;
  for (long long i = 1; i <= n - 1; i++) {
    scanf("%lld", &u);
    scanf("%lld", &v);
    scanf("%lld", &c);
    u++;
    v++;
    graph[u].push_back(make_pair(v, c));
    graph[v].push_back(make_pair(u, c));
  }
}
void dfssize(long long src, long long par) {
  son_size[src] = 1;
  mxson[src] = 0;
  for (long long i = 0; i < graph[src].size(); i++) {
    long long adj = graph[src][i].first;
    if (adj != par && vis[adj] == 0) {
      dfssize(adj, src);
      son_size[src] += son_size[adj];
      mxson[src] = max(mxson[src], son_size[adj]);
    }
  }
}
void dfsroot(long long main_source, long long source, long long par) {
  if (son_size[main_source] - son_size[source] > mxson[source]) {
    mxson[source] = son_size[main_source] - son_size[source];
  }
  if (mxson[source] < mxson[root]) {
    root = source;
  }
  for (long long i = 0; i < graph[source].size(); i++) {
    long long adj = graph[source][i].first;
    if (adj != par && vis[adj] == 0) {
      dfsroot(main_source, adj, source);
    }
  }
}
void dfsdis(long long src, long long par, long long h, long long prev_up,
            long long main_adj) {
  global_up_val[prev_up]++;
  local_up_val[main_adj][prev_up]++;
  long long sa = power(10, h, m);
  for (long long i = 0; i < graph[src].size(); i++) {
    long long adj = graph[src][i].first;
    long long dig = graph[src][i].second;
    if (adj != par && vis[adj] == 0) {
      long long now_up = ((prev_up % m) + (((dig % m) * (sa % m)) % m)) % m;
      dfsdis(adj, src, h + 1, now_up, main_adj);
    }
  }
}
void calc(long long src, long long par, long long prev_down, long long h) {
  long long fir = (((-prev_down) % m) + m) % m;
  long long sec = th[h];
  s2 = t1 = 1;
  s1 = t2 = 0;
  sec = (egcd(sec, m) % m + m) % m;
  long long up_lagbe = ((fir % m) * (sec % m)) % m;
  ans += global_up_val[up_lagbe];
  for (long long i = 0; i < graph[src].size(); i++) {
    long long adj = graph[src][i].first;
    long long dig = graph[src][i].second;
    if (adj != par && vis[adj] == 0) {
      long long now_down = (((prev_down * 10) % m) + (dig % m)) % m;
      calc(adj, src, now_down, h + 1);
    }
  }
}
void calc2(long long src, long long par, long long prev_down, long long h,
           long long main_adj) {
  long long fir = (((-prev_down) % m) + m) % m;
  long long sec = th[h];
  s2 = t1 = 1;
  s1 = t2 = 0;
  sec = (egcd(sec, m) % m + m) % m;
  long long up_lagbe = ((fir % m) * (sec % m)) % m;
  ans -= local_up_val[main_adj][up_lagbe];
  for (long long i = 0; i < graph[src].size(); i++) {
    long long adj = graph[src][i].first;
    long long dig = graph[src][i].second;
    if (adj != par && vis[adj] == 0) {
      long long now_down = (((prev_down * 10) % m) + (dig % m)) % m;
      calc2(adj, src, now_down, h + 1, main_adj);
    }
  }
}
void dfs(long long src) {
  dfssize(src, -1);
  root = 0;
  mxson[0] = n;
  dfsroot(src, src, -1);
  for (long long i = 0; i < graph[root].size(); i++) {
    long long adj = graph[root][i].first;
    long long dig = graph[root][i].second;
    if (vis[adj] == 0) {
      dfsdis(adj, root, 1, dig % m, adj);
    }
  }
  global_up_val[0]++;
  calc(root, -1, 0, 0);
  ans--;
  for (long long i = 0; i < graph[root].size(); i++) {
    long long adj = graph[root][i].first;
    long long dig = graph[root][i].second;
    if (vis[adj] == 0) {
      calc2(adj, root, dig % m, 1, adj);
      local_up_val[adj].clear();
    }
  }
  global_up_val.clear();
  long long now_root = root;
  vis[root] = 1;
  for (long long i = 0; i < graph[now_root].size(); i++) {
    long long adj = graph[now_root][i].first;
    if (vis[adj] == 0) {
      dfs(adj);
    }
  }
}
int main() {
  th[0] = 1;
  scanf("%lld", &n);
  scanf("%lld", &m);
  for (long long i = 1; i < 100005; i++) {
    th[i] = (10 * th[i - 1]) % m;
  }
  in_edges();
  dfs(1);
  cout << ans << endl;
  return 0;
}
