Cube
The distance on the surface of a polyhedron between two points is defined as the shortest length of a broken line that connects the two given points, with all segments of the line lying on the surface of the polyhedron. Consider a cube with vertices in the Cartesian coordinate system, where each coordinate is either 0 or a natural number a, not exceeding 15.
Your task is to write a program that calculates the square of the distance on the surface of the cube between points with integer coordinates.
Input
The first line contains the natural numbers a and n. For each j from 1 to n, the (j + 1)-th line contains 6 non-negative integers. These represent the coordinates (first the abscissa x, then the ordinate y, and finally the applicate z) of a point located on the face of the cube in the xy coordinate plane, and an arbitrary point on the surface of the cube.
Output
For each input line starting from the second, append the square of the length of the broken line that connects the points whose coordinates are given in that line.