#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <queue>
#include <functional>
#include <map>
using namespace std;
#define doub
const int MAX_V = 100010;
using Capacity = int;
using Cost = double;
const auto inf = numeric_limits<Capacity>::max() / 8;

struct Edge {
	int dst;
	Capacity cap, cap_orig;
	Cost cost;
	int revEdge; bool isRev;
	Edge(int dst, Capacity cap, Cost cost, int revEdge, bool isRev)
		:dst(dst), cap(cap), cap_orig(cap), cost(cost), revEdge(revEdge), isRev(isRev) {
	}
};

struct PrimalDual {
	int n;
	vector<vector<Edge> > g;
	PrimalDual(int n_) : n(n_), g(vector<vector<Edge> >(n_)) {}
	void add_edge(int src, int dst, Capacity cap, Cost cost) { // テヲツ慊嘉・ツ青妥ィツセツコ
		g[src].emplace_back(dst, cap, cost, g[dst].size(), false);
		g[dst].emplace_back(src, 0, -cost, g[src].size() - 1, true);
	}
	Cost solve(int s, int t, int f) {
		Cost res = 0;
		static Cost h[MAX_V], dist[MAX_V];
		static int prevv[MAX_V], preve[MAX_V];
		for(int i = 0; i < n; i++) {
			h[i] = 0;
		}
		while(f > 0) {
			typedef pair<Cost, int> pcv;
			priority_queue<pcv, vector<pcv>, greater<pcv> > q;
			for(int i = 0; i < n; i++) {
				dist[i] = inf;
			}
			dist[s] = 0;
			q.emplace(pcv(0, s));
			while(q.size()) {
				pcv p = q.top(); q.pop();
				int v = p.second;
				if(dist[v] < p.first) continue;
				for(int i = 0; i < g[v].size(); i++) {
					Edge &e = g[v][i];
					if(e.cap > 0 && dist[e.dst] > dist[v] + e.cost + h[v] - h[e.dst]) {
						dist[e.dst] = dist[v] + e.cost + h[v] - h[e.dst];
						prevv[e.dst] = v;
						preve[e.dst] = i;
						q.emplace(pcv(dist[e.dst], e.dst));
					}
				}
			}
			if(dist[t] == inf) {
				assert(0);
				return -1;
			}
			for(int v = 0; v < n; v++) {
				h[v] += dist[v];
			}
			// s-t テゥツ鳴禿ヲツ慊?ァツ淞ュティツキツッテ」ツ?ォテヲツイツソテ」ツ?」テ」ツ?ヲテァツ崢ョテ、ツクツ?ヲツ敖ッテヲツオツ?」ツ??
			int d = f;
			for(int v = t; v != s; v = prevv[v]) {
				d = min(d, g[prevv[v]][preve[v]].cap);
			}
			f -= d;
			res += d * h[t];
			for(int v = t; v != s; v = prevv[v]) {
				Edge &e = g[prevv[v]][preve[v]];
				e.cap -= d;
				g[v][e.revEdge].cap += d;
			}
		}
		return res;
	}
	// テヲツオツ?」ツつ古」ツ?淌」ツδ陛」ツδュテ」ツδシ=テ・ツ?ε」ツ??」ツ?ョテ・ツョツケテゥツ??テァツ渉セテ・ツ慊ィテ」ツ?ョテ・ツョツケテゥツ?湘」ツつ津ィツ。ツィテァツ、ツコ
	void view() {
		for(int i = 0; i < g.size(); i++) {
			for(int j = 0; j < g[i].size(); j++) {
				if(!g[i][j].isRev) {
					Edge& e = g[i][j];
					printf("%3d->%3d (flow:%d)\n", i, e.dst, e.cap_orig - e.cap);
				}
			}
		}
	}
};

struct point {
	double x, y, z;
};

double dist(point p1, point p2) {
	p1.x -= p2.x;
	p1.y -= p2.y;
	p1.z -= p2.z;
	return sqrt(p1.x * p1.x + p1.y * p1.y + p1.z * p1.z);
}

const double INF2 = 10000;

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int N, K;
	while(cin >> N >> K, N | K) {
		vector<point> ps(N);

		map<int, int> m;
		for(int i = 0; i < N; i++) {
			cin >> ps[i].x >> ps[i].y >> ps[i].z;
			m[ps[i].z]++;
		}

		bool flag = false;
		for(auto& p : m) {
			if(p.second > K) {
				printf("-1\n");
				flag = true;
				break;
			}
		}
		if(flag) continue;

		vector<int> in(N), out(N);
		for(int i = 0; i < N; i++) {
			in[i] = 2 * i;
			out[i] = in[i] + 1;
		}

		PrimalDual pd(2 * N + 2);
		int S = 2 * N, T = S + 1;

		for(int i = 0; i < N; i++) {
			for(int j = 0; j < N; j++) {
				if(ps[i].z <= ps[j].z) continue;
				pd.add_edge(out[i], in[j], 1, dist(ps[i], ps[j]));
			}
			pd.add_edge(in[i], out[i], 1, -INF2);
			pd.add_edge(out[i], T, 1, 0);
			pd.add_edge(S, in[i], 1, 0);
		}
		pd.add_edge(S, T, 100000, 0);

		double res = pd.solve(S, T, K);
		//pd.view();
		printf("%.10f\n", res + N  * INF2);
	}
}

Measured memory_bytes: 3764