#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
//#define EPS 0.000000001
using namespace std;


#define NUM 205
long double A = 50000;
#define EPS 0.0000000001

typedef pair<long double,int> P;
struct Edge{
	Edge(int arg_to,int arg_capacity,long double arg_cost,int arg_rev_index){
		to = arg_to;
		capacity = arg_capacity;
		cost = arg_cost;
		rev_index = arg_rev_index;
	}

	int to,capacity,rev_index;
	long double cost;
};

struct Point{

	long double x,y,z;
};

int N,K;
int V; //頂点数
int in_num[205],IN[105],OUT[105];
vector<Edge> G[NUM]; //グラフの隣接リスト表現
long double h[NUM]; //ポテンシャル
long double dist[NUM]; //最短距離
int pre_node[NUM],pre_edge[NUM]; //直前の頂点と辺
Point point[105];


long double calc_dist(Point a,Point b){

	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z));
}

void add_edge(int from,int to,int capacity,long double cost){
	G[from].push_back(Edge(to,capacity,cost,G[to].size()));
	G[to].push_back(Edge(from,0,-cost,G[from].size()-1));
}

long double min_cost_flow(int source,int sink,int flow){

	long double ret = 0;
	for(int i = 0; i < V; i++)h[i] = 0; //ポテンシャルを0にする
	while(flow > 0){

		priority_queue<P,vector<P>,greater<P>> Q;
		for(int i = 0; i < V; i++)dist[i] = BIG_NUM;
		dist[source] = 0;
		Q.push(P(0,source));

		while(!Q.empty()){
			P p = Q.top();
			Q.pop();
			int node_id = p.second;
			if(dist[node_id] < p.first)continue; //最短でなければSKIP

			for(int i = 0; i < G[node_id].size(); i++){
				Edge &e = G[node_id][i];
				if(e.capacity > 0 && dist[e.to] > dist[node_id]+e.cost+h[node_id]-h[e.to]){

					dist[e.to] = dist[node_id]+e.cost+h[node_id]-h[e.to];

					pre_node[e.to] = node_id;
					pre_edge[e.to] = i;
					Q.push(P(dist[e.to],e.to));
				}
			}
		}

		if(dist[sink] == BIG_NUM){
			//これ以上流せない
			break;
		}

		for(int node_id = 0; node_id < V; node_id++)h[node_id] += dist[node_id];

		int tmp_flow = flow;

		for(int node_id = sink; node_id != source; node_id = pre_node[node_id]){
			tmp_flow = min(tmp_flow,G[pre_node[node_id]][pre_edge[node_id]].capacity);
		}
		flow -= tmp_flow;
		ret += tmp_flow*h[sink];

		for(int node_id = sink; node_id != source; node_id = pre_node[node_id]){

			Edge &e = G[pre_node[node_id]][pre_edge[node_id]];
			e.capacity -= tmp_flow;
			G[node_id][e.rev_index].capacity += tmp_flow;
		}
	}
	return ret;
}

void func(){

	for(int i = 0; i <= 200; i++){

			in_num[i] = 0;
		}

	int Z;

	for(int i = 0; i < N; i++){

		scanf("%Lf %Lf %d",&point[i].x,&point[i].y,&Z);
		in_num[Z+100]++;
		point[i].z = Z;
	}


	if(N <= K){

		printf("0\n");
		return;
	}

	for(int i = 0; i <= 200; i++){
		if(in_num[i] > K){

			printf("-1\n");
			return;
		}
	}

	for(int i = 0; i < NUM; i++){

		G[i].clear();
	}



	int index = 0;
	for(int i = 0; i < N; i++){

		IN[i] = index++;
	}
	for(int i = 0; i < N; i++){

		OUT[i] = index++;
	}
	int source = index++;
	int sink = index++;

	V = index;

	for(int i = 0; i < N; i++){

		add_edge(source,IN[i],1,0);
		add_edge(IN[i],OUT[i],1,-A);
		add_edge(OUT[i],sink,1,0);
	}

	for(int i = 0; i < N; i++){
		for(int k = 0; k < N; k++){
			if(k == i)continue;

			if(point[i].z > point[k].z){
				add_edge(OUT[i],IN[k],1,calc_dist(point[i],point[k]));
			}
		}
	}

	long double ans = min_cost_flow(source,sink,K);

	ans += (long double)N*A;
	printf("%.12Lf\n",ans);
}

int main(){

	while(true){

		scanf("%d %d",&N,&K);
		if(N == 0 && K == 0)break;

		func();
	}

	return 0;
}



Measured memory_bytes: 4036