#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) begin(v), end(v)
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for(int i = (int)(s); i < (int)(n); i++)

template<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}
template<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}

using pint = pair<int, int>;
using tint = tuple<int, int, int>;
using vint = vector<int>;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

struct Point {
  int x, y, z;
  Point(){}
  Point(int x, int y, int z):x(x), y(y), z(z){}
};

struct edge {
  int to, cost;
  edge(){}
  edge(int to, int cost):to(to), cost(cost){}
};

int n, k, s;
vector<Point> ps;
vector< vector<edge> > graph;

bool isIntersect(int a, int b) {
  if(abs(ps[a].x - ps[b].x) > s) return false;
  if(abs(ps[a].y - ps[b].y) > s) return false;
  if(abs(ps[a].z - ps[b].z) > s) return false;
  return true;
}

int intersectSurface(int a, int b) {
  int vx = s - abs(ps[a].x - ps[b].x);
  int vy = s - abs(ps[a].y - ps[b].y);
  int vz = s - abs(ps[a].z - ps[b].z);
  return (vx*vy + vy*vz + vz*vx) * 2;
}

int ans;

void dfs(int u, int p, int r, int d, int sum) {
  if(d == k) {
    if(k > 2) {
      for(edge& e : graph[u]) {
	if(e.to == r) sum += e.cost;
      }
    }
    chmax(ans, sum);
    return;
  }
  for(edge& e : graph[u]) {
    if(e.to == p || e.to == r) continue;
    dfs(e.to, u, r, d+1, sum+e.cost);
  }
}

int solve() {
  ans = -1;
  rep(u, n) dfs(u, -1, u, 1, 0);
  if(ans == -1) return -1;
  return s*s*6*k - ans;
}

signed main()
{
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  while(cin >> n >> k >> s, n) {
    ps.clear();
    rep(i, n) {
      int x, y, z;
      cin >> x >> y >> z;
      ps.emplace_back(x, y, z);
    }
    graph.clear(); graph.resize(n);
    rep(i, n) rep(j, n) if(i != j) {
      if(isIntersect(i, j)) graph[i].emplace_back(j, intersectSurface(i, j));
    }
    cout << solve() << endl;
  }

  return 0;
}