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

arrD = list(list(map(int, input().split())) for i in range(c))

arrC = list(list(map(int, input().split())) for i in range(n))

 

##剰余r=0,1,2で、

    ##色の出現数を確認する -> いずれかの色に統一する

        ##どの色に統一するか全パターンを実施して、違和感を算出

##和を取る



 ## arrCCnt[r][c]: 剰余rグループにおける初期色cのマス数

arrCCnt = []

for r in range(3):

    arrCCnt.append([0] * (c + 1))

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

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

        r = (i + j) % 3

        oriC = arrC[i - 1][j - 1]

        arrCCnt[r][oriC] += 1



ret = float('inf')

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

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

        if (c0 != c1):

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

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

                    arrToC = [c0, c1, c2]

                    buf = 0

                    for r in range(3):

                        for oriC in range(1, c + 1):

                            buf += arrD[oriC - 1][arrToC[r] - 1] * arrCCnt[r][oriC]

                    ret = min(ret, buf)

print(ret)
#次のコードを改変した

#https://abc099.contest.atcoder.jp/submissions/2657877

    #リストの内包表記を自分でも分かる書き方に直した

from itertools import product

from collections import Counter



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

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

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



res = []

for r in range(3):

    color_count = Counter(matrix[i][j] - 1 for i in range(n) for j in range(n) if (i + j) % 3 == r)



    def color_cost(color):

        return sum(color_costs[ccolor][color] * ccount for ccolor, ccount in list(color_count.items()))

    cx = [(color, color_cost(color)) for color in range(c)]

    sorted_by_cost = sorted(cx, key=lambda x:x[1])

    cheapest3 = sorted_by_cost[:3]

    res.append(cheapest3)



#print(res)

color_pickss = product(*res)

#for picks in color_pickss:

#    print(picks)

filtered_unique = (picks for picks in color_pickss if len(set(color for color, _ in picks)) == 3)

costs = (sum(cost for _, cost in picks) for picks in filtered_unique)

print((min(costs)))
n, c = list(map(int, input().split()))
arrD = list(list(map(int, input().split())) for i in range(c))
arrC = list(list(map(int, input().split())) for i in range(n))
##剰余r=0,1,2で、
##色の出現数を確認する -> いずれかの色に統一する
##どの色に統一するか全パターンを実施して、違和感を算出
##和を取る
## arrCCnt[r][c]: 剰余rグループにおける初期色cのマス数
arrCCnt = []
for r in range(3):
    arrCCnt.append([0] * (c + 1))
for i in range(1, n + 1):
    for j in range(1, n + 1):
        r = (i + j) % 3
        oriC = arrC[i - 1][j - 1]
        arrCCnt[r][oriC] += 1
ret = float("inf")
for c0 in range(1, c + 1):
    for c1 in range(1, c + 1):
        if c0 != c1:
            for c2 in range(1, c + 1):
                if (c1 != c2) and (c2 != c0):
                    arrToC = [c0, c1, c2]
                    buf = 0
                    for r in range(3):
                        for oriC in range(1, c + 1):
                            buf += arrD[oriC - 1][arrToC[r] - 1] * arrCCnt[r][oriC]
                    ret = min(ret, buf)
print(ret)

# 次のコードを改変した
# https://abc099.contest.atcoder.jp/submissions/2657877
# リストの内包表記を自分でも分かる書き方に直した
from itertools import product
from collections import Counter

n, c = list(map(int, input().split()))
color_costs = [list(map(int, input().split())) for _ in range(c)]
matrix = [list(map(int, input().split())) for _ in range(n)]
res = []
for r in range(3):
    color_count = Counter(
        matrix[i][j] - 1 for i in range(n) for j in range(n) if (i + j) % 3 == r
    )

    def color_cost(color):
        return sum(
            color_costs[ccolor][color] * ccount
            for ccolor, ccount in list(color_count.items())
        )

    cx = [(color, color_cost(color)) for color in range(c)]
    sorted_by_cost = sorted(cx, key=lambda x: x[1])
    cheapest3 = sorted_by_cost[:3]
    res.append(cheapest3)
# print(res)
color_pickss = product(*res)
# for picks in color_pickss:
#    print(picks)
filtered_unique = (
    picks for picks in color_pickss if len(set(color for color, _ in picks)) == 3
)
costs = (sum(cost for _, cost in picks) for picks in filtered_unique)
print((min(costs)))

+# 次のコードを改変した +# https://abc099.contest.atcoder.jp/submissions/2657877 +# リストの内包表記を自分でも分かる書き方に直した +from itertools import product +from collections import Counter + -arrD = list(list(map(int, input().split())) for i in range(c)) -arrC = list(list(map(int, input().split())) for i in range(n)) -##剰余r=0,1,2で、 -##色の出現数を確認する -> いずれかの色に統一する -##どの色に統一するか全パターンを実施して、違和感を算出 -##和を取る -## arrCCnt[r][c]: 剰余rグループにおける初期色cのマス数 -arrCCnt = [] +color_costs = [list(map(int, input().split())) for _ in range(c)] +matrix = [list(map(int, input().split())) for _ in range(n)] +res = [] -    arrCCnt.append([0] * (c + 1)) -for i in range(1, n + 1): -    for j in range(1, n + 1): -        r = (i + j) % 3 -        oriC = arrC[i - 1][j - 1] -        arrCCnt[r][oriC] += 1 -ret = float("inf") -for c0 in range(1, c + 1): -    for c1 in range(1, c + 1): -        if c0 != c1: -            for c2 in range(1, c + 1): -                if (c1 != c2) and (c2 != c0): -                    arrToC = [c0, c1, c2] -                    buf = 0 -                    for r in range(3): -                        for oriC in range(1, c + 1): -                            buf += arrD[oriC - 1][arrToC[r] - 1] * arrCCnt[r][oriC] -                    ret = min(ret, buf) -print(ret) +    color_count = Counter( +        matrix[i][j] - 1 for i in range(n) for j in range(n) if (i + j) % 3 == r +    ) + +    def color_cost(color): +        return sum( +            color_costs[ccolor][color] * ccount +            for ccolor, ccount in list(color_count.items()) +        ) + +    cx = [(color, color_cost(color)) for color in range(c)] +    sorted_by_cost = sorted(cx, key=lambda x: x[1]) +    cheapest3 = sorted_by_cost[:3] +    res.append(cheapest3) +# print(res) +color_pickss = product(*res) +# for picks in color_pickss: +#    print(picks) +filtered_unique = ( +    picks for picks in color_pickss if len(set(color for color, _ in picks)) == 3 +) +costs = (sum(cost for _, cost in picks) for picks in filtered_unique) +print((min(costs)))