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;
static const int INF = 0x3f3f3f3f;
static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
template <typename T, typename U>
static void amin(T &x, U y) {
  if (y < x) x = y;
}
template <typename T, typename U>
static void amax(T &x, U y) {
  if (x < y) x = y;
}
struct GModInt {
  static int Mod;
  unsigned x;
  GModInt() : x(0) {}
  GModInt(signed sig) {
    int sigt = sig % Mod;
    if (sigt < 0) sigt += Mod;
    x = sigt;
  }
  GModInt(signed long long sig) {
    int sigt = sig % Mod;
    if (sigt < 0) sigt += Mod;
    x = sigt;
  }
  int get() const { return (int)x; }
  GModInt &operator+=(GModInt that) {
    if ((x += that.x) >= (unsigned)Mod) x -= Mod;
    return *this;
  }
  GModInt &operator-=(GModInt that) {
    if ((x += Mod - that.x) >= (unsigned)Mod) x -= Mod;
    return *this;
  }
  GModInt &operator*=(GModInt that) {
    x = (unsigned long long)x * that.x % Mod;
    return *this;
  }
  GModInt &operator/=(GModInt that) { return *this *= that.inverse(); }
  GModInt operator+(GModInt that) const { return GModInt(*this) += that; }
  GModInt operator-(GModInt that) const { return GModInt(*this) -= that; }
  GModInt operator*(GModInt that) const { return GModInt(*this) *= that; }
  GModInt operator/(GModInt that) const { return GModInt(*this) /= that; }
  GModInt inverse() const {
    signed a = x, b = Mod, u = 1, v = 0;
    while (b) {
      signed t = a / b;
      a -= t * b;
      std::swap(a, b);
      u -= t * v;
      std::swap(u, v);
    }
    if (u < 0) u += Mod;
    GModInt res;
    res.x = (unsigned)u;
    return res;
  }
};
int GModInt::Mod = 0;
vector<int> t_parent;
vector<int> t_ord;
template <typename T>
void wtree_getorder(const vector<vector<pair<int, T>>> &gw, int root,
                    vector<T> &t_weight) {
  int n = (int)gw.size();
  t_parent.assign(n, -1);
  t_ord.clear();
  t_weight.assign(n, T());
  vector<int> stk;
  stk.push_back(root);
  while (!stk.empty()) {
    int i = stk.back();
    stk.pop_back();
    t_ord.push_back(i);
    for (int j = (int)gw[i].size() - 1; j >= 0; j--) {
      int c = gw[i][j].first;
      if (t_parent[c] == -1 && c != root) {
        t_parent[c] = i;
        t_weight[c] = gw[i][j].second;
        stk.push_back(c);
      }
    }
  }
}
template <typename VertexAttr, typename EdgeAttr>
class CentroidDecomposition {
 public:
  struct Vertex : VertexAttr {
    int id;
    Vertex() {}
    Vertex(int id, VertexAttr attr) : id(id), VertexAttr(attr) {}
  };
  struct VertexAndEdge : Vertex, EdgeAttr {
    VertexAndEdge() {}
    VertexAndEdge(Vertex vertex, EdgeAttr edgeAttr)
        : Vertex(vertex), EdgeAttr(edgeAttr) {}
  };
  struct TourEntry : VertexAndEdge {
    unsigned f;
    TourEntry() {}
    TourEntry(Vertex to, EdgeAttr edgeAttr, unsigned f)
        : VertexAndEdge(to, edgeAttr), f(f) {}
  };
  struct DfsEntry : VertexAndEdge {
    int px;
    DfsEntry() {}
    DfsEntry(VertexAndEdge vertexAndEdge, int px)
        : VertexAndEdge(vertexAndEdge), px(px) {}
  };
  void init(const vector<int> &t_ord, const vector<int> &t_parent,
            const vector<VertexAttr> &vertexAttrs,
            const vector<EdgeAttr> &upEdgeAttrs,
            const vector<EdgeAttr> &downEdgeAttrs) {
    int n = (int)t_ord.size();
    vector<unsigned> a(n), b(n), f(n);
    unsigned maxf = 0;
    for (int ix = n - 1; ix >= 0; --ix) {
      int i = t_ord[ix], p = t_parent[i];
      unsigned m = b[i];
      m |= m >> 1;
      m |= m >> 2;
      m |= m >> 4;
      m |= m >> 8;
      m |= m >> 16;
      unsigned fi = ~(a[i] | m);
      fi &= ~fi + 1;
      f[i] = fi;
      if (maxf < fi) maxf = fi;
      unsigned S = (a[i] & ~(fi - 1)) | fi;
      if (p != -1) {
        b[p] |= a[p] & S;
        a[p] |= S;
      }
    }
    eulerTour(t_ord, t_parent, vertexAttrs, upEdgeAttrs, downEdgeAttrs, f,
              _tour);
    _currentLevel = 0;
    while (maxf >> 1 >> _currentLevel) ++_currentLevel;
    _subtrees.assign(_currentLevel + 1, vector<pair<int, int>>());
    if (_currentLevel != 0) {
      int middle = 1;
      while (_tour[middle].f != maxf) ++middle;
      std::rotate(_tour.begin() + 1, _tour.begin() + (middle + 1), _tour.end());
    }
    _subtrees[_currentLevel].assign(1, make_pair(1, (n - 1) * 2 + 1));
  }
  bool makeNextLevel() {
    if (_currentLevel == 0) return false;
    unsigned lv = 1U << (_currentLevel - 1);
    const vector<pair<int, int>> &currentSubtrees = _subtrees[_currentLevel];
    for (int si = 0; si < (int)currentSubtrees.size(); ++si) {
      int L = currentSubtrees[si].first, R = currentSubtrees[si].second;
      int nextL = L + 1;
      unsigned nextLevel = 0;
      int nextMiddle = -1;
      for (int ix = L; ix < R; ++ix) {
        unsigned f = _tour[ix].f;
        if (f > lv) {
          std::rotate(_tour.begin() + nextL, _tour.begin() + nextMiddle,
                      _tour.begin() + ix);
          int k = _currentLevel;
          while (nextLevel != (1U << k)) --k;
          _subtrees[k].push_back(make_pair(nextL, ix));
          nextL = ix + 2;
          nextLevel = 0;
          nextMiddle = -1;
        } else if (nextLevel < f) {
          nextLevel = f;
          nextMiddle = ix + 1;
        }
      }
    }
    --_currentLevel;
    return true;
  }
  int currentLevel() const { return _currentLevel; }
  int numCurrentSubtrees() const {
    return (int)_subtrees[_currentLevel].size();
  }
  void getDfsOrder(int si, vector<DfsEntry> &ord,
                   vector<pair<int, int>> &tmpStack) const {
    assert(0 <= si && si < numCurrentSubtrees());
    tmpStack.resize(_tour.size() / 2 + 2);
    pair<int, int> subtree = _subtrees[_currentLevel][si];
    int L = subtree.first, R = subtree.second;
    ord.clear();
    ord.push_back(DfsEntry(
        VertexAndEdge((Vertex)(VertexAndEdge)_tour[R - 1], EdgeAttr()), -1));
    int sp = 1;
    tmpStack[0] = make_pair(-1, -1);
    tmpStack[sp++] = make_pair(_tour[R - 1].id, 0);
    for (int tx = L; tx < R - 1; ++tx) {
      int id = _tour[tx].id;
      if (id == tmpStack[sp - 2].first) {
        --sp;
      } else {
        int px = tmpStack[sp - 1].second;
        int ix = (int)ord.size();
        tmpStack[sp++] = make_pair(id, ix);
        ord.push_back(DfsEntry(_tour[tx], px));
      }
    }
  }
  void debugShow() const {
    cerr << "level " << _currentLevel << ":\n";
    const vector<pair<int, int>> &currentSubtrees = _subtrees[_currentLevel];
    for (int si = 0; si < (int)currentSubtrees.size(); ++si) {
      int L = currentSubtrees[si].first, R = currentSubtrees[si].second;
      if (L == R) cerr << "[" << (L == 0 ? 0 : _tour[L - 1].id) << "]";
      for (int ix = L; ix < R; ++ix) {
        cerr << _tour[ix].id << ", ";
      }
      cerr << '\n';
    }
  }

 private:
  static void eulerTour(const vector<int> &t_ord, const vector<int> &t_parent,
                        const vector<VertexAttr> &vertexAttrs,
                        const vector<EdgeAttr> &upEdgeAttrs,
                        const vector<EdgeAttr> &downEdgeAttrs,
                        const vector<unsigned> &f, vector<TourEntry> &tour) {
    int n = (int)t_ord.size();
    tour.clear();
    tour.reserve((n - 1) * 2 + 1);
    int root = t_ord[0], i = root;
    tour.push_back(
        TourEntry(Vertex(root, vertexAttrs[root]), EdgeAttr(), f[root]));
    for (int ix = 1; ix <= n; ++ix) {
      int p = ix < n ? t_parent[t_ord[ix]] : root;
      while (i != p) {
        EdgeAttr w = upEdgeAttrs[i];
        i = t_parent[i];
        tour.push_back(TourEntry(Vertex(i, vertexAttrs[i]), w, f[i]));
      }
      if (ix < n) {
        i = t_ord[ix];
        EdgeAttr w = downEdgeAttrs[i];
        tour.push_back(TourEntry(Vertex(i, vertexAttrs[i]), w, f[i]));
      }
    }
    assert(tour.size() == (n - 1) * 2 + 1);
  }

 private:
  int _currentLevel;
  vector<TourEntry> _tour;
  vector<vector<pair<int, int>>> _subtrees;
};
struct VertexAttr {};
struct EdgeAttr {
  GModInt digit;
};
struct Sum {
  GModInt numF, numB;
  GModInt p10, ip10;
};
long long solve(vector<Sum>::iterator begin, vector<Sum>::iterator end) {
  const auto cmp = [](const Sum &a, const Sum &b) {
    return a.numB.get() < b.numB.get();
  };
  sort(begin, end, cmp);
  long long res = 0;
  for (auto it = begin; it != end; ++it) {
    GModInt val = (GModInt() - it->numF) * it->ip10;
    auto range =
        equal_range(begin, end, Sum{GModInt(), val, GModInt(), GModInt()}, cmp);
    res += range.second - range.first;
  }
  return res;
}
int main() {
  int n;
  int M;
  while (~scanf("%d%d", &n, &M)) {
    GModInt::Mod = M;
    vector<vector<int>> g(n);
    vector<vector<pair<int, EdgeAttr>>> gw(n);
    for (int i = 0; i < n - 1; ++i) {
      int u, v, w;
      scanf("%d%d%d", &u, &v, &w);
      g[u].push_back(v);
      g[v].push_back(u);
      gw[u].push_back(make_pair(v, EdgeAttr{w}));
      gw[v].push_back(make_pair(u, EdgeAttr{w}));
    }
    vector<EdgeAttr> digit;
    wtree_getorder(gw, 0, digit);
    CentroidDecomposition<VertexAttr, EdgeAttr> cd;
    cd.init(t_ord, t_parent, vector<VertexAttr>(n), digit, digit);
    const GModInt Ten = 10, InvTen = Ten.inverse();
    long long ans = 0;
    do {
      vector<pair<int, int>> tmpStack;
      vector<CentroidDecomposition<VertexAttr, EdgeAttr>::DfsEntry> ord;
      vector<Sum> sums;
      vector<int> bounds;
      for (int(si) = 0; (si) < (int)(cd.numCurrentSubtrees()); ++(si)) {
        cd.getDfsOrder(si, ord, tmpStack);
        sums.resize(ord.size());
        sums[0] = {GModInt(), GModInt(), GModInt(1), GModInt(1)};
        bounds.clear();
        bounds.push_back(0);
        long long cnt = 0;
        for (int ix = 1; ix < (int)ord.size(); ++ix) {
          const auto &entry = ord[ix];
          const auto &psum = sums[entry.px];
          auto &isum = sums[ix];
          GModInt d = entry.digit;
          isum = {psum.numF * Ten + d, psum.numB + d * psum.p10, psum.p10 * Ten,
                  psum.ip10 * InvTen};
          if (entry.px == 0) bounds.push_back(ix);
        }
        bounds.push_back((int)ord.size());
        for (int(bi) = 0; (bi) < (int)(bounds.size() - 1); ++(bi))
          cnt -=
              solve(sums.begin() + bounds[bi], sums.begin() + bounds[bi + 1]);
        cnt += solve(sums.begin(), sums.end());
        ans += cnt;
      }
    } while (cd.makeNextLevel());
    printf("%lld\n", ans);
  }
  return 0;
}
