;; -*- coding: utf-8 -*-
(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0)))
  #+swank (progn (ql:quickload '(:cl-debug-print :fiveam))
                 (shadow :run)
                 (use-package :fiveam))
  #-swank (set-dispatch-macro-character #\# #\> (lambda (s c p) (declare (ignore c p)) (read s nil (values) t))))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  (declare #.OPT)
  (macrolet ((%read-byte ()
               `(the (unsigned-byte 8)
                     #+swank (char-code (read-char in nil #\Nul))
                     #-swank (sb-impl::ansi-stream-read-byte in nil #.(char-code #\Nul) nil))))
    (let* ((minus nil)
           (result (loop (let ((byte (%read-byte)))
                           (cond ((<= 48 byte 57)
                                  (return (- byte 48)))
                                 ((zerop byte) ; #\Nul
                                  (error "Read EOF or #\Nul."))
                                 ((= byte #.(char-code #\-))
                                  (setf minus t)))))))
      (declare ((integer 0 #.most-positive-fixnum) result))
      (loop
        (let* ((byte (%read-byte)))
          (if (<= 48 byte 57)
              (setq result (+ (- byte 48) (* 10 (the (integer 0 #.(floor most-positive-fixnum 10)) result))))
              (return (if minus (- result) result))))))))

(defmacro dbg (&rest forms)
  #+swank
  (if (= (length forms) 1)
      `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms))
      `(format *error-output* "~A => ~A~%" ',forms `(,,@forms)))
  #-swank (declare (ignore forms)))

(defmacro define-int-types (&rest bits)
  `(progn
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits)
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits)))
(define-int-types 2 4 7 8 15 16 31 32 62 63 64)

(declaim (inline println))
(defun println (obj &optional (stream *standard-output*))
  (let ((*read-default-float-format* 'double-float))
    (prog1 (princ obj stream) (terpri stream))))

(defconstant +mod+ 1000000007)

;; Body

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (c (read))
         (ds (make-array (list c c) :element-type 'uint16))
         (table0 (make-array c :element-type 'uint32))
         (table1 (make-array c :element-type 'uint32))
         (table2 (make-array c :element-type 'uint32)))
    (declare (uint16 n c))
    (dotimes (i c)
      (dotimes (j c)
        (setf (aref ds i j) (read-fixnum))))
    (dotimes (i n)
      (dotimes (j n)
        (let ((col (- (read-fixnum) 1)))
          (ecase (mod (+ i j) 3)
            (0 (incf (aref table0 col)))
            (1 (incf (aref table1 col)))
            (2 (incf (aref table2 col)))))))
    (let ((res #xffffffff))
      (declare (uint32 res))
      (dotimes (col0 c)
        (dotimes (col1 c)
          (dotimes (col2 c)
            (unless (or (= col0 col1) (= col1 col2) (= col2 col0))
              (setf res
                    (min res
                         (+ (loop for scol below c
                                  sum (* (aref ds scol col0) (aref table0 scol))
                                  of-type uint32)
                            (loop for scol below c
                                  sum (* (aref ds scol col1) (aref table1 scol))
                                  of-type uint32)
                            (loop for scol below c
                                  sum (* (aref ds scol col2) (aref table2 scol))
                                  of-type uint32))))))))
      (println res))))

#-swank(main)


Measured memory_bytes: 14824