#include <bits/stdc++.h>

using namespace std;

using ll = long long;
struct Edge {
    int dst;
    ll w;
    Edge() {}
    Edge(int 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 + ((c[i].p[k] < c[j].p[k]) ? (c[i].p[k] - c[j].p[k]) : (c[j].p[k] - c[i].p[k]));

            if (d[0] <= 0 || d[1] <= 0 || 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;
}

int MinimumArea(const int n, const int k, const int s, const Graph &g) {
    vector<bool> used(n, false);
    ll res = -1;

    for (int u = 0; u < 2 * n; ++u) {
        if (used[u % n] || (u < n && g[u % n].size() == 2)) continue;

        const int v = u % n;

        vector<int> sub;
        int cur = v;
        while (true) {
            used[cur] = true;
            sub.push_back(cur);
            if (1 <= g[cur].size() && !used[g[cur][0].dst])
                cur = g[cur][0].dst;
            else if (g[cur].size() == 2 && !used[g[cur][1].dst])
                cur = g[cur][1].dst;
            else
                break;
        }

        const int size = sub.size();
        if (size < k) continue;

        ll sum = 0;
        for (int i = 0; i < k - 1; ++i)
            sum += ((g[sub[i]][0].dst == sub[i + 1]) ? g[sub[i]][0].w : g[sub[i]][1].w);

        const bool is_cycle = (g[sub[0]].size() == 2);
        // cout << "size = " << size << ", k = " << k << " cycle ?= " << is_cycle << endl;
        // cout << sub[0] << " - " << sub[size - 1] << ", sum = " << sum << endl;
        if (size == k && is_cycle) {
            sum += ((g[sub[0]][0].dst == sub[size - 1]) ? g[sub[0]][0].w : g[sub[0]][1].w);
            res = max(res, sum);
            continue;
        }
        res = max(res, sum);

        // cout << " += " << ((g[sub[0]][0].dst == sub[size - 1]) ? g[sub[0]][0].w : g[sub[0]][1].w) << endl;
        // cout << "sum = " << sum << endl;

        const int lim = (is_cycle ? size - 1 : size - k);
//        cout << "v = " << v << ", lim = " << lim << endl;
        for (int i = 1; i <= lim; ++i) {
//            cout << "sum = " << sum << ", - " << ((g[sub[i]][0].dst == sub[i - 1]) ? g[sub[i]][0].w : g[sub[i]][1].w); 
            sum -= ((g[sub[i]][0].dst == sub[i - 1]) ? g[sub[i]][0].w : g[sub[i]][1].w);
            const int j1 = (i + k - 2) % size, j2 = (i + k - 1) % size;
            sum += ((g[sub[j1]][0].dst == sub[j2]) ? g[sub[j1]][0].w : g[sub[j1]][1].w);
//            cout << ", + " << ((g[sub[j1]][0].dst == sub[j2]) ? g[sub[j1]][0].w : g[sub[j1]][1].w) << endl;
//            cout << "sub[" << j1 << "] = " << sub[j1] << ", sub[" << j2 << "] = " << sub[j2] << endl;
//            cout << "sub[" << i << "] = " << sub[i] << ", sub[" << (i + 1) % n << "] = " << sub[(i + 1) % n]
//                 << ", sum = " << sum << endl << endl;;
            res = max(res, sum);
        }
    }

    // for (int v = 0; v < n; ++v) {
    //     cout << v << " : ";
    //     for (auto e : g[v])
    //         cout << "(" << e.dst << ", " << e.w << "), ";
    //     cout << '\n';
    // }

    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;
}



Measured memory_bytes: 3156