#include <bits/stdc++.h>
using namespace std;
#define int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(a)-1;i>=b;i--)
#define chmin(a,b) (a)=min((a),(b));
#define chmax(a,b) (a)=max((a),(b));
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define printV(v) cout<<(#v)<<":";for(auto(x):(v)){cout<<" "<<(x);}cout<<endl;
#define printVS(vs) cout<<(#vs)<<":"<<endl;for(auto(s):(vs)){cout<<(s)<< endl;}
#define printVV(vv) cout<<(#vv)<<":"<<endl;for(auto(v):(vv)){for(auto(x):(v)){cout<<" "<<(x);}cout<<endl;}
#define printP(p) cout<<(#p)<<(p).first<<" "<<(p).second<<endl;
#define printVP(vp) cout<<(#vp)<<":"<<endl;for(auto(p):(vp)){cout<<(p).first<<" "<<(p).second<<endl;}

inline void output(){ cout << endl; }
template<typename First, typename... Rest>
inline void output(const First& first, const Rest&... rest) {
    cout << first << " "; output(rest...);
}

using ll = long long;
using Pii = pair<int, int>;
using TUPLE = tuple<int, int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
const int inf = 1ll << 60;
const int mod = 1e9 + 7;
using Graph = vector<vector<int>>;

// undirected
void addEdge(Graph& G, int x, int y) {
    G[x].emplace_back(y);
    G[y].emplace_back(x);
}

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

struct Cube {
    Point mi, ma;
    Cube() : mi(inf, inf, inf), ma(-inf, -inf, -inf) {}
    Cube(int x, int y, int z, int s) : mi(inf, inf, inf), ma(-inf, -inf, -inf) {
        update(Point(x, y, z));
        update(Point(x + s, y + s, z + s));
    }
    void update(Point p) {
        chmin(mi.x, p.x); chmin(mi.y, p.y); chmin(mi.z, p.z);
        chmax(ma.x, p.x); chmax(ma.y, p.y); chmax(ma.z, p.z);
    }
    bool inside(Point p) {
        return mi.x <= p.x && p.x <= ma.x && mi.y <= p.y && p.y <= ma.y && mi.z <= p.z && p.z <= ma.z;
    }
    int surface() {
        int wx = ma.x - mi.x;
        int wy = ma.y - mi.y;
        int wz = ma.z - mi.z;
        if (wx <= 0 || wy <= 0 || wz <= 0) return -1;
        return 2 * (wx * wy + wy * wz + wz * wx);
    }
    Point kthPoint(int k) {
        switch(k) {
            case 0: return Point(mi.x, mi.y, mi.z);
            case 1: return Point(mi.x, mi.y, ma.z);
            case 2: return Point(mi.x, ma.y, mi.z);
            case 3: return Point(mi.x, ma.y, ma.z);
            case 4: return Point(ma.x, mi.y, mi.z);
            case 5: return Point(ma.x, mi.y, ma.z);
            case 6: return Point(ma.x, ma.y, mi.z);
            case 7: return Point(ma.x, ma.y, ma.z);
        }
    }
    void print() {
        cerr << "mi: "; output(mi.x, mi.y, mi.z);
        cerr << "ma: "; output(ma.x, ma.y, ma.z);
    }
};

int intersect(Cube c1, Cube c2) {
    Cube c;
    rep(k, 8) {
        if (c2.inside(c1.kthPoint(k))) {
            c.update(c1.kthPoint(k));
        }
    }
    rep(k, 8) {
        if (c1.inside(c2.kthPoint(k))) {
            c.update(c2.kthPoint(k));
        }
    }
    // c.print();
    return c.surface();
}

int n, k, s;
int ma;
Graph G;
vvi overlap;
vi vs;

int sumOverlap() {
    // printV(vs);
    int t = 0;
    rep(i, k - 1) {
        assert(overlap[vs[i]][vs[i + 1]] > 0);
        t += overlap[vs[i]][vs[i + 1]];
    }
    if (k >= 3 && overlap[vs.back()][vs[0]] > 0) {
        // cycle
        t += overlap[vs.back()][vs[0]];
    }
    return t;
}

void dfs(int now, int pre, int start) {
    vs.emplace_back(now);

    if ((int)vs.size() == k) {
        chmax(ma, sumOverlap());
    }
    else {
        for (auto nxt : G[now]) {
            if (nxt == pre || nxt == start) continue;
            dfs(nxt, now, start);
        }
    }

    vs.pop_back();
}

main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    int testcase = 0;
    while (cin >> n >> k >> s, n) {
        // cerr << "--- testcase = " << ++testcase << " ---" << endl;

        vector<Cube> cube;
        rep(i, n) {
            int x, y, z;
            cin >> x >> y >> z;
            cube.emplace_back(x, y, z, s);
        }
        // rep(i, n) {
        //     cerr << i << ":" << endl;
        //     cube[i].print();
        // }

        // if (testcase != 4) continue;

        if (k == 1) {
            cout << 6 * s * s << endl;
            continue;
        }

        G.clear(); G.resize(n);
        overlap.clear(); overlap.resize(n, vi(n));
        rep(i, n) {
            rep2(j, i + 1, n) {
                int t = intersect(cube[i], cube[j]);
                if (t > 0) {
                    addEdge(G, i, j);
                    overlap[i][j] = t;
                    overlap[j][i] = t;
                }
            }
        }
        // printVV(G);
        // printVV(overlap);

        ma = -1;
        rep(i, n) {
            dfs(i, -1, i);
        }

        cout << (ma == -1 ? -1 : k * 6 * s * s - ma) << endl;
    }
}

Measured memory_bytes: 10828