#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;
}
