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 MIF = 1e9 + 7;
long long in() {
  long long x = 0;
  int n = 1;
  char c = getchar();
  while (!isdigit(c)) n = (c == '-') ? -1 : 1, c = getchar();
  while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
  return x * n;
}
const int N = 1e5 + 1;
const int MAX = 1e9;
int MOD, n;
bool isprime[100001];
vector<long long> primes;
vector<pair<int, int> > adj[N];
int subsize[N];
bool visited[N];
int treesize;
vector<int> clrlist;
long long up[N];
long long down[N];
int h[N];
int PHI;
int dppart[N];
long long mult(long long a, long long b) { return (a * b) % MOD; }
long long add(long long a, long long b) { return (a + b + MOD) % MOD; }
long long modpow(long long a, long long b) {
  long long r = 1;
  while (b) {
    if (b & 1) r = (r * a) % MOD;
    a = (a * a) % MOD;
    b >>= 1;
  }
  return r;
}
void Sieve(int n) {
  memset(isprime, 1, sizeof(isprime));
  isprime[1] = false;
  for (int i = 2; i <= n; i++) {
    if (isprime[i]) {
      primes.push_back(i);
      for (int j = 2 * i; j <= n; j += i) {
        isprime[j] = false;
      }
    }
  }
}
int phi(int n) {
  long long num = 1;
  long long num2 = n;
  for (long long i = 0; primes[i] * primes[i] <= n; i++) {
    if (n % primes[i] == 0) {
      num2 /= primes[i];
      num *= (primes[i] - 1);
    }
    while (n % primes[i] == 0) {
      n /= primes[i];
    }
  }
  if (n > 1) {
    num2 /= n;
    num *= (n - 1);
  }
  n = 1;
  num *= num2;
  return num;
}
long long inv(long long a) { return modpow(a, PHI - 1); }
void dfs(int u, int par) {
  if (par == -1) clrlist.clear();
  subsize[u] = 1;
  clrlist.push_back(u);
  for (int i = 0; i < adj[u].size(); i++) {
    int v = adj[u][i].first;
    if (visited[v]) continue;
    if (v == par) continue;
    dfs(v, u);
    subsize[u] += subsize[v];
  }
  if (par == -1) treesize = subsize[u];
}
int centroid(int u, int par) {
  for (int i = 0; i < adj[u].size(); i++) {
    int v = adj[u][i].first;
    if (visited[v]) continue;
    if (v == par) continue;
    if (subsize[v] * 2 > treesize) return centroid(v, u);
  }
  return u;
}
int parts = 0;
void fill(int u, int p, int cent) {
  if (p == cent) {
    dppart[u] = parts;
    parts++;
  } else if (p != -1) {
    dppart[u] = dppart[p];
  }
  for (int i = 0; i < adj[u].size(); i++) {
    int v = adj[u][i].first;
    int w = adj[u][i].second;
    if (v == p || visited[v]) continue;
    down[v] = add(mult(down[u], 10), w);
    up[v] = add(up[u], mult(modpow(10, h[u]), w));
    h[v] = h[u] + 1;
    fill(v, u, cent);
  }
}
long long solve(int cent) {
  for (int i = 0; i < clrlist.size(); i++) {
    up[clrlist[i]] = 0;
    down[clrlist[i]] = 0;
    h[clrlist[i]] = 0;
  }
  parts = 0;
  fill(cent, -1, cent);
  parts--;
  dppart[cent] = -1;
  map<long long, long long> tot;
  vector<map<long long, long long> > vec;
  vec.resize(parts + 1);
  tot[0]++;
  for (int i = 0; i < clrlist.size(); i++) {
    int u = clrlist[i];
    if (u == cent) continue;
    tot[up[u]]++;
    vec[dppart[u]][up[u]]++;
  }
  long long ans = 0;
  for (int i = 0; i < clrlist.size(); i++) {
    int u = clrlist[i];
    int ht = h[u];
    int pt = dppart[u];
    if (u == cent) {
      ans += (tot[0] - 1);
    } else {
      long long val = ((-down[u]) % MOD + MOD) % MOD;
      val = mult(val, inv(modpow(10, ht)));
      ans += (tot[val] - vec[pt][val]);
    }
  }
  return ans;
}
long long compsolve(int u) {
  dfs(u, -1);
  int cent = centroid(u, -1);
  long long ans = solve(cent);
  visited[cent] = true;
  for (int i = 0; i < adj[cent].size(); i++) {
    int v = adj[cent][i].first;
    if (!visited[v]) ans += compsolve(v);
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> MOD;
  Sieve(100000);
  PHI = phi(MOD);
  for (int i = 0; i < n - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    adj[u].push_back(pair<int, int>(v, w));
    adj[v].push_back(pair<int, int>(u, w));
  }
  cout << compsolve(0) << '\n';
  return 0;
}
