#include <iostream>
#include <algorithm>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <string>
#include <queue>
#include <deque>
#include <list>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <bitset>
#include <random>
#include <complex>
#include <assert.h>

using namespace std;
typedef long long ll;
// #define endl '\n'

template<typename T>
inline bool chmin(T& x, T a) {
	if (x >= a) { x = a; return true; }
	return false;
}

template<typename T>
inline bool chmax(T& x, T a) {
	if (x <= a) { x = a; return true; }
	return false;
}

ll s;

struct point3D {
	ll x, y, z;
};
bool cross(point3D& a, point3D& b) {
	ll p = abs(a.x - b.x);
	ll q = abs(a.y - b.y);
	ll r = abs(a.z - b.z);
	if (max(p, max(q, r)) < s) {
		return true;
	}
	return false;
}

ll score(point3D& a, point3D& b) {
	ll p = abs(a.x - b.x);
	ll q = abs(a.y - b.y);
	ll r = abs(a.z - b.z);

	p = s - p;
	q = s - q;
	r = s - r;
	return (p * q + q * r + r * p) * 2;
}

int main() {
	while(1) {
		int N, K; cin >> N >> K;
		cin >> s;

		if (N == 0) { break; }

		vector<point3D> v(N);
		for (int i = 0; i < N; i++) {
			cin >> v[i].x >> v[i].y >> v[i].z;
		}

		ll ans = -1000000;

		vector<vector<pair<int, ll>>> next(N);

		for (int i = 0; i < N; i++) {
			for (int j = 0; j < i; j++) {
				if (cross(v[i], v[j])) {
					ll x = score(v[i], v[j]);
					next[i].push_back({ j, x });
					next[j].push_back({ i, x });
				}
			}
		}
		

		vector<bool> used(N, false);
		vector<bool> checked(N, false);

		for (int _i = 0; _i < N; _i++) {
			if (used[_i] == false) {
				if (next[_i].size() == 0) {
					if (K == 1) { chmax(ans, 0LL); }
					used[_i] = true; continue;
				}
				int j, k;
				if (next[_i].size() == 1) {
					j = _i;
					k = next[_i][0].first;
				}
				else {
					j = next[_i][0].first;
					k = _i;
					checked[_i] = true;
					while (1) {
						if (next[j].size() == 1) { break; }
						checked[j] = true;
						if (next[j][0].first == k) {
							k = j;
							j = next[j][1].first;
						}
						else{
							k = j;
							j = next[j][0].first;
						}
						if (checked[j] == true) { break; }
					}
				}
				vector<int> arr;
				arr.push_back(j);
				{
					int tmp = j;
					j = next[j][0].first;
					k = tmp;
				}
				while (1) {
					arr.push_back(j); used[j] = true;
					if (next[j].size() == 1) { break; }
					int x;
					if (next[j][0].first == k) { x = next[j][1].first; }
					else { x = next[j][0].first; }
					if (x == arr[0]) { break; }

					k = j;
					j = x;
				}

				int n = arr.size();
				if (n < K) { continue; }

				if (next[arr[0]].size() == 1) {// straight
					ll x = 0;
					for (int i = 1; i < K; i++) {
						x += score(v[arr[i - 1]], v[arr[i]]);
					}
					ll y = x;
					for (int i = K; i < n; i++) {
						y += score(v[arr[i - 1]], v[arr[i]]) - score(v[arr[i - K + 1]], v[arr[i - K]]);
						chmax(x, y);
					}
					chmax(ans, x);
				}
				else if (n == K) {
					ll x = 0;
					for (int i = 0; i < n; i++) {
						x += score(v[arr[i]], v[arr[(i + 1) % n]]);
					}
					chmax(ans, x);
				}
				else {
					ll x = 0;
					for (int i = 1; i < K; i++) {
						x += score(v[arr[i - 1]], v[arr[i]]);
					}
					ll y = x;
					for (int i = K; i <= n + K; i++) {
						y += score(v[arr[(i - 1) % n]], v[arr[i % n]]) - score(v[arr[(i - K + 1) % n]], v[arr[(i - K) % n]]);
						chmax(x, y);
					}
					chmax(ans, x);
				}
			}
		}

		if (ans < 0) {
			cout << -1 << endl; continue;
		}
		cout << (s * s * 6 * K) - ans << endl;
	}

	return 0;
}


Measured memory_bytes: 3148