#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
using namespace std;

#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define all(v) begin(v), end(v)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 2000000000
#define int long long int

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) ( EQ((a).X, (b).X) && EQ((a).Y, (b).Y) )
#define LE(n, m) ((n) < (m) + EPS)
#define LEQ(n, m) ((n) <= (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define GEQ(n, m) ((n) + EPS >= (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;

int dx[] = {1, 1, 0, 0, 1, 1, 0, 0};
int dy[] = {1, 0, 1, 0, 1, 0, 1, 0};
int dz[] = {0, 0, 0, 0, 1, 1, 1, 1};
int const MOD = 1000000007;

namespace std {
    bool operator<(const P& a, const P& b) {
        return a.X != b.X ? a.X < b.X : a.Y < b.Y;
    }
}

// Union-Find ??¨ (Verified: AtCoder Typical Contest 001 B)
struct UnionFind {
private:
    const int n;
    int __size;
    vector<int> uf;
public:
    // ????????? UnionFind uni(n) ??????????????£?¨?????????°??????
    UnionFind(int _n) : n(_n), __size(_n), uf(_n, -1) {}
    // find (??¨??????????±???????)
    int find(int x) {return (uf[x] < 0) ? x : uf[x] = find(uf[x]);}
    // x ??¨ y ???????????????????±???????????????????
    bool same(int x, int y) {return find(x) == find(y);}
    // x ????±????????????????????´???°
    int size(int x) {return -uf[find(x)];}
    // ???????????????????????????
    int size()      {return __size;}
    // x ??¨ y ????±??????????????????????
    void unite(int x, int y) {
        x = find(x); y = find(y);
        if(x == y) return;
        __size--;
        if(uf[y] < uf[x]) swap(x, y);
        uf[x] += uf[y]; uf[y] = x;
    }
};

// ?§?????????¨????????¨???????????????????¨???????????§???????
template <typename T>
struct Edge {
    int from, to;
    T cost;
    Edge(int s, T d) : to(s), cost(d) {}
    Edge(int f, int s, T d) : from(f), to(s), cost(d) {}

    bool operator<(const Edge &e) const {
        return cost < e.cost;
    }
    bool operator>(const Edge &e) const {
        return cost > e.cost;
    }
};
#define Graph(type) vector< vector< Edge<type> > >

bool incube(int a, int b, int x) {
    return a <= x && x <= b;
}

int n, k, s;
int x[1010], y[1010], z[1010];
int temp, ans;
void dfs(int p, Graph(int) &G, set<pii> S, set<int> T, int counter, int cur) {
    T.insert(p);
    rep(i,0,G[p].size()) {
        int next = G[p][i].to;
        if(counter == k && !T.count(next)) continue;
        int mi = min(p, next), ma = max(p, next);
        pii pr = pii(mi, ma);
        if(S.count(pr)) continue;
        int xx, yy, zz;
        xx = min(x[p], x[next]) - max(x[p], x[next]) + s;
        yy = min(y[p], y[next]) - max(y[p], y[next]) + s;
        zz = min(z[p], z[next]) - max(z[p], z[next]) + s;
        S.insert(pr);
        cur -= 2 * (xx * yy + yy * zz + zz * xx);
        // printf("p = %lld, next = %lld, cur = %lld\n", p, next, cur);
        dfs(next, G, S, T, counter+1, cur);
        S.erase(pr);
        cur += 2 * (xx * yy + yy * zz + zz * xx);
    }
    if(counter >= k) {
        // printf("cur = %lld\n", cur);
        ans = min(ans, cur);
        return;
    }
}

signed main() {
    while(cin >> n >> k >> s, n || k || s) {
        UnionFind uf(n);
        Graph(int) G(n);
        rep(i,0,n) {
            cin >> x[i] >> y[i] >> z[i];
        }
        set<pii> edgepair;
        rep(i,0,n) rep(j,0,n) {
            if(i == j) continue;
            pii temppair = pii( min(i,j), max(i,j) );
            if(edgepair.count(temppair)) continue;
            bool ok = false;
            rep(k,0,8) {
                if(ok) break;
                if(incube(x[i], x[i]+s, x[j]+dx[k]*s) && incube(y[i], y[i]+s, y[j]+dy[k]*s) && incube(z[i], z[i]+s, z[j]+dz[k]*s)) {
                    // printf("incube: i = %lld, j = %lld\n", i, j);
                    uf.unite(i, j);
                    G[i].pb(Edge<int>(j,1));
                    G[j].pb(Edge<int>(i,1));
                    edgepair.insert(temppair);
                    ok = true;
                }
            }
        }

        ans = (int)1e18;
        rep(i,0,n) {
            // printf("uf.size(%lld) = %lld\n", i, uf.size(i));
            if(uf.size(i) >= k) {
                temp = 6 * s * s * k;
                set<pii> S;
                set<int> T;
                dfs(i, G, S, T, 1, temp);
            }
        }
        if(ans != (int)1e18) cout << ans << endl;
        else cout << -1 << endl;
    }
    return 0;
}

Measured memory_bytes: 5388