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>
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
import java.util.Map;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author fakhoury
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        BDigitTree solver = new BDigitTree();
        solver.solve(1, in, out);
        out.close();
    }

    static class BDigitTree {
        static long M;
        static long[] power;
        static long[] inverse;

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt();
            M = in.nextLong();

            if (M == 1) {
                out.println((long) n * (n - 1));
                return;
            }

            power = new long[n + 1];
            inverse = new long[n + 1];
            power[0] = 1;
            inverse[0] = 1;
            for (int i = 1; i <= n; i++) {
                power[i] = power[i - 1] * 10 % M;
                inverse[i] = calcInverse(power[i]);
            }

            BDigitTree.Vertex[] vertices = new BDigitTree.Vertex[n];
            for (int i = 0; i < n; i++) {
                vertices[i] = new BDigitTree.Vertex();
                vertices[i].label = i;
            }

            for (int i = 1; i < n; i++) {
                BDigitTree.Vertex u = vertices[in.nextInt()];
                BDigitTree.Vertex v = vertices[in.nextInt()];
                int digit = in.nextInt();

                u.adj.add(new BDigitTree.Edge(v, digit));
                v.adj.add(new BDigitTree.Edge(u, digit));
            }

            out.println(vertices[0].solve());
        }

        private long calcInverse(long a) {
            long b = M;
            long u = 0;
            long v = 1;
            long tmp;
            while (a > 0) {
                long q = b / a;

                b -= q * a;
                tmp = a;
                a = b;
                b = tmp;

                u -= q * v;
                tmp = u;
                u = v;
                v = tmp;
            }

            if (u < 0) u += M;
            return u;
        }

        static class Edge {
            BDigitTree.Vertex to;
            int digit;

            public Edge(BDigitTree.Vertex to, int digit) {
                this.to = to;
                this.digit = digit;
            }

        }

        static class Vertex {
            ArrayList<BDigitTree.Edge> adj = new ArrayList<>();
            boolean excluded = false;
            int size = 0;
            int label;

            void calcSizes(BDigitTree.Vertex parent) {
                this.size = 1;
                for (BDigitTree.Edge ed : adj)
                    if (ed.to != parent && !ed.to.excluded) {
                        ed.to.calcSizes(this);
                        this.size += ed.to.size;
                    }
            }

            BDigitTree.Vertex centroid(BDigitTree.Vertex parent, int totalSize) {
                for (BDigitTree.Edge ed : adj)
                    if (ed.to != parent && !ed.to.excluded) {
                        if (2 * ed.to.size > totalSize) {
                            return ed.to.centroid(this, totalSize);
                        }
                    }
                return this;
            }

            long dfs(BDigitTree.Vertex parent, long valueDown, long valueUp, int quantity, List<Long> goingDown, List<Long> goingUp) {
                goingDown.add(valueDown * inverse[quantity] % M);
                goingUp.add(valueUp);

                long ret = (valueDown == 0 ? 1 : 0) + (valueUp == 0 ? 1 : 0);
                for (BDigitTree.Edge ed : adj)
                    if (ed.to != parent && !ed.to.excluded) {
                        long newValueDown = (valueDown * 10 + ed.digit) % M;
                        long newValueUp = (ed.digit * power[quantity] + valueUp) % M;
                        ret += ed.to.dfs(this, newValueDown, newValueUp, quantity + 1, goingDown, goingUp);
                    }

                return ret;
            }

            public long calc() {
                excluded = true;

                long answer = 0;
                Map<Long, Integer> freqUp = new TreeMap<>();
                Map<Long, Integer> freqDown = new TreeMap<>();

                for (BDigitTree.Edge ed : adj)
                    if (!ed.to.excluded) {
                        List<Long> goingDown = new ArrayList<>();
                        List<Long> goingUp = new ArrayList<>();
                        answer += ed.to.dfs(null, ed.digit % M, ed.digit % M, 1, goingDown, goingUp);

                        for (Long value : goingDown) {
                            Integer current = freqUp.get((M - value) % M);
                            answer += current == null ? 0 : current;
                        }
                        for (Long value : goingUp) {
                            Integer current = freqDown.get((M - value) % M);
                            answer += current == null ? 0 : current;
                        }
                        for (Long value : goingDown) {
                            Integer current = freqDown.get(value);
                            freqDown.put(value, (current == null ? 0 : current) + 1);
                        }
                        for (Long value : goingUp) {
                            Integer current = freqUp.get(value);
                            freqUp.put(value, (current == null ? 0 : current) + 1);
                        }
                    }

                for (BDigitTree.Edge ed : adj)
                    if (!ed.to.excluded) {
                        answer += ed.to.solve();
                    }

                return answer;
            }

            public long solve() {
                calcSizes(null);
                BDigitTree.Vertex u = centroid(null, size);
                return u.calc();
            }

        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void println(long i) {
            writer.println(i);
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public long nextLong() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}
