import java.io.{BufferedReader, InputStream, InputStreamReader}
import java.util.StringTokenizer

import scala.math.min

object Main {
  val MOD = 1000000007
  def solve(): Unit = {
    val sc = new InputReader(System.in)
    val N = sc.nextInt()
    val C = sc.nextInt()
    val d = Array.ofDim[Int](C, C)
    for {
      i <- 0 until C
      j <- 0 until C
    } d(i)(j) = sc.nextInt()

    val init = Array.ofDim[Int](3, C)
    for {
      i <- 1 to N
      j <- 1 to N
    } {
      init((i + j) % 3)(sc.nextInt() - 1) += 1
    }

    var ans = Integer.MAX_VALUE
    for {
      i <- 0 until C
      j <- 0 until C
      k <- 0 until C
    } {
      if (i != j && i != k && j != k) {
        val dist = Array(i, j, k)
        val sum = (for {
          g <- 0 until 3
          c <- 0 until C
        } yield {
          val cost = d(c)(dist(g))
          val cnt = init(g)(c)
          cost * cnt
        }).sum
        ans = min(ans, sum)
      }
    }

    println(ans)
  }


  def main(args: Array[String]): Unit = {
    solve()
  }


  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = null

    def next: String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next.toInt
    def nextLong(): Long = next.toLong
    def nextChar(): Char = next.charAt(0)
  }


  @specialized
  def permutation[A, B](a: Array[A], z: B)(f: (Array[A], B) => B): B = {
    def swap(i: Int, j: Int): Unit = {
      val temp = a(i)
      a(i) = a(j)
      a(j) = temp
    }

    def step(n: Int, acc: B): B = n match {
      case 1 => f(a, acc)
      case _ =>
        val acc2 = (0 until n - 1).foldLeft(acc) { (acc0, i) =>
          val acc1 = step(n - 1, acc0)
          if (n % 2 == 0) {
            swap(i, n - 1)
          } else {
            swap(0, n - 1)
          }
          acc1
        }
        step(n - 1, acc2)
    }

    step(a.length, z)
  }
}
