from itertools import permutations as perm

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

colors = []

for i in range(C):

    colors.append(list(map(int, input().split())))



grids = {0:{}, 1:{}, 2:{}}

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

    A =list(map(int, input().split()))

    for j in range(N):

        if A[j] not in grids[(i+j+1)%3]:

            grids[(i+j+1)%3][A[j]] = 1

        else:

            grids[(i+j+1)%3][A[j]] += 1

mn = 10**9

for color in perm([i for i in range(1,C+1)], 3):

    tmp = 0

    for i in range(3):

        for k,v in list(grids[i].items()):

            tmp += colors[k-1][color[i]-1] * v

    mn = min(mn, tmp)

print(mn)
n,c = list(map(int, input().split()))

color, grid = [], []

for _ in range(c):

    color.append(list(map(int, input().split())))

grid = {i:{} for i in range(3)}



for i in range(n):

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

        t -= 1

        if t not in grid[(i+j + 2) % 3]:

            grid[(i+j + 2) % 3][t] = 0

        grid[(i+j + 2) % 3][t] += 1



costs = [[0 for _ in range(c)],[0 for _ in range(c)],[0 for _ in range(c)]]

for cc in range(c):

    for i in range(3):

        for d,cnt in list(grid[i].items()):

            costs[i][cc] += color[d][cc] * cnt



x = sorted(list(range(c)), key=lambda x: costs[0][x])[:3]

y = sorted(list(range(c)), key=lambda x: costs[1][x])[:3]

z = sorted(list(range(c)), key=lambda x: costs[2][x])[:3]

mn = 10**9

for i in x:

    for j in y:

        for k in z:

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

                continue

            if mn > costs[0][i] + costs[1][j] + costs[2][k]:

                mn = costs[0][i] + costs[1][j] + costs[2][k]

print(mn)
from itertools import permutations as perm

N, C = list(map(int, input().split()))
colors = []
for i in range(C):
    colors.append(list(map(int, input().split())))
grids = {0: {}, 1: {}, 2: {}}
for i in range(1, N + 1):
    A = list(map(int, input().split()))
    for j in range(N):
        if A[j] not in grids[(i + j + 1) % 3]:
            grids[(i + j + 1) % 3][A[j]] = 1
        else:
            grids[(i + j + 1) % 3][A[j]] += 1
mn = 10**9
for color in perm([i for i in range(1, C + 1)], 3):
    tmp = 0
    for i in range(3):
        for k, v in list(grids[i].items()):
            tmp += colors[k - 1][color[i] - 1] * v
    mn = min(mn, tmp)
print(mn)

n, c = list(map(int, input().split()))
color, grid = [], []
for _ in range(c):
    color.append(list(map(int, input().split())))
grid = {i: {} for i in range(3)}
for i in range(n):
    for j, t in enumerate(map(int, input().split())):
        t -= 1
        if t not in grid[(i + j + 2) % 3]:
            grid[(i + j + 2) % 3][t] = 0
        grid[(i + j + 2) % 3][t] += 1
costs = [[0 for _ in range(c)], [0 for _ in range(c)], [0 for _ in range(c)]]
for cc in range(c):
    for i in range(3):
        for d, cnt in list(grid[i].items()):
            costs[i][cc] += color[d][cc] * cnt
x = sorted(list(range(c)), key=lambda x: costs[0][x])[:3]
y = sorted(list(range(c)), key=lambda x: costs[1][x])[:3]
z = sorted(list(range(c)), key=lambda x: costs[2][x])[:3]
mn = 10**9
for i in x:
    for j in y:
        for k in z:
            if i == j or j == k or k == i:
                continue
            if mn > costs[0][i] + costs[1][j] + costs[2][k]:
                mn = costs[0][i] + costs[1][j] + costs[2][k]
print(mn)

-from itertools import permutations as perm - -N, C = list(map(int, input().split())) -colors = [] -for i in range(C): -    colors.append(list(map(int, input().split()))) -grids = {0: {}, 1: {}, 2: {}} -for i in range(1, N + 1): -    A = list(map(int, input().split())) -    for j in range(N): -        if A[j] not in grids[(i + j + 1) % 3]: -            grids[(i + j + 1) % 3][A[j]] = 1 -        else: -            grids[(i + j + 1) % 3][A[j]] += 1 +n, c = list(map(int, input().split())) +color, grid = [], [] +for _ in range(c): +    color.append(list(map(int, input().split()))) +grid = {i: {} for i in range(3)} +for i in range(n): +    for j, t in enumerate(map(int, input().split())): +        t -= 1 +        if t not in grid[(i + j + 2) % 3]: +            grid[(i + j + 2) % 3][t] = 0 +        grid[(i + j + 2) % 3][t] += 1 +costs = [[0 for _ in range(c)], [0 for _ in range(c)], [0 for _ in range(c)]] +for cc in range(c): +    for i in range(3): +        for d, cnt in list(grid[i].items()): +            costs[i][cc] += color[d][cc] * cnt +x = sorted(list(range(c)), key=lambda x: costs[0][x])[:3] +y = sorted(list(range(c)), key=lambda x: costs[1][x])[:3] +z = sorted(list(range(c)), key=lambda x: costs[2][x])[:3] -for color in perm([i for i in range(1, C + 1)], 3): -    tmp = 0 -    for i in range(3): -        for k, v in list(grids[i].items()): -            tmp += colors[k - 1][color[i] - 1] * v -    mn = min(mn, tmp) +for i in x: +    for j in y: +        for k in z: +            if i == j or j == k or k == i: +                continue +            if mn > costs[0][i] + costs[1][j] + costs[2][k]: +                mn = costs[0][i] + costs[1][j] + costs[2][k]