Minimum Cost Flow

Given a spanning tree and extra edges, with a one-pipe discount available, find the minimum number of edge swaps to reach a minimum-cost spanning tree.

Hard8Minimum spanning treeGraphUnion-findGreedyNo attempts yetTime limit3sMemory limit512 MB

Problem

The city of Watermoo has buildings numbered 11 through NN, and MM pipes that connect pairs of buildings. Because of an oversight in urban planning, building 11 is the only sewage treatment plant in the city.

Each pipe is either active or inactive. The set of active pipes is a valid plan if building 11 is connected, directly or indirectly, to every other building using active pipes. A pipe directly connects a pair of buildings. Buildings XX and ZZ are indirectly connected if XX is directly or indirectly connected to YY and YY is directly or indirectly connected to ZZ.

The municipal government of Watermoo runs a valid plan of N1N - 1 pipes today, and it considers that plan too expensive. Each pipe has a monthly maintenance fee that the city pays while the pipe is active, and the total cost of a plan is the sum of the maintenance fees of its active pipes. An inactive pipe costs nothing.

Researchers at the University of Watermoo built an experimental pipe enhancer that you can use on one pipe of your choice. Using it reduces that pipe's cost from CC to max(0,CD)\max(0, C - D), where DD is the strength of the enhancer.

The city wants the cost of the plan to be minimum, and it wants the work done quickly. Each day you can activate one pipe and deactivate another pipe. How many days do you need so that the set of active pipes is a valid plan whose cost is minimum over all valid plans and all choices of the enhanced pipe?

The plan may be invalid while you work, but it has to be a valid plan at the end.

Input

The first line contains the integers NN, MM, and DD. (1N1000001 \le N \le 100000, N1M200000N - 1 \le M \le 200000, 0D1090 \le D \le 10^9)

Each of the next MM lines contains three integers AiA_i, BiB_i, and CiC_i, which means that a pipe connects building AiA_i and building BiB_i and costs CiC_i per month while it is active. (1Ai,BiN1 \le A_i, B_i \le N, 1Ci1091 \le C_i \le 10^9)

The first N1N - 1 of these lines are the valid plan the city is using now.

At most one pipe connects any two buildings, and no pipe connects a building to itself.

Output

Print one integer on a single line, the minimum number of days needed to finish the task. If the plan the city is using now is already optimal, print 00.

Hint

In the first example D=0D = 0, so it does not matter which pipe you use the enhancer on, because no maintenance fee changes. On the first day, deactivate the pipe between building 22 and building 33 and activate the pipe between building 44 and building 11.

One way to finish the second example in the minimum number of days is this. First use the enhancer on the pipe between building 11 and building 22 to reduce its cost to 33. On the first day, replace the pipe between building 22 and building 33 with the pipe between building 11 and building 33. On the second day, replace the pipe between building 11 and building 44 with the pipe between building 11 and building 55. The cost of the optimal plan is 1010. Using the enhancer on the pipe between building 11 and building 33, or on the pipe between building 11 and building 55, is not allowed in any solution: that pipe would have a maintenance fee of 00, the optimal plan would then cost 1111, and a cost of 1010 is already reachable.

In the third example the plan the city is using now is already optimal. Watch out for integer overflow when you implement your solution.