3D Printing

Select exactly k of n non-overlapping unit-direction cubes whose grid adjacency graph is connected, minimizing the union's surface area.

Medium7GraphBFSCombinatoricsNo attempts yetTime limit2sMemory limit512 MB

Problem

You are designing an installation art piece made of cubes with 3D printing technology, to enter it in the Installation art Contest with Printed Cubes (ICPC). The piece is made of exactly kk cubes of the same size that face the same direction.

First, a CAD system prepares nn candidate positions in 3D space where a cube can be placed, with nkn \ge k. When cubes are placed at every candidate position, the following three conditions hold.

  • The number of other cubes that one cube overlaps is 0, 1, or 2. No cube overlaps three or more.
  • When one cube overlaps two other cubes, those two cubes do not overlap each other.
  • Two cubes that do not overlap touch neither at a face, nor at an edge, nor at a corner.

Now choose kk different positions among the nn candidates and place a cube at each of them. The union of the kk cubes must be a connected polyhedron. A 3D printer usually prints only the thin surface of an object, so the piece that saves filament is the one with the smallest surface area.

Find the smallest surface area among the connected polyhedra that kk of the nn candidate positions can produce.

Figure 1. A polyhedron formed with connected identical cubes.

Input

The input holds several datasets. There are at most 100 datasets. Each dataset has the following format.

n k s
x1 y1 z1
...
xn yn zn

On the first line of a dataset, nn is the number of candidate positions, kk is the number of cubes that form the connected polyhedron, and ss is the edge length of a cube. nn, kk, and ss are integers separated by a space. The next nn lines give the nn candidate positions. On the ii-th line, the integers xix_i, yiy_i, and ziz_i are the coordinates of the corner with the smallest coordinate values of the cube placed there, separated by a space. The edges of a cube are parallel to one of the three coordinate axes.

The values satisfy 1kn20001 \le k \le n \le 2000, 3s1003 \le s \le 100, and 4×107xi,yi,zi4×107-4 \times 10^7 \le x_i, y_i, z_i \le 4 \times 10^7. The candidate positions always satisfy the three conditions above.

The end of the input is a line with three zeros separated by a space.

Output

For each dataset, print the smallest surface area of the connected polyhedron as one integer on its own line. If no kk cubes form a connected polyhedron, print -1.