#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }

/*
 <url:>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

int n,k,s;
ll area(ll x1,ll y1,ll z1,ll x2,ll y2,ll z2){
    ll ret = -1;
    ll dX = 0,dY = 0,dZ = 0;
    if(x1 > x2) swap(x1,x2);
    if(x1 <= x2 && x2 <= x1 + s){
        dX = x1 + s - x2;
    }else{
        return ret;
    }
    if(y1 > y2) swap(y1,y2);
    if(y1 <= y2 && y2 <= y1 + s){
        dY = y1 + s - y2;
    }else{
        return ret;
    }
    if(z1 > z2) swap(z1,z2);
    if(z1 <= z2 && z2 <= z1 + s){
        dZ = z1 + s - z2;
    }else{
        return ret;
    }
  //  cout << dX << " " << dY << " " << dZ << endl;
    ret = dX*dY*2 + dY*dZ*2 + dZ*dX*2;
    return ret;
}

bool ok;
void rec(ll n,int cnt,ll Sum,ll pre,vector<vector<pll>>& G,ll& ret,vector<int>& flag){
  //  cout << n << " " << Sum << endl;
    if(cnt == k){
        ok = true;
        ll maxV = 0;
        for(auto next:G[n]){
            if(next.first == pre) continue;
            if(flag[next.first] == 1){
                maxV = max(maxV,next.second);
            }
        }
        ret = max(ret,Sum+maxV);
        return;
    }
    for(auto next:G[n]){
        if(flag[next.first] == 1) continue;
        flag[next.first] = 1;
        rec(next.first,cnt+1,Sum+next.second,n,G,ret,flag);
        flag[next.first] = 0;
    }
}
ll solve(){
    ll res = 0;
    vector<ll> x(n),y(n),z(n);
    for(int i = 0; i < n;i++) cin >> x[i] >> y[i] >> z[i];
    res = s*s*6*k;
    vector<vector<pll>> G(n);
    for(int i = 0; i < n;i++){
        for(int j = i + 1; j < n;j++){
            ll Area = area(x[i],y[i],z[i],x[j],y[j],z[j]);
            if(Area == -1) continue;
          //  cout << i << " -> " << j << " " << Area << endl;
            G[i].push_back({j,Area});
            G[j].push_back({i,Area});
        }
    }
    ll retMax = 0;
    vector<int> flag(n,0);
    ok = false;
    for(int i = 0; i < n;i++){
        flag[i] = 1;
        rec(i,1,0,-1,G,retMax,flag);
        flag[i] = 0;
    }
    if(ok == false) return -1;
  //  cout << res << endl;
   // cout << retMax << endl;
    res -= retMax;
    return res;
}
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    while(cin >> n >> k >> s,n){
        cout << solve() << endl;
    }
    return 0;
}



Measured memory_bytes: 3252