#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
clock_t START;
inline long double TIME() {
  return (long double)(clock() - START) / CLOCKS_PER_SEC;
}
void SHOW() {
  cout << fixed << setprecision(10);
  cout << TIME() << " SECONDS FROM START\n";
}
long double TL = 2.0;
long long calls = 0;
inline bool IS() {
  if (TL - TIME() < 0.1) return true;
  return false;
}
template <typename T1, typename T2>
inline bool amin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T1, typename T2>
inline bool amax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> &p) {
  os << p.first << ' ' << p.second;
  return os;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (auto &u : v) is >> u;
  return is;
}
template <typename T>
ostream &operator<<(ostream &os, vector<T> &v) {
  for (auto &u : v) os << u << ' ';
  return os;
}
long long hash_mod[4] = {1000000007, 998244353, 1000000009, 999999937},
          mod = hash_mod[rnd() % 4];
long long hash_pows[4] = {179, 239, 1007, 2003}, P = hash_pows[rnd() % 4];
long long binpow(long long first, long long p) {
  long long res = 1;
  while (p) {
    if (p % 2) res = (res * first) % mod;
    first = (first * first) % mod;
    p /= 2;
  }
  return res;
}
const long long N = 1e5 + 7, M = 2e3 + 7, lg = 20, sq = 550, inf = 2e9 + 7;
const long double eps = 1e-9, pi = 3.14159265359;
long long n, m;
long long gcd(long long a, long long b, long long &first, long long &second) {
  if (a == 0) {
    first = 0;
    second = 1;
    return b;
  }
  long long x1, y1;
  long long g = gcd(b % a, a, x1, y1);
  second = x1;
  first = y1 - x1 * (b / a);
  return g;
}
long long inv_pw(long long pw) {
  long long first, second;
  long long g = gcd(pw, m, first, second);
  first %= m;
  if (first < m) first += m;
  return first;
}
vector<pair<long long, long long> > v[N];
long long used[N], sz[N];
long long sizes(long long first, long long p) {
  sz[first] = 1;
  for (auto u : v[first]) {
    if (u.first == p || used[u.first]) continue;
    sz[first] += sizes(u.first, first);
  }
  return sz[first];
}
long long centroid(long long first, long long p, long long s) {
  for (auto u : v[first]) {
    if (used[u.first] || u.first == p) continue;
    if (sz[u.first] * 2 >= s) return centroid(u.first, first, s);
  }
  return first;
}
vector<pair<long long, long long> > tmp;
void dfs(long long first, long long p, long long sum, long long d) {
  tmp.push_back({sum, d});
  for (auto u : v[first]) {
    if (u.first == p || used[u.first]) continue;
    dfs(u.first, first, (sum * 10 + u.second) % m, d + 1);
  }
}
vector<long long> tmp2;
void dfs2(long long first, long long p, long long sum, long long pw) {
  tmp2.push_back(sum);
  for (auto u : v[first]) {
    if (u.first == p || used[u.first]) continue;
    dfs2(u.first, first, (sum + pw * u.second) % m, (pw * 10) % m);
  }
}
long long pw[N], inv[N];
long long ans = 0;
map<long long, long long> cnt;
void solve(long long first) {
  cnt.clear();
  for (auto u : v[first]) {
    if (used[u.first]) continue;
    tmp.clear();
    tmp2.clear();
    dfs(u.first, first, u.second % m, 1);
    dfs2(u.first, first, u.second % m, 10 % m);
    for (auto &[d, l] : tmp) {
      long long d1 = (-d * inv[l]) % m;
      if (d1 < 0) d1 += m;
      ans += cnt[d1];
    }
    for (auto d : tmp2) {
      cnt[d]++;
    }
  }
  reverse((v[first]).begin(), (v[first]).end());
  cnt.clear();
  for (auto u : v[first]) {
    if (used[u.first]) continue;
    tmp.clear();
    tmp2.clear();
    dfs(u.first, first, u.second % m, 1);
    dfs2(u.first, first, u.second % m, (10) % m);
    for (auto &[d, l] : tmp) {
      if (d == 0) ans++;
      long long d1 = (-d * inv[l]) % m;
      if (d1 < 0) d1 += m;
      ans += cnt[d1];
    }
    for (auto d : tmp2) {
      if (d == 0) ans++;
      cnt[d]++;
    }
  }
  used[first] = 1;
  for (auto u : v[first]) {
    if (!used[u.first]) {
      sizes(u.first, -1);
      long long c = centroid(u.first, -1, sz[u.first]);
      solve(c);
    }
  }
}
void solve() {
  cin >> n >> m;
  for (long long i = 0; i < n - 1; i++) {
    long long a, b, c;
    cin >> a >> b >> c;
    c %= m;
    v[a].push_back({b, c});
    v[b].push_back({a, c});
  }
  pw[0] = inv[0] = 1;
  for (long long i = 1; i <= n; i++) {
    pw[i] = (pw[i - 1] * 10) % m;
    inv[i] = inv_pw(pw[i]);
  }
  solve(0);
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
}
