Cities - Competitors#174

Cities - Competitors

2 seconds
64 megabytes
Easy

In Flatlandia, there are cities. Each city sells exactly one of possible goods. Cities selling the same good are called competitor cities. The goal is to connect all the cities with a trade network that satisfies the following:

Your task is to design such a road network so that its total length is minimized. The length of the network is defined as the sum of the lengths of all constructed roads. Roads can only be accessed from the two cities they connect, and they may be built at different levels to avoid intersections.

Input

The first line contains two integers and (1 \leq N \leq 200, 1 \leq M \leq 200). Each of the next lines contains three integers X_i, Y_i, Z_i: X_i and Y_i are the coordinates of city , and Z_i is the number of the good sold in this city (-10000 \leq X_i, Y_i \leq 10000, 1 \leq Z_i \leq M, 1 \leq i \leq N).

Output

Print one real number with precision — the minimum possible total length of the road network. If it is not possible to connect all cities into such a network, print -1.

Examples

Example 1

Loading
Loading