#include <stdio.h>
#include <algorithm>
#include <vector>
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define sc second
typedef pair<int,int> P;
int n,k,s;
int x[2005],y[2005],z[2005];
vector<P>edge[2005];
bool used[2005];
int main(){
    while(1){
        cin >> n >> k >>s; if(n==0) return 0;
        for(int i=1;i<=n;i++){
            cin >> x[i] >> y[i] >>z[i];
        }
        for(int i=1;i<=n;i++) edge[i].clear();
        int ans = -1;
        for(int i=1;i<=n;i++){
            for(int j=i+1;j<=n;j++){
                int X = abs(x[i]-x[j]);
                int Y = abs(y[i]-y[j]);
                int Z = abs(z[i]-z[j]);
                if(X>=s || Y>=s || Z>=s) continue;
                X = s-X; Y = s-Y; Z = s-Z;
                edge[i].pb(mp(j,2*X*Y+2*Y*Z+2*Z*X));
                edge[j].pb(mp(i,2*X*Y+2*Y*Z+2*Z*X));
                //cout <<i <<" " << j <<endl;
            }
        }
        if(k==1){
            cout << 6*s*s <<endl; continue;
        }
        memset(used,0,sizeof(used));
        //path
        for(int i=1;i<=n;i++){
            if(used[i] || edge[i].size() != 1) continue;
            int cur = i;
            vector<int>cs;
            while(!used[cur]){
                //cout<<cur<<endl;
                used[cur] = 1;
                for(int k=0;k<edge[cur].size();k++){
                    if(used[edge[cur][k].fi]) continue;
                    cs.pb(edge[cur][k].sc); cur = edge[cur][k].fi; break;
                }
            }
            //for(int i=0;i<cs.size();i++) cout << cs[i] <<" "; cout <<endl;
            if(cs.size() < k-1) continue;
            int sum = 0;
            for(int i=0;i<cs.size();i++){
                sum += cs[i];
                if(i>=k-2){
                    ans = max(ans,sum);
                    sum -= cs[i-k+2];
                }
            }
        }
        //cycle
        for(int i=1;i<=n;i++){
            if(used[i]) continue;
            int cur = i;
            vector<int>cs;
            while(!used[cur]){
                used[cur] = 1;
                for(int k=0;k<edge[cur].size();k++){
                    if(used[edge[cur][k].fi]) continue;
                    cs.pb(edge[cur][k].sc); cur = edge[cur][k].fi; goto nxt;
                }
                for(int k=0;k<edge[cur].size();k++){
                    if(edge[cur][k].fi == i){
                        cs.pb(edge[cur][k].sc); break;
                    }
                }
            nxt:;
            }
            //if(n==4) for(int i=0;i<cs.size();i++) cout << cs[i] <<" "; cout <<endl;
            int sum = 0;
            if(cs.size() < k) continue;
            if(cs.size()==k){
                for(int i=0;i<k;i++) sum+=cs[i];
                ans = max(ans,sum);
            }
            else{
                for(int i=0;i<2*cs.size();i++){
                    sum += cs[i%cs.size()];
                    if(i>=k-2){
                        ans = max(ans,sum);
                        sum -= cs[(i-k+2)%cs.size()];
                    }
                }	
            }
		}
		
            if(ans==-1)puts("-1");
            else cout << k*6*s*s-ans <<endl;
    }
}

Measured memory_bytes: 3184