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 MOD = (int)1e9 + 7;
const int INF = (int)1e9;
const long long LINF = (long long)1e18;
const long double PI = acos((long double)-1);
const long double EPS = 1e-9;
inline long long gcd(long long a, long long b) {
  long long r;
  while (b) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long fpow(long long n, long long k, int p = MOD) {
  long long r = 1;
  for (; k; k >>= 1) {
    if (k & 1) r = r * n % p;
    n = n * n % p;
  }
  return r;
}
template <class T>
inline int chkmin(T& a, const T& val) {
  return val < a ? a = val, 1 : 0;
}
template <class T>
inline int chkmax(T& a, const T& val) {
  return a < val ? a = val, 1 : 0;
}
inline long long isqrt(long long k) {
  long long r = sqrt(k) + 1;
  while (r * r > k) r--;
  return r;
}
inline long long icbrt(long long k) {
  long long r = cbrt(k) + 1;
  while (r * r * r > k) r--;
  return r;
}
inline void addmod(int& a, int val, int p = MOD) {
  if ((a = (a + val)) >= p) a -= p;
}
inline void submod(int& a, int val, int p = MOD) {
  if ((a = (a - val)) < 0) a += p;
}
inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; }
inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }
const int MAXN = 100000 + 10;
int n, m;
vector<pair<int, int> > adj[MAXN];
int del[MAXN];
int size[MAXN];
void dfs(int u, int p) {
  size[u] = 1;
  for (int i = 0; i < adj[u].size(); i++) {
    int v = adj[u][i].first;
    if (v != p && !del[v]) {
      dfs(v, u);
      size[u] += size[v];
    }
  }
}
int findcenter(int u) {
  int p = -1;
  dfs(u, -1);
  int cap = size[u] >> 1;
  while (1) {
    bool found = false;
    for (int i = 0; i < adj[u].size(); i++) {
      int v = adj[u][i].first;
      if (v != p && !del[v] && size[v] > cap) {
        found = true;
        p = u;
        u = v;
        break;
      }
    }
    if (!found) return u;
  }
}
const int maxn = 100000 + 10;
int lev[maxn];
int f[maxn];
int g[maxn];
int p10[maxn];
int ip10[maxn];
map<int, int> hs;
long long ans;
void add(int u, int p = -1) {
  for (int i = (0); i < (int((adj[u]).size())); i++) {
    int v = adj[u][i].first;
    int w = adj[u][i].second;
    if (!del[v] && v != p) {
      lev[v] = lev[u] + 1;
      f[v] = mult(f[u], 10, m);
      addmod(f[v], w, m);
      int tmp = mult(f[v], ip10[lev[v]], m);
      hs[tmp]++;
      add(v, u);
    }
  }
}
void rem(int u, int p = -1) {
  for (int i = (0); i < (int((adj[u]).size())); i++) {
    int v = adj[u][i].first;
    int w = adj[u][i].second;
    if (!del[v] && v != p) {
      lev[v] = lev[u] + 1;
      f[v] = mult(f[u], 10, m);
      addmod(f[v], w, m);
      int tmp = mult(f[v], ip10[lev[v]], m);
      hs[tmp]--;
      rem(v, u);
    }
  }
}
void calc(int u, int p = -1) {
  for (int i = (0); i < (int((adj[u]).size())); i++) {
    int v = adj[u][i].first;
    int w = adj[u][i].second;
    if (!del[v] && v != p) {
      lev[v] = lev[u] + 1;
      g[v] = g[u];
      addmod(g[v], mult(p10[lev[u]], w, m), m);
      ans += hs[(m - g[v]) % m];
      if (f[v] % m == 0) {
        ans++;
      }
      calc(v, u);
    }
  }
}
void work(int u) {
  hs.clear();
  lev[u] = 0, f[u] = g[u] = 0;
  for (int i = (0); i < (int((adj[u]).size())); i++) {
    int v = adj[u][i].first;
    int w = adj[u][i].second;
    if (!del[v]) {
      lev[v] = lev[u] + 1;
      f[v] = mult(f[u], 10, m);
      addmod(f[v], w, m);
      int tmp = mult(f[v], ip10[lev[v]], m);
      hs[tmp]++;
      add(v, u);
    }
  }
  hs[0]++;
  for (int i = (0); i < (int((adj[u]).size())); i++) {
    int v = adj[u][i].first;
    int w = adj[u][i].second;
    if (!del[v]) {
      lev[v] = lev[u] + 1;
      f[v] = mult(f[u], 10, m);
      addmod(f[v], w, m);
      int tmp = mult(f[v], ip10[lev[v]], m);
      hs[tmp]--;
      rem(v, u);
      lev[v] = lev[u] + 1;
      g[v] = g[u];
      addmod(g[v], mult(p10[lev[u]], w, m), m);
      ans += hs[(m - g[v]) % m];
      if (f[v] % m == 0) {
        ans++;
      }
      calc(v, u);
      add(v, u);
      hs[tmp]++;
    }
  }
}
void tcd(int u) {
  u = findcenter(u);
  del[u] = 1;
  work(u);
  for (int i = 0; i < adj[u].size(); i++) {
    int v = adj[u][i].first;
    if (!del[v]) {
      tcd(v);
    }
  }
}
pair<int, int> euclid(int a, int b) {
  if (b == 0) return make_pair(1, 0);
  pair<int, int> r = euclid(b, a % b);
  return make_pair(r.second, r.first - a / b * r.second);
}
int inverse(int a, int m) {
  pair<int, int> r = euclid(a, m);
  return (r.first % m + m) % m;
}
void solve() {
  cin >> n >> m;
  p10[0] = 1;
  for (int i = (1); i < (maxn); i++) p10[i] = mult(p10[i - 1], 10, m);
  for (int i = (0); i < (maxn); i++) ip10[i] = inverse(p10[i], m);
  for (int i = (0); i < (n - 1); i++) {
    int u, v, w;
    cin >> u >> v >> w;
    adj[u].push_back(make_pair(v, w)), adj[v].push_back(make_pair(u, w));
  }
  tcd(0);
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}
