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>
#pragma comment(linker, "/STACK:256000000")
using namespace std;
const string LN = "\n";
const double eps = 1e-9;
const double pi = acos(-1.0);
const int INF = (int)1e9 + 7;
const long long LINF = (long long)9e18 + 7;
const long long P = 353251;
const long long P1 = 353251;
const long long P2 = 239017;
const long long MOD = 1e9 + 7;
const long long MOD1 = 1e9 + 7;
const long long MOD2 = 1e9 + 9;
int n, m;
int root;
int p[100007];
int w[100007];
bool del[100007];
long long st1[100007];
long long st2[100007];
map<int, int> ma, ma2;
long long ans[100007];
vector<pair<int, int> > g[100007];
int dfsw(int v, int pred) {
  w[v] = 1;
  for (int i = 0; i < g[v].size(); i++) {
    int to = g[v][i].first;
    if (to != pred && !del[to]) w[v] += dfsw(to, v);
  }
  return w[v];
}
int dfsg(int v, int pred, int ww) {
  for (int i = 0; i < g[v].size(); i++) {
    int to = g[v][i].first;
    if (to != pred && !del[to]) {
      if (w[to] > ww / 2) return dfsg(to, v, ww);
    }
  }
  return v;
}
void dfsans(int v, int pred, long long now, bool f, int hh) {
  if (f)
    ma[now]++;
  else
    ma2[now]++;
  for (int i = 0; i < g[v].size(); i++) {
    int to = g[v][i].first;
    int c = g[v][i].second;
    long long d = (now + (long long)c * st1[hh]) % m;
    if (to != pred && !del[to]) {
      dfsans(to, v, d, f, hh + 1);
    }
  }
}
int rt;
void go(int v, int pred, long long now, int hh) {
  if (now == 0) ans[rt]++;
  ans[rt] += (ma[(m - now) * st2[hh] % m] - ma2[(m - now) * st2[hh] % m]);
  for (int i = 0; i < g[v].size(); i++) {
    int to = g[v][i].first;
    int c = g[v][i].second;
    long long d = (now * (long long)10 + c) % m;
    if (to != pred && !del[to]) {
      go(to, v, d, hh + 1);
    }
  }
}
int build(int v) {
  dfsw(v, -1);
  v = dfsg(v, -1, w[v]);
  del[v] = 1;
  ma.clear();
  for (int i = 0; i < g[v].size(); i++) {
    int to = g[v][i].first;
    if (!del[to]) {
      dfsans(to, -1, g[v][i].second % m, 1, 1);
    }
  }
  ans[v] += ma[0];
  rt = v;
  for (int i = 0; i < g[v].size(); i++) {
    int to = g[v][i].first;
    if (!del[to]) {
      ma2.clear();
      dfsans(to, -1, g[v][i].second % m, 0, 1);
      go(to, -1, g[v][i].second % m, 1);
    }
  }
  for (int i = 0; i < g[v].size(); i++) {
    int to = g[v][i].first;
    if (!del[to]) {
      p[build(to)] = v;
    }
  }
  return v;
}
long long bp(long long a, int k) {
  if (k == 1) return a;
  if (k & 1) {
    return bp(a, k - 1) * a % m;
  } else {
    long long q = bp(a, k / 2);
    return q * q % m;
  }
}
int phi(int n) {
  int result = n;
  for (int i = 2; i * i <= n; ++i)
    if (n % i == 0) {
      while (n % i == 0) n /= i;
      result -= result / i;
    }
  if (n > 1) result -= result / n;
  return result;
}
const bool is_testing = 0;
int main() {
  srand('D' + 'E' + 'N' + 'I' + 'S' + 'S' + 'O' + 'N');
  if (is_testing) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  } else {
  }
  scanf("%d %d", &n, &m);
  if (m == 1) {
    cout << (long long)(n - 1) * (long long)n;
    exit(0);
  }
  st1[1] = 10 % m;
  int ss = phi(m);
  for (int i = 2; i <= 100005; i++) st1[i] = (st1[i - 1] * (long long)10) % m;
  for (int i = 1; i <= 100005; i++) st2[i] = bp(st1[i], ss - 1);
  for (int i = 0; i < n - 1; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    g[a].push_back(make_pair(b, c));
    g[b].push_back(make_pair(a, c));
  }
  root = build(0);
  p[root] = -1;
  long long ha = 0;
  for (int i = 0; i < n; i++) ha += ans[i];
  cout << ha;
}
