716_E. Digit Tree_18735
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.*;
import java.util.*;
import java.math.*;
import java.util.concurrent.*;

public final class digit_tree
{
    static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	static FastScanner sc=new FastScanner(br);
    static PrintWriter out=new PrintWriter(System.out);
	static Random rnd=new Random();
	static Node[][] al;
	static int[] x,y,cnt,size,w;
	static boolean[] removed;
	static int ctr,n;
	static long mod;
	static long res=0;
	static Map<Integer,Integer> m1;
	static int[] pow,inv_pow;
	static int maxn=(int)(1e5+5),ff;
	static long tot;
	
	static void build(int u,int p)
	{
		size[u]=1;ctr++;
		
		for(Node x:al[u])
		{
			if(x.idx!=p && !removed[x.idx])
			{
				build(x.idx,u);size[u]+=size[x.idx];
			}
		}
	}
	
	static int findCentroid(int u,int p)
	{
		int max=-1,big_child=-1;
		
		for(Node x:al[u])
		{
			if(x.idx!=p && !removed[x.idx])
			{
				if(size[x.idx]>max)
				{
					max=size[x.idx];big_child=x.idx;
				}
			}
		}
		
		if(ctr-size[u]<=ctr/2 && max<=ctr/2)
		{
			return u;
		}
		
		return findCentroid(big_child,u);
	}
	
	static int add(long a,long b)
	{
		long ret=a+b;
		
		if(ret>=mod)
		{
			ret%=mod;
		}
		
		return (int)ret;
	}
	
	static int mul(long a,long b)
	{
		long ret=a*b;
		
		if(ret>=mod)
		{
			ret%=mod;
		}
		
		return (int)ret;
	}
	
	static int pow(int a,long b)
	{
		int x=1,y=a;
		
		while(b>0)
		{	
			if(b%2==1)
			{
				x=mul(x,y);
			}
			
			y=mul(y,y);b=b/2;
		}
		
		return (int)(x%mod);
	}
	
	static void build()
	{
		pow=new int[maxn];pow[0]=(int)(1%mod);
		
		ff=(int)(10%mod);
		
		for(int i=1;i<maxn;i++)
		{
			pow[i]=mul(pow[i-1],ff);
		}
		
		inv_pow=new int[maxn];int inv=pow(ff,tot-1);inv_pow[1]=inv;
		
		for(int i=2;i<maxn;i++)
		{
			inv_pow[i]=mul(inv,inv_pow[i-1]);
		}
	}
	
	static long getTot(long m)
	{
		long ret=1;
		
		for(long i=2;i*i<=m;i++)
		{
			int ctr=0;long curr=1;
			
			while(m%i==0)
			{
				m/=i;ctr++;
				
				curr*=i;
			}
			
			if(ctr>0)
			{
				ret*=(curr-(curr/i));
			}
		}
		
		if(m>1)
		{
			ret*=(m-1);
		}
		
		return ret;
	}
	
	static void dfs1(int u,int p,int curr,int level)
	{
		m1.put(curr,m1.getOrDefault(curr,0)+1);
		
		for(Node x:al[u])
		{
			if(x.idx!=p && !removed[x.idx])
			{
				dfs1(x.idx,u,add(curr,mul(pow[level],x.cost)),level+1);
			}
		}
	}
	
	static void dfs2(int u,int p,int curr,int level)
	{
		m1.put(curr,m1.get(curr)-1);
		
		for(Node x:al[u])
		{
			if(x.idx!=p && !removed[x.idx])
			{
				dfs2(x.idx,u,add(curr,mul(pow[level],x.cost)),level+1);
			}
		}
	}
	
	static void dfs3(int u,int p,int curr,int level)
	{	
		int now=(int)((mod-curr)%mod);
		
		int zz=mul(inv_pow[level],now);
		
		res=res+m1.getOrDefault(zz,0);
		
		for(Node x:al[u])
		{
			if(x.idx!=p && !removed[x.idx])
			{
				dfs3(x.idx,u,add(x.cost,mul(ff,curr)),level+1);
			}
		}
	}
	
	static void decompose(int u,int p) throws Exception
	{
		ctr=0;build(u,p);
		
		int centroid=findCentroid(u,p);removed[centroid]=true;m1=new HashMap<>();
		
		m1.put(0,1);
		
		for(Node x:al[centroid])
		{
			if(x.idx!=p && !removed[x.idx])
			{	
				dfs1(x.idx,centroid,x.cost,1);
			}
		}
		
		res=res+m1.get(0)-1;
		
		for(Node x:al[centroid])
		{
			if(x.idx!=p && !removed[x.idx])
			{	
				dfs2(x.idx,centroid,x.cost,1);
				
				dfs3(x.idx,centroid,x.cost,1);
				
				dfs1(x.idx,centroid,x.cost,1);
			}
		}
		
	//	out.println(centroid+" "+res);
		
		for(Node x:al[centroid])
		{
			if(x.idx!=p && !removed[x.idx])
			{
				decompose(x.idx,centroid);
			}
		}
	}
	
    public static void main(String args[]) throws Exception
    {
		n=sc.nextInt();mod=sc.nextLong();
		
		tot=getTot(mod);//out.println(tot);
		
		build();
		
		x=new int[n];y=new int[n];cnt=new int[n];w=new int[n];
		
		for(int i=1;i<n;i++)
		{
			x[i]=sc.nextInt();y[i]=sc.nextInt();
			
			cnt[x[i]]++;cnt[y[i]]++;
			
			w[i]=(int)(sc.nextInt()%mod);
		}
		
		al=new Node[n][];
		
		for(int i=0;i<n;i++)
		{
			al[i]=new Node[cnt[i]];cnt[i]=0;
		}
		
		for(int i=1;i<n;i++)
		{
			int u=x[i],v=y[i];
			
			al[u][cnt[u]++]=new Node(v,w[i]);al[v][cnt[v]++]=new Node(u,w[i]);
		}
		
		size=new int[n];removed=new boolean[n];decompose(0,-1);
		
		out.println(res);out.close();
    }
	
	private static class Node
	{
		int idx,cost;
		
		public Node(int idx,int cost)
		{
			this.idx=idx;this.cost=cost;
		}
	}
}
class FastScanner
{
    BufferedReader in;
    StringTokenizer st;

    public FastScanner(BufferedReader in) {
        this.in = in;
    }
	
    public String nextToken() throws Exception {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(in.readLine());
        }
        return st.nextToken();
    }
	
	public String next() throws Exception {
		return nextToken().toString();
	}
	
    public int nextInt() throws Exception {
        return Integer.parseInt(nextToken());
    }

    public long nextLong() throws Exception {
        return Long.parseLong(nextToken());
    }

    public double nextDouble() throws Exception {
        return Double.parseDouble(nextToken());
    }
}