import itertools





def main():

    N, C = list(map(int, input().split()))

    D = [list(map(int, input().split())) for _ in range(C)]

    CC = [list([int(x) - 1 for x in input().split()]) for _ in range(N)]



    cost = [[0 for _ in range(C)] for _ in range(3)]

    for c in range(C):

        for i in range(N):

            for j in range(N):

                cost[(i + j) % 3][c] += D[CC[i][j]][c]



    res = float("inf")

    color = [i for i in range(C)]

    for i, c in enumerate(list(itertools.permutations(color, 3))):

        tmp = 0

        for it in range(3):

            tmp += cost[it][c[it]]

        res = min(res, tmp)



    print(res)



main()
from itertools import product, permutations



N, C = list(map(int, input().split()))

D = [list(map(int, input().split())) for _ in range(C)]

grid = [list([int(x) - 1 for x in input().split()]) for _ in range(N)]



cost = [[0] * C for _ in range(3)]



for x, y in product(list(range(N)), list(range(N))):

    cost[(x + y) % 3][grid[x][y]] += 1



minn = float("inf")

for colors in permutations(list(range(C)), 3):

    t = 0

    for s, color_n in enumerate(colors):

        for color_p, n in enumerate(cost[s]):

            t += n * D[color_p][color_n]

    minn = min(minn, t)

print(minn)

import itertools


def main():
    N, C = list(map(int, input().split()))
    D = [list(map(int, input().split())) for _ in range(C)]
    CC = [list([int(x) - 1 for x in input().split()]) for _ in range(N)]
    cost = [[0 for _ in range(C)] for _ in range(3)]
    for c in range(C):
        for i in range(N):
            for j in range(N):
                cost[(i + j) % 3][c] += D[CC[i][j]][c]
    res = float("inf")
    color = [i for i in range(C)]
    for i, c in enumerate(list(itertools.permutations(color, 3))):
        tmp = 0
        for it in range(3):
            tmp += cost[it][c[it]]
        res = min(res, tmp)
    print(res)


main()

from itertools import product, permutations

N, C = list(map(int, input().split()))
D = [list(map(int, input().split())) for _ in range(C)]
grid = [list([int(x) - 1 for x in input().split()]) for _ in range(N)]
cost = [[0] * C for _ in range(3)]
for x, y in product(list(range(N)), list(range(N))):
    cost[(x + y) % 3][grid[x][y]] += 1
minn = float("inf")
for colors in permutations(list(range(C)), 3):
    t = 0
    for s, color_n in enumerate(colors):
        for color_p, n in enumerate(cost[s]):
            t += n * D[color_p][color_n]
    minn = min(minn, t)
print(minn)

-import itertools +from itertools import product, permutations - -def main(): -    N, C = list(map(int, input().split())) -    D = [list(map(int, input().split())) for _ in range(C)] -    CC = [list([int(x) - 1 for x in input().split()]) for _ in range(N)] -    cost = [[0 for _ in range(C)] for _ in range(3)] -    for c in range(C): -        for i in range(N): -            for j in range(N): -                cost[(i + j) % 3][c] += D[CC[i][j]][c] -    res = float("inf") -    color = [i for i in range(C)] -    for i, c in enumerate(list(itertools.permutations(color, 3))): -        tmp = 0 -        for it in range(3): -            tmp += cost[it][c[it]] -        res = min(res, tmp) -    print(res) - - -main() +N, C = list(map(int, input().split())) +D = [list(map(int, input().split())) for _ in range(C)] +grid = [list([int(x) - 1 for x in input().split()]) for _ in range(N)] +cost = [[0] * C for _ in range(3)] +for x, y in product(list(range(N)), list(range(N))): +    cost[(x + y) % 3][grid[x][y]] += 1 +minn = float("inf") +for colors in permutations(list(range(C)), 3): +    t = 0 +    for s, color_n in enumerate(colors): +        for color_p, n in enumerate(cost[s]): +            t += n * D[color_p][color_n] +    minn = min(minn, t) +print(minn)