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

D=[[int(i) for i in input().split()] for j in range(C)]

co=[[int(i) for i in input().split()] for j in range(N)]

A=[[0]*C for i in range(3)]#iに変えた時の違和感

for i in range(N):

    for j in range(N):

        t = (i+j)%3

        for s in range(C):

            A[t][s]+=D[co[j][i]-1][s]

ans =10**13

for x in range(C):

    for y in range(C):

        if x==y:

            continue

        for z in range(C):

            if x==z or y==z:

                continue

            ans=min(ans,A[0][x]+A[1][y]+A[2][z])

print(ans)
from collections import defaultdict

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

D=[[int(i) for i in input().split()] for j in range(C)]

M=[[int(i) for i in input().split()] for j in range(N)]

ans=10**9

L=[defaultdict(int) for i in range(3)]



for i in range(N):

    for j in range(N):

        L[(i+j)%3][M[i][j]-1]+=1

#print(L)

for i in range(C):

    for j in range(C):

        for k in range(C):

            if i==j or j==k or k==i:

                continue

            num=0

            for color,number in list(L[0].items()):

                #num+=D[i][color]*number

                num += D[color][i] * number

                #print(num)

            for color,number in list(L[1].items()):

                #num+=D[j][color]*number

                #print(num)

                num += D[color][j] * number

            for color,number in list(L[2].items()):

                #num+=D[k][color]*number

                #print(num)

                num += D[color ][k] * number

            ans=min(ans,num)



            #print(ans,i,j,k)

#for color,number in L[0].items():

    #print(color,number)



print(ans)



N, C = list(map(int, input().split()))
D = [[int(i) for i in input().split()] for j in range(C)]
co = [[int(i) for i in input().split()] for j in range(N)]
A = [[0] * C for i in range(3)]  # iに変えた時の違和感
for i in range(N):
    for j in range(N):
        t = (i + j) % 3
        for s in range(C):
            A[t][s] += D[co[j][i] - 1][s]
ans = 10**13
for x in range(C):
    for y in range(C):
        if x == y:
            continue
        for z in range(C):
            if x == z or y == z:
                continue
            ans = min(ans, A[0][x] + A[1][y] + A[2][z])
print(ans)

from collections import defaultdict

N, C = list(map(int, input().split()))
D = [[int(i) for i in input().split()] for j in range(C)]
M = [[int(i) for i in input().split()] for j in range(N)]
ans = 10**9
L = [defaultdict(int) for i in range(3)]
for i in range(N):
    for j in range(N):
        L[(i + j) % 3][M[i][j] - 1] += 1
# print(L)
for i in range(C):
    for j in range(C):
        for k in range(C):
            if i == j or j == k or k == i:
                continue
            num = 0
            for color, number in list(L[0].items()):
                # num+=D[i][color]*number
                num += D[color][i] * number
                # print(num)
            for color, number in list(L[1].items()):
                # num+=D[j][color]*number
                # print(num)
                num += D[color][j] * number
            for color, number in list(L[2].items()):
                # num+=D[k][color]*number
                # print(num)
                num += D[color][k] * number
            ans = min(ans, num)
            # print(ans,i,j,k)
# for color,number in L[0].items():
# print(color,number)
print(ans)

+from collections import defaultdict + -co = [[int(i) for i in input().split()] for j in range(N)] -A = [[0] * C for i in range(3)]  # iに変えた時の違和感 +M = [[int(i) for i in input().split()] for j in range(N)] +ans = 10**9 +L = [defaultdict(int) for i in range(3)] -        t = (i + j) % 3 -        for s in range(C): -            A[t][s] += D[co[j][i] - 1][s] -ans = 10**13 -for x in range(C): -    for y in range(C): -        if x == y: -            continue -        for z in range(C): -            if x == z or y == z: +        L[(i + j) % 3][M[i][j] - 1] += 1 +# print(L) +for i in range(C): +    for j in range(C): +        for k in range(C): +            if i == j or j == k or k == i: -            ans = min(ans, A[0][x] + A[1][y] + A[2][z]) +            num = 0 +            for color, number in list(L[0].items()): +                # num+=D[i][color]*number +                num += D[color][i] * number +                # print(num) +            for color, number in list(L[1].items()): +                # num+=D[j][color]*number +                # print(num) +                num += D[color][j] * number +            for color, number in list(L[2].items()): +                # num+=D[k][color]*number +                # print(num) +                num += D[color][k] * number +            ans = min(ans, num) +            # print(ans,i,j,k) +# for color,number in L[0].items(): +# print(color,number)