### BUGGY CODE ###

N, C = map(int, input().split())              # 同上。map関数を使うとこのように書ける

D = [[0 for i in range(C)] for j in range(C)]
c = [[0 for i in range(N)] for j in range(N)]
# iwakan = [[[0 for i in range(N)] for j in range(N)] for k in range(C)]

for i in range(C):
    D[i] = list(map(int, input().split()))

for i in range(N):
    c[i] = list(map(int, input().split()))

# まず違和感リストを作る。
# for i in range(C):
#     for j in range(N):
#         for k in range(N):
#             iwakan[i][j][k] = D[c[j][k] - 1][i]

# 違和感リストから違和感の合計直を求める。
sum_min = float("inf")

for i in range(C):
    for j in range(C):
        for k in range(C):
            if (i == j) or (j == k) or (k == i):
                pass
            else:
                sum_tmp = 0
                for l in range(N):
                    for m in range(N):
                        if(l + m) % 3 == 0:
                            # sum_tmp = sum_tmp + iwakan[i][l][m]
                            sum_tmp = sum_tmp + D[c[l][m] - 1][i]
                        elif(l + m) % 3 == 1:
                            # sum_tmp = sum_tmp + iwakan[j][l][m]
                            sum_tmp = sum_tmp + D[c[l][m] - 1][j]
                        else:
                            # sum_tmp = sum_tmp + iwakan[k][l][m]
                            sum_tmp = sum_tmp + D[c[l][m] - 1][k]
                if sum_min > sum_tmp:
                    sum_min = sum_tmp

print(sum_min)

### FIXED CODE ###

# import time
N, C = map(int, input().split())              # 同上。map関数を使うとこのように書ける

D = [[0 for i in range(C)] for j in range(C)]
c = [[0 for i in range(N)] for j in range(N)]
# iwakan = [[[0 for i in range(N)] for j in range(N)] for k in range(C)]

for i in range(C):
    D[i] = list(map(int, input().split()))

for i in range(N):
    c[i] = list(map(int, input().split()))

# まず違和感リストを作る。
# for i in range(C):
#     for j in range(N):
#         for k in range(N):
#             iwakan[i][j][k] = D[c[j][k] - 1][i]

'''
Gij0 : (i + j) % 3 == 0 となる場所
Gij1 : (i + j) % 3 == 1 となる場所
Gij2 : (i + j) % 3 == 2 となる場所
とした時に、
Gijnに属する場所全ての色をaに塗り替える場合の違和感の合計を
iwakan[n][a]とする。
iwakan[n][a]を全て求めるための計算量は、30*500*500 = 7500000
iwakan を用いて全ての色の塗り替えの組み合わせを求めるための計算量は
30 * 29 * 28 = 24360
'''

# t1 = time.time()

# Gijnに属する色の数を合計したリストを作る。
numOfColors = [[0 for i in range(C)] for j in range(3)]
for i in range(N):
    for j in range(N):
        if (i + j) % 3 == 0:
            numOfColors[0][c[i][j] - 1] = numOfColors[0][c[i][j] - 1] + 1
        if (i + j) % 3 == 1:
            numOfColors[1][c[i][j] - 1] = numOfColors[1][c[i][j] - 1] + 1
        if (i + j) % 3 == 2:
            numOfColors[2][c[i][j] - 1] = numOfColors[2][c[i][j] - 1] + 1

iwakan = [[0 for i in range(C)] for j in range(3)]
for i in range(3):
    for j in range(C):
        for a in range(C):
            iwakan[i][j] = iwakan[i][j] + numOfColors[i][a] * D[a][j]

# t2 = time.time()

sum_min = float("inf")
for i in range(C):
    for j in range(C):
        for k in range(C):
            if (i == j) or (j == k) or (k == i):
                pass
            else:
                sum_tmp = iwakan[0][i] + iwakan[1][j] + iwakan[2][k]
                if sum_min > sum_tmp:
                    sum_min = sum_tmp

# t3 = time.time()

print(sum_min)

# elapsed_time1 = t2 - t1
# elapsed_time2 = t3 - t2
# print(f'time1: {elapsed_time1}')
# print(f'time2: {elapsed_time2}')