(defvar magic-squares (make-array '(8 3 3) :initial-contents '(((2 7 6) (9 5 1) (4 3 8)) ((4 9 2) (3 5 7) (8 1 6)) ((8 3 4) (1 5 9) (6 7 2)) ((6 1 8) (7 5 3) (2 9 4)) ((6 7 2) (1 5 9) (8 3 4)) ((2 9 4) (7 5 3) (6 1 8)) ((4 3 8) (9 5 1) (2 7 6)) ((8 1 6) (3 5 7) (4 9 2))))) (defun distance (square k) (loop for i from 0 to 2 sum (loop for j from 0 to 2 sum (abs (- (aref square i j) (aref magic-squares k i j)))))) (defvar input (make-array '(3 3))) (dotimes (i 3) (dotimes (j 3) (setf (aref input i j) (read)))) (format t "~d~%" (apply #'min (loop for k from 0 to 7 collect (distance input k))))