#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl
#define svec(v) cout<<#v<<":";rep(pachico,v.size())cout<<" "<<v[pachico];cout<<endl
#define svecp(v) cout<<#v<<":";each(pachico,v)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl
#define sset(s) cout<<#s<<":";each(pachico,s)cout<<" "<<pachico;cout<<endl
#define smap(m) cout<<#m<<":";each(pachico,m)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

struct edge
{
    int to;
    ll cost;
};

struct data
{
    ll x,y,z;
};

void dfs(int u,int p,int& vc, int& ed, vector<int>& visit, vector<vector<edge> >& G, vector<int>& v)
{
    v.pb(u);
    visit[u] = vc++;
    each(e,G[u]){
        if(visit[e.to] < 0){
            ed++;
            dfs(e.to,u,vc,ed,visit,G,v);
        }else if(visit[e.to] < visit[u] && e.to != p){
            ed++;
        }
    }
}

ll solve(ll x,ll y,ll z)
{
    return 2*(x*y+y*z+z*x);
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(1){
        int n,K;
        ll s;
        cin >> n >> K >> s;
        if(n == 0) break;
        vector<vector<edge> > G(n);
        vector<data> vec(n);
        rep(i,n){
            ll x,y,z;
            cin >> x >> y >> z;
            vec[i] = (data){x,y,z};
        }
        if(K == 1){
            cout << 6*s*s*K << "\n";
            continue;
        }
        rep(i,n){
            srep(j,i+1,n){
                data& p = vec[i], q = vec[j];
                if(p.x <= q.x){
                    if(q.x < p.x+s){
                        if(p.y <= q.y){
                            if(q.y < p.y+s){
                                if(p.z <= q.z){
                                    if(q.z < p.z+s){
                                        ll val = solve(p.x+s-q.x,p.y+s-q.y,p.z+s-q.z);
                                        G[i].pb((edge){j,val}), G[j].pb((edge){i,val});
                                    }
                                }else{
                                    if(p.z < q.z+s){
                                        ll val = solve(p.x+s-q.x,p.y+s-q.y,q.z+s-p.z);
                                        G[i].pb((edge){j,val}), G[j].pb((edge){i,val});
                                    }
                                }
                            }
                        }else{
                            if(p.y < q.y+s){
                                if(p.z <= q.z){
                                    if(q.z < p.z+s){
                                        ll val = solve(p.x+s-q.x,q.y+s-p.y,p.z+s-q.z);
                                        G[i].pb((edge){j,val}), G[j].pb((edge){i,val});
                                    }
                                }else{
                                    if(p.z < q.z+s){
                                        ll val = solve(p.x+s-q.x,q.y+s-p.y,q.z+s-p.z);
                                        G[i].pb((edge){j,val}), G[j].pb((edge){i,val});
                                    }
                                }
                            }
                        }
                    }
                }else{
                    if(p.x < q.x+s){
                        if(p.y <= q.y){
                            if(q.y < p.y+s){
                                if(p.z <= q.z){
                                    if(q.z < p.z+s){
                                        ll val = solve(q.x+s-p.x,p.y+s-q.y,p.z+s-q.z);
                                        G[i].pb((edge){j,val}), G[j].pb((edge){i,val});
                                    }
                                }else{
                                    if(p.z < q.z+s){
                                        ll val = solve(q.x+s-p.x,p.y+s-q.y,q.z+s-p.z);
                                        G[i].pb((edge){j,val}), G[j].pb((edge){i,val});
                                    }
                                }
                            }
                        }else{
                            if(p.y < q.y+s){
                                if(p.z <= q.z){
                                    if(q.z < p.z+s){
                                        ll val = solve(q.x+s-p.x,q.y+s-p.y,p.z+s-q.z);
                                        G[i].pb((edge){j,val}), G[j].pb((edge){i,val});
                                    }
                                }else{
                                    if(p.z < q.z+s){
                                        ll val = solve(q.x+s-p.x,q.y+s-p.y,q.z+s-p.z);
                                        G[i].pb((edge){j,val}), G[j].pb((edge){i,val});
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        ll mx = -1;
        vector<int> visit(n,-1);
        rep(i,n){
            if(visit[i] < 0){
                int vc = 0, ed = 0;
                vector<int> v;
                dfs(i,-1,vc,ed,visit,G,v);
                if(vc < K){
                    continue;
                }
                // cout << vc << " " << ed << " " << K << "\n";
                int st = i, p = -1;
                if(vc != ed){
                    each(j,v){
                        if(len(G[j]) == 1){
                            st = j;
                            break;
                        }
                    }
                    assert(st >= 0);
                    int cnt = 0;
                    deque<ll> deq;
                    ll val = 0;
                    bool ok = true;
                    while(ok){
                        ok = false;
                        cnt++;
                        each(e,G[st]){
                            if(e.to != p){
                                if(cnt+1 > K){
                                    val -= deq.front();
                                    deq.pop_front();
                                }
                                deq.pb(e.cost);
                                val += e.cost;
                                cmx(mx,val);
                                p = st;
                                st = e.to;
                                ok = true;
                                break;
                            }
                        }
                    }
                    cmx(mx,val);
                }else{
                    int cnt = 0;
                    deque<ll> deq;
                    ll val = 0;
                    int ad = 0;
                    while(ad < vc){
                        cnt++;
                        each(e,G[st]){
                            if(e.to != p){
                                deq.pb(e.cost);
                                val += e.cost;
                                if(cnt+1 > K){
                                    if(vc == K) cmx(mx,val);
                                    ad++;
                                    val -= deq.front();
                                    deq.pop_front();
                                }
                                cmx(mx,val);
                                p = st;
                                st = e.to;
                                break;
                            }
                        }
                    }
                    cmx(mx,val);
                }
            }
        }
        if(mx < 0){
            cout << "-1\n";
        }else{
            cout << 6*s*s*K - mx << "\n";
        }
    }
    return 0;
}



Measured memory_bytes: 3204