#include<iomanip>
#include<limits>
#include<thread>
#include<utility>
#include<iostream>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<numeric>
#include<cassert>
#include<random>
#include<chrono>
#include<unordered_set>
#include<unordered_map>
#include<fstream>
#include<list>
#include<functional>
#include<bitset>
#include<complex>
#include<tuple>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pi;
typedef pair<double,double> pd;
typedef pair<double,ll> pdl;
#define F first
#define S second
const ll E=1e18+7;
const ll MOD=1000000007;

ll n,k,s;
vector<bool> used;
vector<pair<pll,ll>> A;

vector<ll> Ring;
vector<ll> cnt;
ll ans=-E;

ll cross(ll a,ll b){
    ll x=abs(A[a].F.F-A[b].F.F);
    ll y=abs(A[a].F.S-A[b].F.S);
    ll z=abs(A[a].S-A[b].S);
    if(x>=s || y>=s || z>=s){return 0;}
    if(A[a]==A[b]){return 6*s*s;}
    return ((s-x)*(s-y)+(s-x)*(s-z)+(s-y)*(s-z))*2;
}


void dfs(ll w,ll p=-1){
    Ring.push_back(w);
    used[w]=true;
    for(int i=0;i<n;i++){
        if(i==w || i==p || used[i]){continue;}
        if(cross(i,w)){dfs(i,w);}
    }
}

void solve(ll w){
    Ring.clear();
    dfs(w);
    if(Ring.size()<k){return;}
    if(k==1){ans=0; return;}
    if(cross(Ring[0],Ring.back())){
        if(Ring.size()==k){
            ll ret=0;
            for(int i=1;i<k;i++){ret+=cross(Ring[i],Ring[i-1]);}
            if(k!=2){
                ret+=cross(Ring[0],Ring.back());
            }
            ans=max(ans,ret);
            return;
        }
        else{
            for(int i=0;i<Ring.size();i++){
                ll ret=0;
                for(int t=1;t<k;t++){
                    ret+=cross(Ring[(i+t)%Ring.size()],Ring[(i+t-1)%Ring.size()]);
                }
                ans=max(ans,ret);
            }
            return;
        }
    }
    else{
        for(int i=0;i+k<=Ring.size();i++){
            ll ret=0;
            for(int t=1;t<k;t++){
                ret+=cross(Ring[i+t],Ring[i+t-1]);
            }
            ans=max(ans,ret);
        }
    }
}



int main(){
    while(cin>>n>>k>>s){
        if(n==0){break;}
    used.resize(n);
    for(int i=0;i<n;i++){used[i]=false;}
    A.resize(n);
    cnt.resize(n);
    for(int i=0;i<n;i++){cnt[i]=0;}
    for(auto &I:A){cin>>I.F.F>>I.F.S>>I.S;}
    for(int i=0;i<n;i++){
        for(int t=0;t<n;t++){
            if(i!=t){cnt[i]+=(cross(i,t)?1:0);}
        }
    }
    ans=-E;
    for(int i=0;i<n;i++){
        if(!used[i] && cnt[i]<=1){solve(i);}
    }
    for(int i=0;i<n;i++){
        if(!used[i]){solve(i);}
    }
    if(ans==-E){cout<<-1<<endl;}
    else{cout<<s*s*6*k-ans<<endl;}
    }
    
    return 0;
}

