#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;

ll n, k, s, x[2000], y[2000], z[2000], e[2000][2], cost[2000][2], counter[2000], used[2000], temp[5000];
const ll INF = 100000000000LL;

ll dfs(ll num, ll before, ll c, ll sum) {
  if (used[num] >= 2) return INF;
  //printf("%lld, %lld, %lld, %lld\n", num, before, c, sum);
  used[num]++;
  ll ans = INF;
  if (c >= k-1) ans = s * s * 6 * k - sum;
  for (ll i = 0; i < counter[num]; i++) if (e[num][i] != before) {
    temp[c] = cost[num][i];
    sum += cost[num][i];
    if (c == k-1 && used[i] == 1 && used[e[num][i]] == 1) return s * s * 6 * k - sum;
    if (c >= k-1) sum -= temp[c-k+1];
    ans = min(ans, dfs(e[num][i], num, c+1, sum));
  }
  return ans;
}

int main() {
  while (scanf("%lld%lld%lld", &n, &k, &s), n) {
    for (ll i = 0; i < n; i++) scanf("%lld%lld%lld", &x[i], &y[i], &z[i]), counter[i] = 0;
    for (ll i = 0; i < n; i++) {
      for (ll j = i+1; j < n; j++) {
        if (i == j) continue;
        ll xdiff = s - abs(x[i] - x[j]);
        ll ydiff = s - abs(y[i] - y[j]);
        ll zdiff = s - abs(z[i] - z[j]);
        if (xdiff < 0 || ydiff < 0 || zdiff < 0) continue;
        e[i][counter[i]] = j;
        cost[i][counter[i]] = (xdiff * ydiff + ydiff * zdiff + zdiff * xdiff) * 2;
        counter[i]++;
        e[j][counter[j]] = i;
        cost[j][counter[j]] = (xdiff * ydiff + ydiff * zdiff + zdiff * xdiff) * 2;
        counter[j]++;
      }
    }
    for (ll i = 0; i < n; i++) used[i] = 0;
    ll ans = INF;
    for (ll i = 0; i < n; i++) if (!used[i] && counter[i] == 1) ans = min(ans, dfs(i, -1, 0, 0));
    for (ll i = 0; i < n; i++) if (!used[i] && counter[i] == 2) ans = min(ans, dfs(i, e[i][0], 0, 0));
    for (ll i = 0; i < n; i++) if (!used[i]) ans = min(ans, dfs(i, -1, 0, 0));
    if (ans == INF) ans = -1;
    printf("%lld\n", ans);
  }
}

