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

#define fs first
#define sc second

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }


// std::vector Declaration
template<typename T>
vector<T> make_v(size_t a) { return vector<T>(a); }
template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts) {
    return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

// std::vector Initialization
template<typename T>
void fill_all(T& arr, const T& v) { arr = v; }
template<typename T, typename U>
void fill_all(T& arr, const U& v) { for (auto& i : arr) fill_all(i, v); }

// std::vector Debug
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    os << "[";
    bool a = 1;
    for (auto e : v ) {
        os << (a ? "" : " ");
        os << e;
        a = 0;
    }
    os << "]";
    return os;
}

// std::pair Debug
template<typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    os << "(" << p.first << " " << p.second << ")";
    return os;
}

// std::set Debug
template<typename T>
ostream& operator<<(ostream& os, const set<T>& st) {
    os << "{";
    bool a = 1;
    for (auto e : st) {
        os << (a ? "" : " ");
        os << e;
        a = 0;
    }
    os << "}";
    return os;
}

// std::multiset Debug
template<typename T>
ostream& operator<<(ostream& os, const multiset<T>& st) {
    os << "{";
    bool a = 1;
    for (auto e : st) {
        os << (a ? "" : " ");
        os << e;
        a = 0;
    }
    os << "}";
    return os;
}

// std::map Debug
template<typename T, typename U>
ostream& operator<<(ostream& os, const map<T, U>& mp) {
    os << "{";
    bool a = 1;
    for (auto e : mp) {
        os << (a ? "" : " ");
        os << e.first << ":" << e.second;
        a = 0;
    }
    os << "}";
    return os;
}

// std::tuple Debug
template<int N, class Tuple>
void out(ostream& os, const Tuple& t){}
template<int N, class Tuple, class H, class ...Ts>
void out(ostream& os, const Tuple& t) {
    if (N) os << " ";
    os << get<N>(t);
    out<N+1,Tuple,Ts...>(os, t);
}
template<class ...Ts>
ostream& operator<<(ostream& os, const tuple<Ts...>& t) {
    os << "(";
    out<0,tuple<Ts...>,Ts...>(os, t);
    os << ")";
    return os;
}

// Debug
#define DUMP(x) cerr<<#x<<" = "<<(x)<<endl


struct edge { int to, cost; };
ostream& operator<<(ostream& os, const edge& e) {
    os << "(" << e.to << ", " << e.cost << ")";
    return os;
}

const LL LINF = 1LL<<60;
const int IINF = 1<<30;
const LL MOD = 1e9+7;

void dfs(const auto& G, auto& visited, auto& costs, int v, int p, bool& cyclic) {
    if (visited[v]) {
        cyclic = true;
        return;
    }
    visited[v] = true;
    for (auto e : G[v]) {
        if (e.to == p) continue;
        costs.push_back(e.cost);
        dfs(G, visited, costs, e.to, v, cyclic);
        break;
    }
}

int moveToLeaf(const auto& G, auto& visited, int v, int p) {
    if (visited[v]) {
        return v;
    }
    visited[v] = true;
    int ret = v;
    for (auto e : G[v]) {
        if (e.to == p) continue;
        ret = moveToLeaf(G, visited, e.to, v);
    }
    return ret;
}

void getCosts(const auto& G, auto& visited, auto& costs, int v, bool& cyclic)
{
    // cyclicじゃないときに, 葉に移動
    map<int,bool> tmpvisited;
    v = moveToLeaf(G, tmpvisited, v, -1);

    dfs(G, visited, costs, v, -1, cyclic);
}

int main()
{
    vector<int> ns, ks, ss;
    vector<vector<int>> xs, ys, zs;

    while (true) {
        int n, k, s; cin >> n >> k >> s;
        if (n == 0 && k == 0 && s == 0) {
            break;
        }
        vector<int> x(n), y(n), z(n);
        for (int i = 0; i < n; ++i) {
            cin >> x[i] >> y[i] >> z[i];
        }

        ns.push_back(n);
        ks.push_back(k);
        ss.push_back(s);
        xs.push_back(x);
        ys.push_back(y);
        zs.push_back(z);
    }

    for (int t = 0; t < ns.size(); ++t) {
        int n = ns[t], k = ks[t], s = ss[t];
        auto x = xs[t], y = ys[t], z = zs[t];

        vector<vector<edge>> G(n);

        for (int i = 0; i < n; ++i) {
            for (int j = i+1; j < n; ++j) {
                int 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) {
                    int X = s - dx,
                        Y = s - dy,
                        Z = s - dz;
                    int cost = 2 * (X*Y + Y*Z + Z*X);
                    G[i].push_back(edge{ j, cost });
                    G[j].push_back(edge{ i, cost });
                }
            }
        }

        LL Max = -1;

        vector<bool> visited(n, false);
        for (int i = 0; i < n; ++i) {
            if (visited[i]) {
                continue;
            }
            vector<int> costs;
            bool cyclic = false;
            getCosts(G, visited, costs, i, cyclic);

            if (cyclic) {
                int m = costs.size();
                if (m < k-1) {
                    continue;
                }
                vector<LL> sum(m+1);
                for (int j = 0; j < m; ++j) {
                    sum[j+1] = sum[j] + costs[j];
                }

                if (m == k) {
                    chmax(Max, sum[m]);
                }

                for (int j = 0; j+k-1 <= m; ++j) {
                    chmax(Max, sum[j+k-1] - sum[j]);
                }
                for (int j = 0; j <= k-1; ++j) {
                    chmax(Max, sum[j] + (sum[m] - sum[m-(k-1-j)]));
                }
            }
            else {
                int m = costs.size();
                if (m < k-1) {
                    continue;
                }
                vector<LL> sum(m+1);
                for (int j = 0; j < m; ++j) {
                    sum[j+1] = sum[j] + costs[j];
                }

                for (int j = 0; j+k-1 <= m; ++j) {
                    chmax(Max, sum[j+k-1] - sum[j]);
                }
            }
        }
        if (Max == -1) {
            cout << -1 << endl;
        } else {
            LL ans = s*s*6 * k - Max;
            cout << ans << endl;
        }
    }

    return 0;
}



Measured memory_bytes: 3292