open System
let mutable s = Console.ReadLine().Split()
let n = int(s.[0])
let c = int(s.[1])
let mutable d : string array = Console.ReadLine().Split()
let mutable i = 1
while i < c do
    d <- Array.append d (Console.ReadLine().Split())
    i <- i + 1
let GetD = fun x y -> int(d.[(c * (x - 1) + y - 1)])
let mutable m0 : int array = Array.zeroCreate (n * n)
let mutable m1 : int array = Array.zeroCreate (n * n)
let mutable m2 : int array = Array.zeroCreate (n * n)
i <- 1
let mutable j = 1
let mutable p = 0
let mutable q = 0
let mutable r = 0
while i <= n do
    s <- Console.ReadLine().Split()
    while j <= n do
        if (i + j) % 3 = 0 then
            m0.[p] <- int(s.[j - 1])
            p <- p + 1
        if (i + j) % 3 = 1 then
            m1.[q] <- int(s.[j - 1])
            q <- q + 1
        if (i + j) % 3 = 2 then
            m2.[r] <- int(s.[j - 1])
            r <- r + 1
        j <- j + 1
    i <- i + 1
    j <- 1
i <- 0
let mutable d0 : int array = Array.zeroCreate c
let mutable d1 : int array = Array.zeroCreate c
let mutable d2 : int array = Array.zeroCreate c
while i < c do
    let mutable t = 0
    j <- 0
    while j < m0.Length do
        if m0.[j] <> 0 then
            t <- t + GetD m0.[j] (i + 1)
        j <- j + 1
    d0.[i] <- t
    t <- 0
    j <- 0
    while j < m1.Length do
        if m1.[j] <> 0 then
            t <- t + GetD m1.[j] (i + 1)
        j <- j + 1
    d1.[i] <- t
    t <- 0
    j <- 0
    while j < m2.Length do
        if m2.[j] <> 0 then
            t <- t + GetD m2.[j] (i + 1)
        j <- j + 1
    d2.[i] <- t
    i <- i + 1
let mutable ans = 250000000
let mutable k = 0
i <- 0
while i < c do
    j <- 0
    while j < c do
        k <- 0
        while k < c do
            if i <> j && j <> k && k <> i then
                ans <- min ans (d0.[i] + d1.[j] + d2.[k])
            k <- k + 1
        j <- j + 1
    i <- i + 1
printfn "%d" ans