#include <bits/stdc++.h>
#define loop for(;;)
#define rep(i, n) for(long i = 0; i < (long)(n); ++i)
typedef std::vector<long> LI;

struct Solver {
   std::vector<long> nexts[2123];
   std::vector<long> overlap[2123];
   long n, len, s;
   LI xs, ys, zs;
   long f(long i, long k) {
      long dx = std::max((long)0, s - std::abs(xs[k] - xs[i]));
      long dy = std::max((long)0, s - std::abs(ys[k] - ys[i]));
      long dz = std::max((long)0, s - std::abs(zs[k] - zs[i]));
      if( dx == 0 or dy == 0 or dz == 0 ) return 0;
      return 2 * (dx * dy + dy * dz + dz * dx);
   }
   void solve() {
      std::cin >> n >> len >> s;
      if( !(n|len|s) ) exit(0);
      xs.resize(n), ys.resize(n), zs.resize(n);
      rep(i, n) std::cin >> xs[i] >> ys[i] >> zs[i];
      rep(i, n) rep(k, n) {
         if( i == k ) continue;
         long over = f(i, k);
         if( over == 0 ) continue;
         nexts[i].push_back(k);
         overlap[i].push_back(over);
      }
      long res = -(1LL << 53);
      rep(i, n) {
         visited.resize(n, false);
         res = std::max(res, dfs(i, len-1, i));
      }
      visited.resize(n);
      printf("%ld\n", res >= 0 ? 6 * s * s * len - res : -1);
   }
   std::vector<bool> visited;
   long dfs(long v, long d, long begin) {
      if( d == 0 ) {
         if( len >= 3 ) {
            rep(i, nexts[v].size()) {
               long next = nexts[v][i];
               long over = overlap[v][i];
               if( next == begin ) {
                  return over;
               }
            }
         }
         return 0;
      }
      long res = -(1LL << 53);
      visited[v] = true;
      rep(i, nexts[v].size()) {
         long next = nexts[v][i];
         long over = overlap[v][i];
         if( visited[next] ) continue;
         res = std::max(res, dfs(next, d-1, begin) + over);
      }
      visited[v] = false;
      return res;
   }
};

int main() {
   loop std::shared_ptr<Solver>(new Solver())->solve();
}