#include <bits/stdc++.h>
using namespace std;

using ll = long long;

constexpr ll INF = 1e18;

struct edge {
    int to;
    ll cost;
};

using edges = vector<edge>;
using graph = vector<edges>;

int main() {
    ll n, k, s;
    while(cin >> n >> k >> s, n) {
        vector<ll> x(n), y(n), z(n);
        for(int i=0; i<n; ++i) {
            cin >> x[i] >> y[i] >> z[i];
        }
        if(k == 1) {
            cout << s*s*6 << endl;
            continue;
        }
        graph g(n);
        for(int i=0; i<n; ++i) {
            for(int j=i+1; j<n; ++j) {
                ll dx = abs(x[i] - x[j]), dy = abs(y[i] - y[j]), dz = abs(z[i] - z[j]);
                if(dx <= s && dy <= s && dz <= s) {
                    ll S = ((s - dx)*(s - dy) + (s - dy)*(s - dz) + (s - dz)*(s - dx)) * 2;
                    g[i].push_back(edge{j, -S});
                    g[j].push_back(edge{i, -S});
                }
            }
        }
        vector<bool> chain(n);
        vector<bool> cycle(n, true);
        for(int i=0; i<n; ++i) {
            if(g[i].size() == 1) {
                chain[i] = true;
                cycle[i] = false;
                int now = i, prev = -1;
                bool update = true;
                while(update) {
                    update = false;
                    for(auto& e : g[now]) {
                        if(prev != e.to) {
                            update = true;
                            prev = now;
                            now = e.to;
                            chain[now] = cycle[now] = false;
                            break;
                        }
                    }
                }
            }
        }
        ll res = INF;
        vector<int> cnt(n);
        for(int i=0; i<n; ++i) {
            if(chain[i] || cycle[i]) {
                if(cnt[i]) {
                    continue;
                }
                cnt[i] = 1;
                deque<ll> usedv;
                deque<ll> cost;
                int now = i, prev = -1;
                usedv.push_back(now);
                ll t = s * s * 6;
                bool update = true;
                while(true) {
                    if(usedv.size() == k) {
                        res = min(res, t);
                    }
                    if(!update) {
                        break;
                    }
                    update = false;
                    for(auto& e : g[now]) {
                        if(prev != e.to) {
                            if(usedv.size() == k && e.to == usedv.front()) {
                                t += e.cost;
                                break;
                            }
                            usedv.push_back(e.to);
                            t += s * s * 6 + e.cost;
                            cost.push_back(s*s*6 + e.cost);
                            if(usedv.size() > k) {
                                usedv.pop_front();
                                t -= cost.front();
                                cost.pop_front();
                            }
                            prev = now;
                            now = e.to;
                            cnt[now] += 1;
                            update = cnt[usedv.front()] <= 1;
                            break;
                        }
                    }
                }
            }
        }
        cout << (res == INF ? -1 : res) << endl;
    }
}


Measured memory_bytes: 3144