from collections import defaultdict

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



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



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





for i in range(N):

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

    for j in range(N):

        for k in range(C):

            d[(i+j) % 3][k] += D[c[j]-1][k]

# print(d)



ans = 10**9

for i in range(C):

    for j in range(C):

        for k in range(C):

            if i != j and j != k and k != i:

                ans = min(ans, d[0][i]+d[1][j]+d[2][k])



print(ans)

from collections import defaultdict

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

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



color = [[int(i) for i in input().split()] for _ in range(N)]

M = [defaultdict(int) for _ in range(3)]

for i in range(N):

    for j in range(N):

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



ans = 10**20

for Y0 in range(C):

    for Y1 in range(C):

        for Y2 in range(C):

            if Y0 != Y1 and Y1 != Y2 and Y2 != Y0:

                Y = (Y0, Y1, Y2)

                d = 0

                for i in range(3):

                    for X, v in list(M[i].items()):

                        d += D[X-1][Y[i]]*v

                ans = min(ans, d)

print(ans)

from collections import defaultdict

N, C = list(map(int, input().split()))
D = [tuple(map(int, input().split())) for _ in range(C)]
d = [defaultdict(int) for i in range(3)]
for i in range(N):
    c = [int(x) for x in input().split()]
    for j in range(N):
        for k in range(C):
            d[(i + j) % 3][k] += D[c[j] - 1][k]
# print(d)
ans = 10**9
for i in range(C):
    for j in range(C):
        for k in range(C):
            if i != j and j != k and k != i:
                ans = min(ans, d[0][i] + d[1][j] + d[2][k])
print(ans)

from collections import defaultdict

N, C = list(map(int, input().split()))
D = [[int(i) for i in input().split()] for _ in range(C)]
color = [[int(i) for i in input().split()] for _ in range(N)]
M = [defaultdict(int) for _ in range(3)]
for i in range(N):
    for j in range(N):
        M[(i + j) % 3][color[i][j]] += 1
ans = 10**20
for Y0 in range(C):
    for Y1 in range(C):
        for Y2 in range(C):
            if Y0 != Y1 and Y1 != Y2 and Y2 != Y0:
                Y = (Y0, Y1, Y2)
                d = 0
                for i in range(3):
                    for X, v in list(M[i].items()):
                        d += D[X - 1][Y[i]] * v
                ans = min(ans, d)
print(ans)

-D = [tuple(map(int, input().split())) for _ in range(C)] -d = [defaultdict(int) for i in range(3)] +D = [[int(i) for i in input().split()] for _ in range(C)] +color = [[int(i) for i in input().split()] for _ in range(N)] +M = [defaultdict(int) for _ in range(3)] -    c = [int(x) for x in input().split()] -        for k in range(C): -            d[(i + j) % 3][k] += D[c[j] - 1][k] -# print(d) -ans = 10**9 -for i in range(C): -    for j in range(C): -        for k in range(C): -            if i != j and j != k and k != i: -                ans = min(ans, d[0][i] + d[1][j] + d[2][k]) +        M[(i + j) % 3][color[i][j]] += 1 +ans = 10**20 +for Y0 in range(C): +    for Y1 in range(C): +        for Y2 in range(C): +            if Y0 != Y1 and Y1 != Y2 and Y2 != Y0: +                Y = (Y0, Y1, Y2) +                d = 0 +                for i in range(3): +                    for X, v in list(M[i].items()): +                        d += D[X - 1][Y[i]] * v +                ans = min(ans, d)