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

D = []

for i in range(C):

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

    D.append(d)

A = []

for i in range(N):

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

    A.append(a)



X0,X1,X2 = [],[],[]

for i in range(N):

    for j in range(N):

        if (i+j)%3 == 0:

            X0.append(A[i][j])

        elif (i+j)%3 == 1:

            X1.append(A[i][j])

        else:

            X2.append(A[i][j])



# それぞれの集合を塗り替える場合のコストを計算

Xn = [X0,X1,X2]

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

for i in range(3):

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

        Ans = 0

        for x in Xn[i]:

            if x != c:

                Ans += D[x-1][c-1]

        cost[i][c-1] = Ans



Y0 = cost[0]

Y1 = cost[1]

Y2 = cost[2]

Ans = 1000000000000000

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, Y0[i]+Y1[j]+Y2[k])



print(Ans)

# Python3 (3.4.3)

import sys

input = sys.stdin.readline



# -------------------------------------------------------------

# function

# -------------------------------------------------------------





# -------------------------------------------------------------

# main

# -------------------------------------------------------------

# 入力の C は M とする

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

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

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



# X[x][y] : (i+j)%3 == x の, 初期状態での色が y(0-indexed) のマスの数

X = [[0 for _ in range(M)] for _ in range(3)]

for i in range(N):

    for j in range(N):

        x = (i+1 + j+1)%3

        y = C[i][j] - 1

        X[x][y] += 1



# Y[x][y] : (i+j)%3 == x の領域を y で塗る場合に必要なコスト

Y = [[0 for _ in range(M)] for _ in range(3)]

for i in range(3):

    for c_to in range(M):

        total = 0

        for c_from in range(M):

            total += X[i][c_from] * D[c_from][c_to]

        Y[i][c_to] = total



ans = 10**10

# 塗る色の組み合わせで全探索

for i in range(M):

    for j in range(M):

        for k in range(M):

            # 全ての色が異なる場合のみ考える

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

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



print(ans)
N, C = list(map(int, input().split()))
D = []
for i in range(C):
    d = list(map(int, input().split()))
    D.append(d)
A = []
for i in range(N):
    a = list(map(int, input().split()))
    A.append(a)
X0, X1, X2 = [], [], []
for i in range(N):
    for j in range(N):
        if (i + j) % 3 == 0:
            X0.append(A[i][j])
        elif (i + j) % 3 == 1:
            X1.append(A[i][j])
        else:
            X2.append(A[i][j])
# それぞれの集合を塗り替える場合のコストを計算
Xn = [X0, X1, X2]
cost = [[0 for _ in range(C)] for _ in range(3)]
for i in range(3):
    for c in range(1, C + 1):
        Ans = 0
        for x in Xn[i]:
            if x != c:
                Ans += D[x - 1][c - 1]
        cost[i][c - 1] = Ans
Y0 = cost[0]
Y1 = cost[1]
Y2 = cost[2]
Ans = 1000000000000000
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, Y0[i] + Y1[j] + Y2[k])
print(Ans)

# Python3 (3.4.3)
import sys

input = sys.stdin.readline
# -------------------------------------------------------------
# function
# -------------------------------------------------------------
# -------------------------------------------------------------
# main
# -------------------------------------------------------------
# 入力の C は M とする
N, M = list(map(int, input().split()))
D = [list(map(int, input().split())) for _ in range(M)]
C = [list(map(int, input().split())) for _ in range(N)]
# X[x][y] : (i+j)%3 == x の, 初期状態での色が y(0-indexed) のマスの数
X = [[0 for _ in range(M)] for _ in range(3)]
for i in range(N):
    for j in range(N):
        x = (i + 1 + j + 1) % 3
        y = C[i][j] - 1
        X[x][y] += 1
# Y[x][y] : (i+j)%3 == x の領域を y で塗る場合に必要なコスト
Y = [[0 for _ in range(M)] for _ in range(3)]
for i in range(3):
    for c_to in range(M):
        total = 0
        for c_from in range(M):
            total += X[i][c_from] * D[c_from][c_to]
        Y[i][c_to] = total
ans = 10**10
# 塗る色の組み合わせで全探索
for i in range(M):
    for j in range(M):
        for k in range(M):
            # 全ての色が異なる場合のみ考える
            if i != j and j != k and k != i:
                ans = min(ans, Y[0][i] + Y[1][j] + Y[2][k])
print(ans)

-N, C = list(map(int, input().split())) -D = [] -for i in range(C): -    d = list(map(int, input().split())) -    D.append(d) -A = [] -for i in range(N): -    a = list(map(int, input().split())) -    A.append(a) -X0, X1, X2 = [], [], [] +# Python3 (3.4.3) +import sys + +input = sys.stdin.readline +# function +# main +# 入力の C は M とする +N, M = list(map(int, input().split())) +D = [list(map(int, input().split())) for _ in range(M)] +C = [list(map(int, input().split())) for _ in range(N)] +# X[x][y] : (i+j)%3 == x の, 初期状態での色が y(0-indexed) のマスの数 +X = [[0 for _ in range(M)] for _ in range(3)] -        if (i + j) % 3 == 0: -            X0.append(A[i][j]) -        elif (i + j) % 3 == 1: -            X1.append(A[i][j]) -        else: -            X2.append(A[i][j]) -# それぞれの集合を塗り替える場合のコストを計算 -Xn = [X0, X1, X2] -cost = [[0 for _ in range(C)] for _ in range(3)] +        x = (i + 1 + j + 1) % 3 +        y = C[i][j] - 1 +        X[x][y] += 1 +# Y[x][y] : (i+j)%3 == x の領域を y で塗る場合に必要なコスト +Y = [[0 for _ in range(M)] for _ in range(3)] -    for c in range(1, C + 1): -        Ans = 0 -        for x in Xn[i]: -            if x != c: -                Ans += D[x - 1][c - 1] -        cost[i][c - 1] = Ans -Y0 = cost[0] -Y1 = cost[1] -Y2 = cost[2] -Ans = 1000000000000000 -for i in range(C): -    for j in range(C): -        for k in range(C): +    for c_to in range(M): +        total = 0 +        for c_from in range(M): +            total += X[i][c_from] * D[c_from][c_to] +        Y[i][c_to] = total +ans = 10**10 +# 塗る色の組み合わせで全探索 +for i in range(M): +    for j in range(M): +        for k in range(M): +            # 全ての色が異なる場合のみ考える -                Ans = min(Ans, Y0[i] + Y1[j] + Y2[k]) -print(Ans) +                ans = min(ans, Y[0][i] + Y[1][j] + Y[2][k]) +print(ans)