def iin(): return int(eval(input()))

def nl(): return list(map(int, input().split()))

def ary(r, c, v): return [[v for _ in range(c)] for _ in range(r)]



N, C = nl()

d = [nl() for _ in range(C)]

c = [nl() for _ in range(N)]



color_count_by_r = []

for i in range(3):

    color_count_by_r.append(dict())

for i in range(N):

    for j in range(N):

        color_count_by_r[(i + j) % 3][c[i][j]] = color_count_by_r[(i + j) % 3].get(c[i][j], 0) + 1



ans = float('inf')

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

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

        if c0 == c1:

            continue

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

            if c0 == c2 or c1 == c2:

                continue

            cost = 0

            for i in range(3):

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

                    next_color = [c0, c1, c2][i]

                    cost += color_count_by_r[i].get(j, 0) * d[j - 1][next_color - 1]

            ans = min(ans, cost)

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

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

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



for i in range(n):

    for j in range(n):

        C[i][j] -= 1



cnt_color_by_r = [dict() for _ in range(3)]



for r in range(3):

    for clr in range(c):

        cnt_color_by_r[r][clr] = 0



for i in range(n):

    for j in range(n):

        cnt_color_by_r[(i + j) % 3][C[i][j]] += 1



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



for r in range(3):

    for after in range(c):

        cost = 0

        for before in range(c):

            cost += D[before][after] * cnt_color_by_r[r][before]

        costs[r][after] = cost



cost_r0 = list(sorted([(after, costs[0][after]) for after in range(c)], key=lambda x: x[1]))[:3]

cost_r1 = list(sorted([(after, costs[1][after]) for after in range(c)], key=lambda x: x[1]))[:3]

cost_r2 = list(sorted([(after, costs[2][after]) for after in range(c)], key=lambda x: x[1]))[:3]



ans = float('inf')

for c0, cost0 in cost_r0:

    for c1, cost1 in cost_r1:

        for c2, cost2 in cost_r2:

            if c0 != c1 and c1 != c2 and c2 != c0:

                ans = min(ans, cost0 + cost1 + cost2)



print(ans)
def iin():
    return int(eval(input()))


def nl():
    return list(map(int, input().split()))


def ary(r, c, v):
    return [[v for _ in range(c)] for _ in range(r)]


N, C = nl()
d = [nl() for _ in range(C)]
c = [nl() for _ in range(N)]
color_count_by_r = []
for i in range(3):
    color_count_by_r.append(dict())
for i in range(N):
    for j in range(N):
        color_count_by_r[(i + j) % 3][c[i][j]] = (
            color_count_by_r[(i + j) % 3].get(c[i][j], 0) + 1
        )
ans = float("inf")
for c0 in range(1, C + 1):
    for c1 in range(1, C + 1):
        if c0 == c1:
            continue
        for c2 in range(1, C + 1):
            if c0 == c2 or c1 == c2:
                continue
            cost = 0
            for i in range(3):
                for j in range(1, C + 1):
                    next_color = [c0, c1, c2][i]
                    cost += color_count_by_r[i].get(j, 0) * d[j - 1][next_color - 1]
            ans = min(ans, cost)
print(ans)

n, c = list(map(int, input().split()))
D = [list(map(int, input().split())) for _ in range(c)]
C = [list(map(int, input().split())) for _ in range(n)]
for i in range(n):
    for j in range(n):
        C[i][j] -= 1
cnt_color_by_r = [dict() for _ in range(3)]
for r in range(3):
    for clr in range(c):
        cnt_color_by_r[r][clr] = 0
for i in range(n):
    for j in range(n):
        cnt_color_by_r[(i + j) % 3][C[i][j]] += 1
costs = [[0 for _ in range(c)] for _ in range(3)]
for r in range(3):
    for after in range(c):
        cost = 0
        for before in range(c):
            cost += D[before][after] * cnt_color_by_r[r][before]
        costs[r][after] = cost
cost_r0 = list(
    sorted([(after, costs[0][after]) for after in range(c)], key=lambda x: x[1])
)[:3]
cost_r1 = list(
    sorted([(after, costs[1][after]) for after in range(c)], key=lambda x: x[1])
)[:3]
cost_r2 = list(
    sorted([(after, costs[2][after]) for after in range(c)], key=lambda x: x[1])
)[:3]
ans = float("inf")
for c0, cost0 in cost_r0:
    for c1, cost1 in cost_r1:
        for c2, cost2 in cost_r2:
            if c0 != c1 and c1 != c2 and c2 != c0:
                ans = min(ans, cost0 + cost1 + cost2)
print(ans)

-def iin(): -    return int(eval(input())) - - -def nl(): -    return list(map(int, input().split())) - - -def ary(r, c, v): -    return [[v for _ in range(c)] for _ in range(r)] - - -N, C = nl() -d = [nl() for _ in range(C)] -c = [nl() for _ in range(N)] -color_count_by_r = [] -for i in range(3): -    color_count_by_r.append(dict()) -for i in range(N): -    for j in range(N): -        color_count_by_r[(i + j) % 3][c[i][j]] = ( -            color_count_by_r[(i + j) % 3].get(c[i][j], 0) + 1 -        ) +n, c = list(map(int, input().split())) +D = [list(map(int, input().split())) for _ in range(c)] +C = [list(map(int, input().split())) for _ in range(n)] +for i in range(n): +    for j in range(n): +        C[i][j] -= 1 +cnt_color_by_r = [dict() for _ in range(3)] +for r in range(3): +    for clr in range(c): +        cnt_color_by_r[r][clr] = 0 +for i in range(n): +    for j in range(n): +        cnt_color_by_r[(i + j) % 3][C[i][j]] += 1 +costs = [[0 for _ in range(c)] for _ in range(3)] +for r in range(3): +    for after in range(c): +        cost = 0 +        for before in range(c): +            cost += D[before][after] * cnt_color_by_r[r][before] +        costs[r][after] = cost +cost_r0 = list( +    sorted([(after, costs[0][after]) for after in range(c)], key=lambda x: x[1]) +)[:3] +cost_r1 = list( +    sorted([(after, costs[1][after]) for after in range(c)], key=lambda x: x[1]) +)[:3] +cost_r2 = list( +    sorted([(after, costs[2][after]) for after in range(c)], key=lambda x: x[1]) +)[:3] -for c0 in range(1, C + 1): -    for c1 in range(1, C + 1): -        if c0 == c1: -            continue -        for c2 in range(1, C + 1): -            if c0 == c2 or c1 == c2: -                continue -            cost = 0 -            for i in range(3): -                for j in range(1, C + 1): -                    next_color = [c0, c1, c2][i] -                    cost += color_count_by_r[i].get(j, 0) * d[j - 1][next_color - 1] -            ans = min(ans, cost) +for c0, cost0 in cost_r0: +    for c1, cost1 in cost_r1: +        for c2, cost2 in cost_r2: +            if c0 != c1 and c1 != c2 and c2 != c0: +                ans = min(ans, cost0 + cost1 + cost2)