E. Enchanted Tree

Time limit: 0.3sMemory limit: 256MBInput: Output:

In a magical forest, there are nn villages connected by n−1n-1 roads, forming a tree. Each road has a positive integer representing its magical power.

A traveler can journey between any two distinct villages along their unique simple path. The power of a journey is defined as the greatest common divisor (GCD) of the powers of all roads along the path.

The forest guardian has chosen a special integer kk. A journey is considered enchanted if its power is not divisible by kk.

Your task is to count the number of enchanted journeys.

Journeys between the same two villages are counted only once, regardless of direction.

Input

The first line contains two integers nn and kk (2≤n≤1052 \le n \le 10^5, 1≤k≤1091 \le k \le 10^9).

Each of the next n−1n-1 lines contains three integers uu, vv, and ww (1≤u,v≤n1 \le u,v \le n, u≠vu \ne v, 1≤w≤1091 \le w \le 10^9), describing a road between villages uu and vv with magical power ww.

It is guaranteed that the roads form a tree.

Output

Print a single integer: the number of enchanted journeys.

Example

stdin

5 6
1 2 6
2 3 10
2 4 12
4 5 3

stdout

7

Note

Here k=6k = 6. The tree has the roads 1−21 - 2 (power 66), 2−32 - 3 (power 1010), 2−42 - 4 (power 1212) and 4−54 - 5 (power 33).

The 77 enchanted journeys are:

  • 1→31 \to 3 (path 1−2−31 - 2 - 3): gcd⁡(6,10)=2\gcd(6, 10) = 2;
  • 1→51 \to 5 (path 1−2−4−51 - 2 - 4 - 5): gcd⁡(6,12,3)=3\gcd(6, 12, 3) = 3;
  • 2→32 \to 3 (path 2−32 - 3): gcd⁡(10)=10\gcd(10) = 10;
  • 2→52 \to 5 (path 2−4−52 - 4 - 5): gcd⁡(12,3)=3\gcd(12, 3) = 3;
  • 3→43 \to 4 (path 3−2−43 - 2 - 4): gcd⁡(10,12)=2\gcd(10, 12) = 2;
  • 3→53 \to 5 (path 3−2−4−53 - 2 - 4 - 5): gcd⁡(10,12,3)=1\gcd(10, 12, 3) = 1;
  • 4→54 \to 5 (path 4−54 - 5): gcd⁡(3)=3\gcd(3) = 3.

None of these powers is divisible by 66.

The remaining 33 journeys are not enchanted, because their power is divisible by 66:

  • 1→21 \to 2: gcd⁡(6)=6\gcd(6) = 6;
  • 1→41 \to 4 (path 1−2−41 - 2 - 4): gcd⁡(6,12)=6\gcd(6, 12) = 6;
  • 2→42 \to 4: gcd⁡(12)=12\gcd(12) = 12.

Log In or Sign Up to be able to send submissions!