#include<bits/stdc++.h>

#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)
#define EPS (1e-8)
#define equals(a,b) (fabs((a)-(b))<EPS)
#define pb push_back

using namespace std;

typedef long long ll;

bool LT(double a,double b) { return !equals(a,b) && a < b; }
bool LTE(double a,double b) { return equals(a,b) || a < b; }

struct Edge {
  int dst;
  long long w;
};

#define MAX 2100
int n,K;
ll s, x[MAX], y[MAX], z[MAX];
vector<Edge> G[MAX];

const bool debug = 0;

long long calc_common_area(ll x1L, ll x1R, ll x2L, ll x2R) {
  if( x1L <= x2L && x2L <= x1R ) return min(x2R,x1R)-x2L;
  if( x1L <= x2R && x2R <= x1R ) return min(x2R,x1R)-x1L;
  return 0;
}

long long calc_area(int a,int b) {
  ll wx = calc_common_area(x[a],x[a]+s,x[b],x[b]+s);
  ll wy = calc_common_area(y[a],y[a]+s,y[b],y[b]+s);
  ll wz = calc_common_area(z[a],z[a]+s,z[b],z[b]+s);
  if( debug ) {
    cout << "(wx,wy,wz) = (" << wx << "," << wy << "," << wz << ")" << endl;
  }
  if( wx == 0LL || wy == 0LL || wz == 0LL ) return 0LL;
  return wx * wy * 2LL + wx * wz * 2LL + wy * wz * 2LL;
}

void add_edge(int a,int b) {
  long long w = calc_area(a,b);
  assert( w >= 0LL );
  if( w == 0 ) return;
  G[a].push_back((Edge){b,w});
  G[b].push_back((Edge){a,w});
}

bool used[MAX];

void dfs(int cur, deque<int> &vs) {
  rep(i,(int)G[cur].size()) {
    Edge &e = G[cur][i];
    if( used[e.dst] ) continue;
    used[e.dst] = true;
    vs.pb(e.dst);
    dfs(e.dst,vs);
  }
}

bool used2[MAX];
void dfs2(int cur, deque<ll> &deq) {
  rep(i,(int)G[cur].size()) {
    Edge &e = G[cur][i];
    if( used2[e.dst] ) continue;
    used2[e.dst] = true;
    deq.pb(e.w);
    dfs2(e.dst,deq);
  }
}


void solve() {
  if( K == 1 ) {
    cout << s * s * 6 << endl;
    return;
  }
  rep(i,n) G[i].clear();
  rep(i,n) {
    REP(j,i+1,n) {
      add_edge(i,j);
    }
  }
  if( debug ) {
    cout << "Graph---" << endl;
    rep(i,n) {
      cout << i << "-th:" << endl;
      cout << "  ";
      rep(j,(int)G[i].size()) {
	cout << "(" << G[i][j].dst << "," << G[i][j].w << ")";
      } puts("");
    }
  }

  ll mini = LLONG_MAX;
  memset(used,false,sizeof used);
  rep(i,n) if( !used[i] ) {
    deque<int> vs;
    vs.pb(i);
    used[i] = true;
    dfs(i,vs);
    if( debug ) {
      cout << "* Start " << i << endl;
      cout << "  --[vs(" << vs.size() << ")]--" << endl;
      cout << "    ";
      rep(kk,(int)vs.size()) {
	cout << vs[kk] << ", ";
      } puts("");
    }

    if( (int)vs.size() < K ) continue;
    if( (int)vs.size() == 1 ) {
      continue;
    }

    int sp = -1;
    rep(j,(int)vs.size()) if( G[vs[j]].size() == 1 ) { sp = j; break; }
    if( debug ) {
      cout << "* sp = " << sp << endl;
    }
    if( sp == -1 ) { // loop
      ll sum = 0;
      int len = K-1;
      if( (int)vs.size() == K ) ++len;
      rep(j,len) {
	int cur = vs[j];
	int nex = vs[(j+1)%(int)vs.size()];
	if( debug ) {
	  cout << "  ? cur = " << cur << ", nex = " << nex << endl;
	}
	ll w = -1;
	rep(k,(int)G[cur].size()) if( G[cur][k].dst == nex ) { w = G[cur][k].w; break; }
	assert( w != -1LL );
	sum += w;
      }
      if( (int)vs.size() == K ) {
	mini = min(mini,s*s*6LL*K-sum);
	continue;
      }
      rep(j,(int)vs.size()) {
	int cur = vs[j];
	int nex = vs[(j+1)%(int)vs.size()];
	ll w = -1;
	rep(k,(int)G[cur].size()) if( G[cur][k].dst == nex ) { w = G[cur][k].w; break; }
	assert( w != -1LL );	

	int cur2 = vs[(j+K-1)%(int)vs.size()];
	int nex2 = vs[(j+K)%(int)vs.size()];
	ll w2 = -1;
	rep(k,(int)G[cur2].size()) if( G[cur2][k].dst == nex2 ) { w2 = G[cur2][k].w; break; }
	assert( w2 != -1LL );
	sum = sum - w + w2;
	mini = min(mini,s*s*6LL*K-sum);	
      }
      
    } else { // chain
      memset(used2,false, sizeof used2);
      used2[vs[sp]] = true;
      deque<ll> deq;
      dfs2(vs[sp],deq);
      if( debug ) {
	cout << "  ** this is chain...!" << endl;
	cout << "    chain :: deq[";
	rep(k,(int)deq.size()) {
	  cout << deq[k] << " ";
	} cout << "]" << endl;
      }
      ll sum = 0;
      rep(k,K-1) sum += deq[k];
      ll maxi = sum;
      rep(k,(int)deq.size()) {
	sum -= deq[k];
	if( k+K-1 < (int)deq.size() && deq[k+K-1] ) {
	  sum += deq[k+K-1];
	  maxi = max(maxi,sum);
	} else break;
      }
      
      if( debug) {
	cout << "  maxi = " << maxi << endl;
      }
      mini = min(mini,s*s*6LL*K-maxi);
    }
    
  }

  if( mini == LLONG_MAX ) puts("-1");
  else cout << mini << endl;
}

int main() {
  while( cin >> n >> K >> s, n | K | s ) {
    rep(i,n) cin >> x[i] >> y[i] >> z[i];
    solve();
  }
  return 0;
}



Measured memory_bytes: 3300