#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <cassert>
using namespace std;
using ll = long long;

struct Cube{
    ll lx, rx;
    ll ly, ry;
    ll lz, rz;
    ll S(){
        ll dx = rx - lx;
        ll dy = ry - ly;
        ll dz = rz - lz;
        return (dx * dy + dy * dz + dz * dx) * 2;
    } 
};

ll f(Cube c1, Cube c2){
    ll LX, LY, LZ, RX, RY, RZ;
    LX = max(c1.lx, c2.lx);
    RX = min(c1.rx, c2.rx);
    LY = max(c1.ly, c2.ly);
    RY = min(c1.ry, c2.ry);
    LZ = max(c1.lz, c2.lz);
    RZ = min(c1.rz, c2.rz);
    if(RX <= LX) return 0;
    if(RY <= LY) return 0;
    if(RZ <= LZ) return 0;
    ll dx = RX - LX;
    ll dy = RY - LY;
    ll dz = RZ - LZ;
    return (dx * dy + dy * dz + dz * dx) * 2;
}

int n, k, s;
Cube c[2000];
int vis[2000];

vector<int> G[2000];

vector<vector<int>> C;// ????????????
vector<vector<int>> S;// ?????¬???

// ????????????????????????
void dfs(int v, int p, vector<int>& a){
    vis[v] = 1;
    a.push_back(v);
    for(int c : G[v]){
        if(c == p) continue;
        if(vis[c]) continue;
        dfs(c, v, a);
        return;
    }
    return;
}

// ??±?????¢???;
ll X[2000][2000];

int main(){
    while(cin >> n >> k >> s, n){
        for(int i = 0; i < n; i++){
            cin >> c[i].lx  >> c[i].ly >> c[i].lz;
            c[i].rx = c[i].lx + s;
            c[i].ry = c[i].ly + s;
            c[i].rz = c[i].lz + s;
            G[i].clear();
        }
        C.clear();
        S.clear();

        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                if(i == j) continue;
                ll res = f(c[i], c[j]);
                X[i][j] = res;
                if(res) G[i].push_back(j);
            }
        }

        memset(vis, 0, sizeof vis);
        for(int i = 0; i < n; i++){
            if(vis[i]) continue;
            if(G[i].size() <= 1){
                vector<int> a;
                dfs(i, -1, a);
                if(a.size() < k) continue;
                S.push_back(a);
            }   
        }
        for(int i = 0; i < n; i++){
            if(vis[i]) continue;
            assert(G[i].size() <= 2);
            vector<int> a;
            dfs(i, -1, a);
            if(a.size() < k) continue;
            C.push_back(a);
        }

        ll ans = 1LL << 61;
        for(auto a : C){
            int sz = a.size();
            for(int i = 0; i < sz; i++) a.push_back(a[i]);
            ll sum = 0;
            for(int i = 0; i < k; i++){
                sum += c[a[i]].S();
                if(i != k - 1 || k == sz) sum -= X[a[i]][a[i + 1]];
            }
            for(int i = 0; i < sz && sz != k; i++){
                ans = min(ans, sum);
                sum -= c[a[i]].S();
                sum += X[a[i]][a[i+1]];
                sum += c[a[i+k]].S();
                sum -= X[a[i+k-1]][a[i+k]];
            }
            ans = min(ans, sum);
        }
        for(auto a : S){
            ll sum = 0;
            for(int i = 0; i < k; i++){
                sum += c[a[i]].S();
                if(i != k - 1) sum -= X[a[i]][a[i + 1]];
            }
            for(int i = 0; i < a.size() - k; i++){
                ans = min(ans, sum);
                sum -= c[a[i]].S();
                sum += X[a[i]][a[i+1]];
                sum += c[a[i+k]].S();
                sum -= X[a[i+k-1]][a[i+k]];
            }
            ans = min(ans, sum);
        }
        if(ans == 1LL << 61) ans = -1;
        cout << ans << endl;
       // cout << ans << " " << n << " " << S.size() << " "  << C.size() << " " <<  k << endl;
    }
}

Measured memory_bytes: 14960