#include <cstdio>
#include <cstring>
#include <vector>
#include <iostream>
#include <cmath>
#include <queue>
#include <algorithm>

using namespace std;

const int N = 100 + 10;
const int V = 200 + 10;
const int M = V * V * 2;
const long double INF = 1e5;

struct Edge
{
	long double w;
	int c, t;
	Edge *n, *r;
} edges[M], *totEdge, *firstEdge[V];

int n, m;
int x[N], y[N], z[N];

Edge* makeEdge(int s, int t, int c, long double w)
{
	Edge *e = totEdge ++;
	e->t = t; e->n = firstEdge[s]; firstEdge[s] = e;
	e->w = w; e->c = c;
	return e;
}

void addEdge(int s, int t, int c, long double w)
{
	Edge *st = makeEdge(s, t, c, w), *ts = makeEdge(t, s, 0, -w);
	st->r = ts; ts->r = st;
}

long double sqr(long double x)
{
	return x * x;
}

long double dis(int u, int v)
{
	return sqrt(sqr(x[u] - x[v]) + sqr(y[u] - y[v]) + sqr(z[u] - z[v]));
}

int spfa(int s, int t, int tot, long double &ret)
{
	static int in[V];
	static int aug[V];
	static long double dis[V];
	static Edge *prev[V];

	for(int i = 0; i < tot; ++ i) {
		in[i] = false;
		dis[i] = INF * INF;
	}

	queue<int> que;
	que.push(s);
	in[s] = true;
	dis[s] = 0;
	aug[s] = tot;
	aug[t] = 0;

	for( ; que.size(); ) {
		int u = que.front(); que.pop();
		for(Edge *e = firstEdge[u]; e; e = e->n) {
			if (e->c == 0) continue;
			int v = e->t;
			if (dis[v] > dis[u] + e->w) {
				dis[v] = dis[u] + e->w;
				aug[v] = min(aug[u], e->c);
				prev[v] = e;
				if (! in[v]) {
					in[v] = true;
					que.push(v);
				}
			}
		}
		in[u] = false;
	}

	if (aug[t] == 0)
		return false;
	ret += dis[t] * aug[t];
	int by = aug[t];

	for(int u = t; u != s; ) {
		prev[u]->c -= by;
		prev[u]->r->c += by;
		u = prev[u]->r->t;
	}
	return true;
}

long double costFlow(int s, int t, int tot)
{
	long double ret = 0;
	for( ; spfa(s, t, tot, ret); );
	return ret;
}

void solve()
{
	totEdge = edges;
	for(int i = 0; i < n; ++ i) {
		cin >> x[i] >> y[i] >> z[i];
	}
	int s = n + n;
	int t = s + 2;
	for(int i = 0; i <= t; ++ i) {
		firstEdge[i] = 0;
	}
	addEdge(s, s + 1, m, 0);
	for(int i = 0; i < n; ++ i) {
		addEdge(s + 1, i, 1, 0);
		addEdge(i, i + n, 1, -INF);
		addEdge(i + n, t, 1, 0);
		for(int j = 0; j < n; ++ j) {
			if (z[j] < z[i]) {
				addEdge(i + n, j, 1, dis(i, j));
			}
		}
	}

	long double ret = costFlow(s, t, n + n + 3) + n * INF;
	if (ret >= INF) {
		cout << -1 << endl;
	} else {
		printf("%.16f\n", (double)ret);
	}
}

int main()
{
	for( ; cin >> n >> m && (n || m); ) {
		solve();
	}
	return 0;
}