import java.math.*

fun main(args: Array<String>) {
    val (N, C) = readInputLine().split(" ").map { it.toInt() }
    
    val changeCost = Array(C) { readInputLine().split(" ").map { it.toInt() }.toIntArray() }
    
    val colorCnt = Array(3) { IntArray(C) }
    
    for (i in 0 until N) {
        readInputLine().split(" ").forEachIndexed { j, c -> colorCnt[(i + j + 2) % 3][c.toInt() - 1]++ }
    }
    
    var ans = Int.MAX_VALUE
    
    for (i in 0 until C) {
        val tmp1 = colorCnt[0].foldIndexed(0) { index, acc, cnt -> acc + changeCost[index][i] * cnt }
        for (j in 0 until C) {
            if (i == j) {
                continue
            }
            val tmp2 = colorCnt[1].foldIndexed(0) { index, acc, cnt -> acc + changeCost[index][j] * cnt }
            for (k in 0 until C) {
                if (i == k || j == k) {
                    continue
                }
                val tmp3 = colorCnt[2].foldIndexed(0) { index, acc, cnt -> acc + changeCost[index][k] * cnt }
                ans = Math.min(ans, tmp1 + tmp2 + tmp3)
            }
        }
    }
    
    println(ans)
}

fun readInputLine(): String {
    return readLine()!!
}
