#include<bits/stdc++.h>
using namespace std;
#define MAX_N 2000
#define pb push_back
typedef long long int ll;
int N,K;
ll S;
ll X[MAX_N],Y[MAX_N],Z[MAX_N];
vector<int>G[MAX_N];
vector<vector<int> >object;
vector<vector<ll> >kyoutuu;
bool flag[MAX_N];
ll ans=100000000000000;
int main()
{
	while(true){
		scanf("%d%d%lld",&N,&K,&S);
		if(N==0&&K==0&&S==0LL){break;}
		for(int i=0;i<N;i++){
			scanf("%lld%lld%lld",&X[i],&Y[i],&Z[i]);
		}
		for(int i=0;i<N;i++){
			G[i].clear();
		}
		for(int i=0;i<N;i++){
			flag[i]=1;
		}
		object.clear();
		kyoutuu.clear();
		ans=100000000000000;
		for(int i=0;i<N;i++){
			for(int j=i+1;j<N;j++){
				if(i==j){continue;}
				if(abs(X[i]-X[j])<=S){
					if(abs(Y[i]-Y[j])<=S){
						if(abs(Z[i]-Z[j])<=S){
							G[i].pb(j);
							G[j].pb(i);
						}
					}
				}
			}
		}
		for(int i=0;i<N;i++){
			if(flag[i]==0){continue;}
			if((int)G[i].size()==1){
				vector<int> now;
				now.pb(i);flag[i]=0;
				int fv=i;
				int v=G[i][0];
				now.pb(v);flag[v]=0;
				while(true){
					if((int)G[v].size()==1){
						break;
					}
					else{
						if(G[v][0]!=fv){
							now.pb(G[v][0]);flag[G[v][0]]=0;
							fv=v;
							v=G[v][0];
						}
						else{
							now.pb(G[v][1]);flag[G[v][1]]=0;
							fv=v;
							v=G[v][1];
						}
					}
				}
				object.pb(now);
			}
		}
		for(int i=0;i<N;i++){
			if(flag[i]==0){continue;}
			if((int)G[i].size()==0){
				vector<int>now;
				now.pb(i);flag[i]=0;
				object.pb(now);
			}
		}
		for(int i=0;i<N;i++){
			if(flag[i]==0){continue;}
			if((int)G[i].size()==2){
				vector<int> now;
				now.pb(i);flag[i]=0;
				int fv=i;
				int v=G[i][0];
				now.pb(v);flag[v]=0;
				while(true){
					if(v==i){
						now.erase(now.end()-1,now.end());
						break;
					}
					else{
						if(G[v][0]!=fv){
							now.pb(G[v][0]);flag[G[v][0]]=0;
							fv=v;
							v=G[v][0];
						}
						else{
							now.pb(G[v][1]);flag[G[v][1]]=0;
							fv=v;
							v=G[v][1];
						}
					}
				}
				object.pb(now);
			}
		}
		/*
		for(int i=0;i<N;i++){
			printf("%d:",i);
			for(int j=0;j<(int)G[i].size();j++){
				printf("%d ",G[i][j]);
			}
			printf("\n");
		}
		printf("\n");
		for(int i=0;i<(int)object.size();i++){
			printf("%d:",(int)object[i].size());
			for(int j=0;j<(int)object[i].size();j++){
				printf("%d ",object[i][j]);
			}
			printf("\n");
		}
		*/
		for(int i=0;i<(int)object.size();i++){
			vector<ll> kari;
			for(int j=0;j<((int)object[i].size())-1;j++){
				//printf("%d %d\n",i,j);
				ll x=S-abs(X[object[i][j]]-X[object[i][j+1]]);
				ll y=S-abs(Y[object[i][j]]-Y[object[i][j+1]]);
				ll z=S-abs(Z[object[i][j]]-Z[object[i][j+1]]);
				//printf("%lld %lld %lld\n",x,y,z);
				ll SS=2*x*y+2*y*z+2*z*x;
				//printf("%lld\n",SS);
				kari.pb(SS);
				//printf("%lld\n",SS);
			}
			int os=(int)object[i].size()-1;
			ll x=S-abs(X[object[i][os]]-X[object[i][0]]);
			ll y=S-abs(Y[object[i][os]]-Y[object[i][0]]);
			ll z=S-abs(Z[object[i][os]]-Z[object[i][0]]);
			ll SS=2*x*y+2*y*z+2*z*x;
			if(x<=0||y<=0||z<=0){SS=0;}
			kari.pb(SS);
		//	printf("%lld\n",SS);
			kyoutuu.pb(kari);
		}
		for(int i=0;i<(int)object.size();i++){
			if((int)object[i].size()<K){continue;}
			if((int)G[object[i][0]].size()==1){
				ll sum=0;
				for(int j=0;j<K-1;j++){
					sum+=kyoutuu[i][j];
				}
				ll cou=6*S*S*K-sum;
				ans=min(ans,cou);
				for(int j=K-1;j<(int)object[i].size();j++){
					cou=6*S*S*K;
					sum-=kyoutuu[i][j-K+1];
					sum+=kyoutuu[i][j];
					cou-=sum;
					ans=min(ans,cou);
				}
			}
			else if((int)object[i].size()!=K){
				ll sum=0;
				for(int j=0;j<K-1;j++){
					sum+=kyoutuu[i][j];
				}
				ll cou=6*S*S*K-sum;
				//printf("%lld\n",cou);
				ans=min(ans,cou);
				for(int j=0;j<(int)object[i].size();j++){
					cou=6*S*S*K;
					sum-=kyoutuu[i][j];
					if(j+K-1>=(int)object[i].size()){sum+=kyoutuu[i][j+K-1-(int)object[i].size()];}
					else{sum+=kyoutuu[i][(j+K-1)];}
					cou-=sum;
					ans=min(ans,cou);
				}
			}
			else if((int)object[i].size()!=1){
				ll sum=0;
				for(int j=0;j<K;j++){
					sum+=kyoutuu[i][j];
				}
				ll cou=6*S*S*K-sum;
				ans=min(ans,cou);
			}
			else{
				ll cou=6*S*S*K;
				ans=min(ans,cou);
			}
		}
		
		if(ans==100000000000000){printf("-1\n");}
		else{printf("%lld\n",ans);}
		
		
	}
	return 0;
}



Measured memory_bytes: 3484