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



D = []

c = []



for i in range(C) :

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

    D.append(line)

for i in range(N) :

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

    c.append(line)



colorCount = [[0 for _ in range(C + 1)] for _ in range(3)]  # colorCount[index][color]

for i in range(N) :

    for j in range(N) :

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



ans = float('inf')

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

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

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

            if firstColor == secondColor or secondColor == thirdColor or thirdColor == firstColor :

                continue

            cost = 0

            for dist, toColor in enumerate([secondColor, thirdColor, firstColor]) :

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

                    if toColor == fromColor :

                        continue

                    cost += colorCount[dist][fromColor] * D[fromColor - 1][toColor - 1]

            ans = min(ans, cost)



print(ans)
from collections import defaultdict

from itertools import product



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

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



colors = defaultdict(lambda : defaultdict(int))



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

    for w, c in enumerate(map(int, input().split()), start=1):

        colors[(h + w) % 3][c] += 1



cost = [[None] * C for _ in range(3)]



for i in range(3):

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

        cnt = 0

        for c, k in list(colors[i].items()):

            cnt += D[c - 1][color - 1] * k

        cost[i][color - 1] = cnt



ans = 10**10

for cs in product(list(range(C)), repeat=3):

    if len(set(cs)) == 3:

        cnt = 0

        for i, c in enumerate(cs):

            cnt += cost[i][c]

        ans = min(ans, cnt)



print(ans)

N, C = list(map(int, input().split()))
D = []
c = []
for i in range(C):
    line = list(map(int, input().split()))
    D.append(line)
for i in range(N):
    line = list(map(int, input().split()))
    c.append(line)
colorCount = [[0 for _ in range(C + 1)] for _ in range(3)]  # colorCount[index][color]
for i in range(N):
    for j in range(N):
        colorCount[(i + j + 2) % 3][c[i][j]] += 1
ans = float("inf")
for firstColor in range(1, C + 1):
    for secondColor in range(1, C + 1):
        for thirdColor in range(1, C + 1):
            if (
                firstColor == secondColor
                or secondColor == thirdColor
                or thirdColor == firstColor
            ):
                continue
            cost = 0
            for dist, toColor in enumerate([secondColor, thirdColor, firstColor]):
                for fromColor in range(1, C + 1):
                    if toColor == fromColor:
                        continue
                    cost += colorCount[dist][fromColor] * D[fromColor - 1][toColor - 1]
            ans = min(ans, cost)
print(ans)

from collections import defaultdict
from itertools import product

N, C = list(map(int, input().split()))
D = [tuple(map(int, input().split())) for _ in range(C)]
colors = defaultdict(lambda: defaultdict(int))
for h in range(1, N + 1):
    for w, c in enumerate(map(int, input().split()), start=1):
        colors[(h + w) % 3][c] += 1
cost = [[None] * C for _ in range(3)]
for i in range(3):
    for color in range(1, C + 1):
        cnt = 0
        for c, k in list(colors[i].items()):
            cnt += D[c - 1][color - 1] * k
        cost[i][color - 1] = cnt
ans = 10**10
for cs in product(list(range(C)), repeat=3):
    if len(set(cs)) == 3:
        cnt = 0
        for i, c in enumerate(cs):
            cnt += cost[i][c]
        ans = min(ans, cnt)
print(ans)

+from collections import defaultdict +from itertools import product + -D = [] -c = [] -for i in range(C): -    line = list(map(int, input().split())) -    D.append(line) -for i in range(N): -    line = list(map(int, input().split())) -    c.append(line) -colorCount = [[0 for _ in range(C + 1)] for _ in range(3)]  # colorCount[index][color] -for i in range(N): -    for j in range(N): -        colorCount[(i + j + 2) % 3][c[i][j]] += 1 -ans = float("inf") -for firstColor in range(1, C + 1): -    for secondColor in range(1, C + 1): -        for thirdColor in range(1, C + 1): -            if ( -                firstColor == secondColor -                or secondColor == thirdColor -                or thirdColor == firstColor -            ): -                continue -            cost = 0 -            for dist, toColor in enumerate([secondColor, thirdColor, firstColor]): -                for fromColor in range(1, C + 1): -                    if toColor == fromColor: -                        continue -                    cost += colorCount[dist][fromColor] * D[fromColor - 1][toColor - 1] -            ans = min(ans, cost) +D = [tuple(map(int, input().split())) for _ in range(C)] +colors = defaultdict(lambda: defaultdict(int)) +for h in range(1, N + 1): +    for w, c in enumerate(map(int, input().split()), start=1): +        colors[(h + w) % 3][c] += 1 +cost = [[None] * C for _ in range(3)] +for i in range(3): +    for color in range(1, C + 1): +        cnt = 0 +        for c, k in list(colors[i].items()): +            cnt += D[c - 1][color - 1] * k +        cost[i][color - 1] = cnt +ans = 10**10 +for cs in product(list(range(C)), repeat=3): +    if len(set(cs)) == 3: +        cnt = 0 +        for i, c in enumerate(cs): +            cnt += cost[i][c] +        ans = min(ans, cnt)