from itertools import *

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

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

c = [list(map(int, input().split())) for _ in [0]*N]

tri = [[] for _ in [0]*3]

for i in range(1, N+1):

    for j in range(1, N+1):

        tri[(i+j) % 3] += [c[i-1][j-1] - 1]

incom = [[0]*C for _ in [0]*3]

for i in range(3):

    for color in range(C):

        incom[i][color] = sum(D[x][color] for x in tri[i])

ans = 1<<30

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

    t = sum(incom[x][y]for x,y in enumerate(p))

    if t < ans: ans = t

print(ans)
from itertools import *

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

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

tri = [[0]*C for _ in [0]*3]

for i in range(N):

    for j,x in enumerate(map(int, input().split())):

        tri[(i+j)%3][x-1] += 1

res = 1<<30

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

    tt = sum(D[l][p[m]] * tri[m][l] for l in range(C) for m in range(3))

    if tt < res: res = tt

print(res)
from itertools import *

N, C = list(map(int, input().split()))
D = [list(map(int, input().split())) for _ in [0] * C]
c = [list(map(int, input().split())) for _ in [0] * N]
tri = [[] for _ in [0] * 3]
for i in range(1, N + 1):
    for j in range(1, N + 1):
        tri[(i + j) % 3] += [c[i - 1][j - 1] - 1]
incom = [[0] * C for _ in [0] * 3]
for i in range(3):
    for color in range(C):
        incom[i][color] = sum(D[x][color] for x in tri[i])
ans = 1 << 30
for p in permutations(list(range(C)), 3):
    t = sum(incom[x][y] for x, y in enumerate(p))
    if t < ans:
        ans = t
print(ans)

from itertools import *

N, C = list(map(int, input().split()))
D = [list(map(int, input().split())) for _ in [0] * C]
tri = [[0] * C for _ in [0] * 3]
for i in range(N):
    for j, x in enumerate(map(int, input().split())):
        tri[(i + j) % 3][x - 1] += 1
res = 1 << 30
for p in permutations(list(range(C)), 3):
    tt = sum(D[l][p[m]] * tri[m][l] for l in range(C) for m in range(3))
    if tt < res:
        res = tt
print(res)

-c = [list(map(int, input().split())) for _ in [0] * N] -tri = [[] for _ in [0] * 3] -for i in range(1, N + 1): -    for j in range(1, N + 1): -        tri[(i + j) % 3] += [c[i - 1][j - 1] - 1] -incom = [[0] * C for _ in [0] * 3] -for i in range(3): -    for color in range(C): -        incom[i][color] = sum(D[x][color] for x in tri[i]) -ans = 1 << 30 +tri = [[0] * C for _ in [0] * 3] +for i in range(N): +    for j, x in enumerate(map(int, input().split())): +        tri[(i + j) % 3][x - 1] += 1 +res = 1 << 30 -    t = sum(incom[x][y] for x, y in enumerate(p)) -    if t < ans: -        ans = t -print(ans) +    tt = sum(D[l][p[m]] * tri[m][l] for l in range(C) for m in range(3)) +    if tt < res: +        res = tt +print(res)