#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define dbg(...) do{cerr<<__LINE__<<": ";dbgprint(#__VA_ARGS__, __VA_ARGS__);}while(0);

using namespace std;

namespace std{template<class S,class T>struct hash<pair<S,T>>{size_t operator()(const pair<S,T>&p)const{return ((size_t)1e9+7)*hash<S>()(p.first)+hash<T>()(p.second);}};template<class T>struct hash<vector<T>>{size_t operator()(const vector<T> &v)const{size_t h=0;for(auto i : v)h=h*((size_t)1e9+7)+hash<T>()(i)+1;return h;}};}
template<class T>ostream& operator<<(ostream &os, const vector<T> &v){os<<"[ ";rep(i,v.size())os<<v[i]<<(i==v.size()-1?" ]":", ");return os;}template<class T>ostream& operator<<(ostream &os,const set<T> &v){os<<"{ "; for(const auto &i:v)os<<i<<", ";return os<<"}";}
template<class T,class U>ostream& operator<<(ostream &os,const map<T,U> &v){os<<"{";for(const auto &i:v)os<<" "<<i.first<<": "<<i.second<<",";return os<<"}";}template<class T,class U>ostream& operator<<(ostream &os,const pair<T,U> &p){return os<<"("<<p.first<<", "<<p.second<<")";}
void dbgprint(const string &fmt){cerr<<endl;}template<class H,class... T>void dbgprint(const string &fmt,const H &h,const T&... r){cerr<<fmt.substr(0,fmt.find(","))<<"= "<<h<<" ";dbgprint(fmt.substr(fmt.find(",")+1),r...);}
typedef long long ll;typedef vector<int> vi;typedef pair<int,int> pi;const int inf = (int)1e9;const double INF = 1e12, EPS = 1e-9;

vector<vector<pi>> build_graph(int n, vi &x, vi &y, vi &z, int s){
	vector<vector<pi>> e(n);
	auto is = [&](int i, int j){
		int dx = s - abs(x[i] - x[j]);
		int dy = s - abs(y[i] - y[j]);
		int dz = s - abs(z[i] - z[j]);
		if(dx <= 0 || dy <= 0 || dz <= 0) return 0;
		return 2 * (dx * dy + dy * dz + dz * dx);
	};
	rep(i, n) rep(j, i){
		int x = is(i, j);
		if(!x) continue;
		e[i].emplace_back(j, x);
		e[j].emplace_back(i, x);
		//dbg(i, j, x);
	}
	return e;
}
vector<pair<bool, vi>> composition(vector<vector<pi>> &e, int n){
	vector<bool> v(n);
	vector<pair<bool, vi>> res;
	function<bool(int,int,vi&)> rec = [&](int c, int p, vi &vs){
		vs.pb(c);
		v[c] = 1;
		bool res = 0;
		for(auto i : e[c]) if(i.first != p){
			if(v[i.first]) res = 1;
			else res |= rec(i.first, c, vs);
		}
		return res;
	};
	rep(i, n) if(!v[i]){
		vi vs;
		bool lp = rec(i, i, vs);
		res.emplace_back(lp, vs);
		//dbg(lp, vs);
	}
	return res;
}
ll calc_loop(vi &vs, vector<vector<pi>> &e){
	ll sum = 0;
	for(int p = -1, c = vs[0], i = 0; i < vs.size(); i++){
		for(auto j : e[c]) if(j.first != p){
			sum += j.second;
			p = c;
			c = j.first;
			break;
		}
	}
	return sum;
}
vi calc_cost(bool loop, vi &vs, vector<vector<pi>> &e){
	vi cost;
	int c = vs[0];
	for(int i : vs) if(e[i].size() == 1) c = i;
	
	for(int p = -1, i = 0; i < (int)vs.size() - !loop; i++){
		int nc = -1;
		for(auto j : e[c]) if(j.first != p){
			cost.pb((int)j.second);
			p = c;
			nc = j.first;
			break;
		}
		assert(nc != -1);
		c = nc;
	}
	if(loop){
		//dbg(cost);
		rep(i, vs.size()) cost.pb(cost[i]);
		//dbg(cost);
	}
	return cost;
}
int main(){
	cin.tie(0); cin.sync_with_stdio(0);
	int n, k, s;
	while(cin >> n >> k >> s, n){
		vi x(n), y(n), z(n);
		rep(i, n) cin >> x[i] >> y[i] >> z[i];
		
		if(k == 1){
			cout << 6ll * k * s * s << endl;
			continue;
		}
		
		vector<vector<pi>> e = build_graph(n, x, y, z, s);
		vector<pair<bool, vi>> comp = composition(e, n);
		ll mx = -1;
		
		for(auto c : comp) if(c.second.size() >= k){
			bool loop = c.first;
			vi vs = c.second;
			if(loop && vs.size() == k) mx = max(mx, calc_loop(vs, e));
			else{
				vi costs = calc_cost(loop, vs, e);
				ll sum = accumulate(costs.begin(), costs.begin() + k - 2, 0ll);
				rep(i, (int)costs.size() - k + 2){
					sum += costs[i + k - 2];
					mx = max(mx, sum);
					sum -= costs[i];
				}
				/*
				dbg(loop, 6 * k * s * s);
				dbg(k, costs);
				rep(j, costs.size()){
					ll sum = 0;
					rep(i, k - 1) if(j + i < costs.size()) sum += costs[j + i];
					else{ sum = -1; break; }
					//dbg(sum, mx);
					mx = max(mx, sum);
				}
				*/
			}
		}
		cout << (mx < 0 ? -1ll : 6ll * k * s * s - mx) << endl;
	}
	return 0;
}

Measured memory_bytes: 3328