from itertools import permutations

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



d = [[0]*C for i in range(C)]

for i in range(C):

  d[i] = [int(c) for c in input().split()]



grid = [[0]*C for i in range(3)]

for i in range(N):

  inf = [int(c)-1 for c in input().split()]

  for j in range(N):

    x = (j+i+2)%3

    g = inf[j]

    grid[x][g] += 1



record = [[0]*C for i in range(3)]

for i in range(3):

  for j in range(C):

    m = 0

    for h in range(C):

      m += grid[i][h]*d[h][j]

    record[i][j] = m



ans = 10**9

seq = list(range(C))

for i,j,h in permutations(seq,3):

  m = record[0][i]+record[1][j]+record[2][h]

  if m<ans:

    ans = m

    

print(ans)
from itertools import permutations

N, C, *L = list(map(int, open(0).read().split()))

ls =[[0]*(C+1) for i in range(3)]

D = L[:C*C]

X = L[C*C:]

for i in range(N):

  for j in range(N):

    c = X[i*N+j]

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

cost = [[0]*(C+1) for i in range(3)]

for i in range(3):

  n = sum(ls[i])

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

    for h in range(1,C+1):

      cost[i][j] += D[(h-1)*C+j-1]*ls[i][h]

  

ans = 10**10

for l in permutations(list(range(1,C+1)),3):

  ans = min(ans, sum(cost[i][l[i]] for i in range(3)))

print(ans)
from itertools import permutations

N, C = list(map(int, input().split()))
d = [[0] * C for i in range(C)]
for i in range(C):
    d[i] = [int(c) for c in input().split()]
grid = [[0] * C for i in range(3)]
for i in range(N):
    inf = [int(c) - 1 for c in input().split()]
    for j in range(N):
        x = (j + i + 2) % 3
        g = inf[j]
        grid[x][g] += 1
record = [[0] * C for i in range(3)]
for i in range(3):
    for j in range(C):
        m = 0
        for h in range(C):
            m += grid[i][h] * d[h][j]
        record[i][j] = m
ans = 10**9
seq = list(range(C))
for i, j, h in permutations(seq, 3):
    m = record[0][i] + record[1][j] + record[2][h]
    if m < ans:
        ans = m
print(ans)

from itertools import permutations

N, C, *L = list(map(int, open(0).read().split()))
ls = [[0] * (C + 1) for i in range(3)]
D = L[: C * C]
X = L[C * C :]
for i in range(N):
    for j in range(N):
        c = X[i * N + j]
        ls[(i + j) % 3][c] += 1
cost = [[0] * (C + 1) for i in range(3)]
for i in range(3):
    n = sum(ls[i])
    for j in range(1, C + 1):
        for h in range(1, C + 1):
            cost[i][j] += D[(h - 1) * C + j - 1] * ls[i][h]
ans = 10**10
for l in permutations(list(range(1, C + 1)), 3):
    ans = min(ans, sum(cost[i][l[i]] for i in range(3)))
print(ans)

-N, C = list(map(int, input().split())) -d = [[0] * C for i in range(C)] -for i in range(C): -    d[i] = [int(c) for c in input().split()] -grid = [[0] * C for i in range(3)] +N, C, *L = list(map(int, open(0).read().split())) +ls = [[0] * (C + 1) for i in range(3)] +D = L[: C * C] +X = L[C * C :] -    inf = [int(c) - 1 for c in input().split()] -        x = (j + i + 2) % 3 -        g = inf[j] -        grid[x][g] += 1 -record = [[0] * C for i in range(3)] +        c = X[i * N + j] +        ls[(i + j) % 3][c] += 1 +cost = [[0] * (C + 1) for i in range(3)] -    for j in range(C): -        m = 0 -        for h in range(C): -            m += grid[i][h] * d[h][j] -        record[i][j] = m -ans = 10**9 -seq = list(range(C)) -for i, j, h in permutations(seq, 3): -    m = record[0][i] + record[1][j] + record[2][h] -    if m < ans: -        ans = m +    n = sum(ls[i]) +    for j in range(1, C + 1): +        for h in range(1, C + 1): +            cost[i][j] += D[(h - 1) * C + j - 1] * ls[i][h] +ans = 10**10 +for l in permutations(list(range(1, C + 1)), 3): +    ans = min(ans, sum(cost[i][l[i]] for i in range(3)))