#include <bits/stdc++.h>

using namespace std;

#define int long long

const long double eps = 1e-6;
const long double inf = 1e10;
const long double mxval = 1e6;

#define eq(a, b) (fabs((a)-(b)) < eps)
#define lt(a, b) ((a)-(b) < -eps)

struct PrimalDual {
  struct edge {
    int to, capacity;
    double cost;
    int rev;
    edge(){}
    edge(int to, int capacity, double cost, int rev):to(to), capacity(capacity), cost(cost), rev(rev){}
  };

  vector< vector<edge> > graph;
  vector<long double> potential, mincost;
  vector<int> prevv, preve;
  PrimalDual(int V):graph(V), potential(V), mincost(V), prevv(V), preve(V){}
  void add_edge(int from, int to, int capacity, long double cost) {
    graph[from].push_back(edge(to, capacity, cost, (int)graph[to].size()));
    graph[to].push_back(edge(from, 0, -cost, (int)graph[from].size()-1));
  }
  long double min_cost_flow(int source, int sink, int f) {
    long double res = 0;
    fill(potential.begin(), potential.end(), 0);
    fill(prevv.begin(), prevv.end(), -1);
    fill(preve.begin(), preve.end(), -1);

    typedef pair<long double, int> Pi;
    while(f > 0) {
      priority_queue<Pi, vector<Pi>, greater<Pi> > que;
      fill(mincost.begin(), mincost.end(), inf);
      mincost[source] = 0;
      que.push(Pi(0, source));
      while(!que.empty()) {
	Pi p = que.top(); que.pop();
	int v = p.second;
	if(mincost[v] < p.first) continue;
	for(int i = 0; i < (int)graph[v].size(); i++) {
	  edge& e = graph[v][i];
	  long double dual_cost = mincost[v] + e.cost + potential[v] - potential[e.to];
	  if(e.capacity > 0 && !eq(dual_cost, mincost[e.to]) && dual_cost < mincost[e.to]) {
	    mincost[e.to] = dual_cost;
	    //printf("%lld %.12Lf\n", e.to, mincost[e.to]);
	    prevv[e.to] = v; preve[e.to] = i;
	    que.push(Pi(mincost[e.to], e.to));
	  }
	}
      }

      for(int v = 0; v < (int)graph.size(); v++) potential[v] += mincost[v];

      if(potential[sink] >= 0) break;
      int d = f;
      for(int v = sink; v != source; v = prevv[v]) d = min(d, graph[prevv[v]][preve[v]].capacity);
      //cout<<"OK"<<endl;
      f -= d;

      res += d * potential[sink];
      for(int v = sink; v != source; v = prevv[v]) {
	edge& e = graph[prevv[v]][preve[v]];
	e.capacity -= d;
	graph[v][e.rev].capacity += d;
      }
    }
    return res;
  }
};

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

signed main() {
  int N, K;
  while(scanf("%lld %lld", &N, &K), N || K) {
    vector<int> x(N), y(N), z(N);
    map<int, int> mp;
    for(int i = 0; i < N; ++i) {
      scanf("%lld %lld %lld", &x[i], &y[i], &z[i]);
      mp[z[i]]++;
    }
    if(N <= K) {
      printf("0\n");
      continue;
    }
    bool flag = false;
    for(auto p : mp) {
      if(p.second > K) flag = true;
    }
    if(flag) {
      printf("-1\n");
      continue;
    }

    int s = 2*N, t = s+1, V = t+1;
    PrimalDual graph(V);
    for(int i = 0; i < N; ++i) {
      graph.add_edge(s, i, 1, 0);
      graph.add_edge(i, N+i, 1, -mxval);
      graph.add_edge(N+i, t, 1, 0);
      for(int j = 0; j < N; ++j) {
	if(i == j) continue;
	if(z[i] > z[j]) {
	  long double norm = sq(x[i]-x[j])+sq(y[i]-y[j])+sq(z[i]-z[j]);
	  long double dist = sqrt(norm);
	  graph.add_edge(N+i, j, 1, dist);
	}
      }
    }
    long double res = graph.min_cost_flow(s, t, K);
    printf("%.12Lf\n", res+N*mxval);
  }
  return 0;
}



Measured memory_bytes: 3580