#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>
#include <cstring>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define all(c) begin(c),end(c)

template<typename T1, typename T2>
inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }

template<typename T1, typename T2>
inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }

//改造
typedef long long int ll;
using ll = long long int;
using ull = long long unsigned int;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0 //デバッグする時1にしてね
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007


//ここから編集する
class Solve {
public:
    int n, k;
    ll s;
    vector<ll> X, Y, Z;
    vector<vector<int>> G;

    //共通部分を求める
    tuple <ll, ll, ll> uni(int i, int j) {
        tuple <ll, ll, ll> ret;
        get<0>(ret) = s - abs(X[i] - X[j]);
        get<1>(ret) = s - abs(Y[i] - Y[j]);
        get<2>(ret) = s - abs(Z[i] - Z[j]);

        if (get<0>(ret) < 0 || get<1>(ret) < 0 || get<2>(ret) < 0) {
            return make_tuple(-1ll, -1ll, -1ll);
        }
        return ret;
    }

    // iにjを加えたときの増加する表面積
    ll add_cube(int i, int j) {
        ll ret = 6 * s * s;
        auto tapi = uni(i, j);
        ret -= 2 * get<0>(tapi) * get<1>(tapi);
        ret -= 2 * get<1>(tapi) * get<2>(tapi);
        ret -= 2 * get<2>(tapi) * get<0>(tapi);
        return ret;
    }

    int dstart;

    ll dfs(int v, int cnt = 1, ll square = 0, int p = -1) {
        if (p == -1) dstart = v;

        if (k == 1) {
            return 6 * s * s;
        }
        if (v == dstart && cnt != 1) return INFl;

        if (cnt == k) {
            // dstartと重なっている場合はひく
            ll ret = square;
            if (cnt > 2 && get<0>(uni(dstart, v)) > 0) {
                ret += add_cube(v, dstart);
                ret -= 6 * s * s;
            }
            return ret;
        }

        ll ret = INFl;
        if (cnt == 1) {
            square += 6 * s * s;
        }
        for (auto e : G[v]) {
            if (e == p) continue;
            int nv = e;
            int ncnt = cnt + 1;
            ll nsquare = square + add_cube(v, e);
            int np = v;
            ll tmp = dfs(nv, ncnt, nsquare, np);
            chmin(ret, tmp);
        }

        return ret;
    }

    bool solve() {
        cin >> n >> k >> s;
        if (n == 0 && k == 0 && s == 0) return false;
        G.resize(n);

        for (int i = 0; i < n; ++i) {
            ll x, y, z;
            cin >> x >> y >> z;
            X.push_back(x);
            Y.push_back(y);
            Z.push_back(z);
        }

        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                if (i == j) continue;
                auto tup = uni(i, j);
                if (get<0>(tup) > 0) {
                    G[i].push_back(j);
                }
            }
        }

        ll ans = INFl;
        for (int i = 0; i < n; ++i) {
            ll tmp = dfs(i);
            chmin(ans, tmp);
        }

        if (ans == INFl) {
            cout << -1 << endl;
        } else {
            cout << ans << endl;
        }


        return true;
    }
};


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);

    while (Solve().solve());

    return 0;
}



Measured memory_bytes: 3220