#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct Edge {
int dst;
ll w;
Edge() {}
Edge(ll d, ll w) : dst(d), w(w) {}
};
using Graph = vector<vector<Edge>>;
struct Cube { ll p[3]; };
Graph MakeGraph(const int n, const int s, vector<Cube> &c) {
Graph g(n);
ll d[3];
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
for (int k = 0; k < 3; ++k)
d[k] = s - abs(c[i].p[k] - c[j].p[k]);
if (min({d[0], d[1], d[2]}) <= 0) continue;
ll w = 2 * d[0] * d[1] + 2 * d[0] * d[2] + 2 * d[1] * d[2];
g[i].emplace_back(Edge(j, w));
g[j].emplace_back(Edge(i, w));
}
}
return g;
}
ll Dfs(const int start, const int prev, const int cur, int size, const Graph &g) {
if (size == 0) {
if ((g[cur].size() == 2) && ((g[cur][0].dst == start && g[cur][0].dst != prev)
|| (g[cur][1].dst == start && g[cur][1].dst != prev)))
return (g[cur][0].dst == start) ? g[cur][0].w : g[cur][1].w;
else
return 0;
}
ll max_w = -1;
for (const auto &e : g[cur])
if (e.dst != prev) {
const ll res = Dfs(start, cur, e.dst, size - 1, g);
if (res != -1) max_w = max(max_w, res + e.w);
}
return max_w;
}
int MinimumArea(const int n, const int k, const int s, const Graph &g) {
ll res = -1;
for (int v = 0; v < n; ++v)
res = max(res, Dfs(v, -1, v, k - 1, g));
return (res == -1) ? -1 : 6 * k * s * s - res;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, k, s;
while (cin >> n >> k >> s, n) {
vector<Cube> cube(n);
for (auto &c : cube)
for (int i = 0; i < 3; ++i) cin >> c.p[i];
cout << MinimumArea(n, k, s, MakeGraph(n, s, cube)) << '\n';
}
return 0;
}