#include <algorithm>
#include <array>
#include <cassert>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()

int subcube(int len, int x0, int y0, int z0, int x1, int y1, int z1) {
    int dx = x0 - x1, dy = y0 - y1, dz = z0 - z1;
    int sx = max(min(len + dx, len - dx), 0);
    int sy = max(min(len + dy, len - dy), 0);
    int sz = max(min(len + dz, len - dz), 0);
    return (sx * sy * sz == 0) ? 0 : 2 * (sx * sy + sy * sz + sz * sx);
}

int main(){
    int n, k, s; 
    while(cin >> n >> k >> s, n != 0){
    vector<int> xs(n), ys(n), zs(n);
    REP(i, n) {
        cin >> xs[i] >> ys[i] >> zs[i];
    }

    if (k == 1) {
        cout << s * s * 6 * k << endl;
        continue;
    }

    vector<vector<int>> subc(n, vector<int>(n));
    REP(a, n) {
        REP(b, n) {
            if (a == b) {/*cout << "--\t";*/ continue;}
            subc[a][b] = subcube(s, xs[a], ys[a], zs[a], xs[b], ys[b], zs[b]);
            //cout << subc[a][b] << "\t";
        }
        //cout << endl;
    }

    vector<vector<int>> segs;
    vector<int> used(n, false);

    /// 4 8 12 16 20
    REP(a, n){
        if (used[a]) continue;
        vector<int> seg;
        int p = a; bool ok = true;
        seg.push_back(p);
        used[a] = true;
        while(ok) {
            ok = false;
            REP(b, n) {
                if (subc[p][b] > 0) {
                    if (used[b]) continue;
                    p = b;
                    seg.push_back(p);
                    used[b] = true;
                    ok = true;
                    break;
                }
            }
        }
        reverse(ALL(seg));
        p = a;
        ok = true;
        while(ok) {
            ok = false;
            REP(b, n) {
                //cout << "?" << a << "->" << b << endl;
                if (subc[p][b] > 0) {
                    if (used[b]) continue;
                    p = b;
                    seg.push_back(p);
                    used[b] = true;
                    ok = true;
                    break;
                }
            }
        }
        segs.push_back(seg);
    }

    int res = -1;
    REP(i, segs.size()) {
        const vector<int> seg(segs[i]);
        
        /*cout << "# "; REP(a, seg.size()) {
            cout << seg[a] << " ";
        } cout << endl;*/
        
        const int l = seg.size();
        if (l < k) { continue; }
        if (l == k) {
            int sum = 0;
            if (l == 2) {
                res = max(res, subc[seg[0]][seg[1]]);
                continue;
            }
            REP(a, l) {
                //cout << "+=" << seg[a] << " " << seg[(a+1)%l] << endl;
                //cout << "   : " << subc[seg[a]][seg[(a+1)%l]] << endl;
                sum += subc[seg[a]][seg[(a+1)%l]];
            }
            res = max(res, sum);
            continue;
        }
        int sum = 0;
        REP(a, k - 1) {
            sum += subc[seg[a]][seg[a+1]];
        }
        REP(a, l) {
            res = max(res, sum);
            int ad = subc[seg[(a + k - 1) % l]][seg[(a + k) % l]];
            if (ad == 0) break;
            sum += ad;
            sum -= subc[seg[(a % l)]][seg[(a + 1) % l]];
        }
    }

    // cout << "$" << subc[0][1] << endl;
    // cout << res << endl;
    if (res < 0) cout << res << endl;
    else cout << s * s * 6 * k - res << endl;
    }
}



Measured memory_bytes: 6844