CodeEff/ECCO huggingface dataset:
# Original solution
# ContestName
# URL
import sys
#import numpy as np

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    N, C = i2nn()
    D = ii2nnn(C)
    c = ii2nnn(N)
    def main2(afters):
        #print('step %d' % cl)
        n = 0
        for i in range(N):
            for j in range(N):
                before = c[i][j]
                after = afters[(i + j) % 3]
                if before  == after:
                    continue
                iwakan = D[before-1][after-1]
                n += iwakan
        return n
    iwakan = N * N * 1000
    for c1 in range(C):
        for c2 in range(C):
            for c3 in range(C):
                if c1 == c2 or c2 == c3 or c3 == c1:
                    continue
                n = main2((c1+1, c2+1, c3+1))
                if n < iwakan:
                    iwakan = n
    print(iwakan)

main()


# Optimized solution
# ContestName
# URL
import sys
#import numpy as np

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    N, C = i2nn()
    D = ii2nnn(C)
    c = ii2nnn(N)
    E = [[0] * C for _ in range(3)]
    for i in range(N):
        for j in range(N):
            before = c[i][j]
            e = (i+j) % 3
            for after in range(1, C+1):
                E[e][after-1] += D[before-1][after-1]
    iwakan = N * N * 1000
    for c1 in range(C):
        for c2 in range(C):
            for c3 in range(C):
                if c1 == c2 or c2 == c3 or c3 == c1:
                    continue
                n = E[0][c1] + E[1][c2] + E[2][c3]
                if n < iwakan:
                    iwakan = n
    print(iwakan)

main()
# Original solution
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)]
ans0=[0]
ans1=[0]
ans2=[0]
for k in range(1,m+1):
    a0=0
    a1=0
    a2=0
    for i in range(n):
        for j in range(n):
            if (i+j+2)%3==0:
                a0+=d[c[i][j]-1][k-1]
            elif (i+j+2)%3==1:
                a1+=d[c[i][j]-1][k-1]
            else:
                a2+=d[c[i][j]-1][k-1]
    ans0.append(a0)
    ans1.append(a1)
    ans2.append(a2)
ans=float("inf")
import itertools
for x,y,z in itertools.permutations(list(range(1,m+1)),3):
    ans=min(ans,ans0[x]+ans1[y]+ans2[z])

print(ans)


# Optimized solution
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)]
x=[[0]*(C+1) for _ in range(3)]
for i in range(N):
    for j in range(N):
        x[(i+j+2)%3][c[i][j]]+=1

from itertools import permutations
ans=1<<32
for ch in permutations(list(range(1,C+1)),3):
    ans1=0
    for i in range(3):
        for j in range(1,C+1):
            ans1+=x[i][j]*d[j-1][ch[i]-1]
    ans=min(ans,ans1)
print(ans)
# Original solution
from itertools import*
import sys
input=sys.stdin.readline
n,c=list(map(int,input().split()))
cost=[list(map(int,input().split()))for i in range(c)]
d=[[],[],[]]
for i in range(n):
    for j,v in enumerate(map(int,input().split())):
        d[(i+j+2)%3].append(v-1)
ans=[]
for cmb in permutations(list(range(c)),3):
    ans.append(sum(sum(cost[i][cmb[dindex]]for i in d[dindex])for dindex in range(3)))
print((min(ans)))

# Optimized solution
from itertools import*
n,c=list(map(int,input().split()))
cost=[list(map(int,input().split()))for i in range(c)]
d=[[0]*c for i in range(3)]
for i in range(n):
    for j,v in enumerate(map(int,input().split())):
        d[(i+j)%3][v-1]+=1
print((min(sum(cost[j][cmb[i]]*d[i][j]for j in range(c)for i in range(3))for cmb in permutations(list(range(c)),3))))
# Original solution
n,c = [int(i) for i in input().split()]
d = []
for i in range(c):
    d.append(list(map(int, input().split())))
color = []
for i in range(n):
    color.append(list(map(int, input().split())))
wrongfeeling = [[0 for i in range(3)] for j in range(c)]
for a in range(c):
    for l in range(n):
        for row in range(n):
            wrongfeeling[a][(l+row)%3] += d[color[row][l]-1][a]
D = wrongfeeling[0][0]+wrongfeeling[1][1]+wrongfeeling[2][2]
for c0 in range (c):
    for c1 in range (c):
        if c1 == c0:
            continue
        for c2 in range (c):
            if c2 == c1 or c2 == c0:
                continue
            D = min(D,wrongfeeling[c0][0]+wrongfeeling[c1][1]+wrongfeeling[c2][2])
print(D)

# Optimized solution
n,c = [int(i) for i in input().split()]
d = [[int(i) for i in input().split()]for i in range(c)]
#d[A][B] はAからBになるときの違和感
color = [[int(i) for i in input().split()]for i in range(n)]

devcol = [[0 for i in range(3)] for j in range(c)]
for l in range(n):
        for row in range(n):
            devcol[color[l][row]-1][(l+row)%3] += 1
Dsum = [[0 for i in range(3)] for j in range(c)]
for i in range(c):
    for j in range(c):
        for k in range(3):
            Dsum[i][k] += d[j][i]*devcol[j][k] 
D = Dsum[0][0]+Dsum[1][1]+Dsum[2][2]
for c0 in range (c):
    for c1 in range (c):
        if c1 == c0:
            continue
        for c2 in range (c):
            if c2 == c1 or c2 == c0:
                continue
            D = min(D,Dsum[c0][0]+Dsum[c1][1]+Dsum[c2][2])
#O(c^3)のつもり
print(D)
# Original solution
from itertools import permutations
# use faster method than input()
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

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

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


# それぞれのグループで、color_iが何個あるかをカウント
mod_0 = [0]*C
mod_1 = [0]*C
mod_2 = [0]*C

for i in range(N):
    l = list(map(int, input().split()))
    for j in range(N):
        m = i+j
        color = l[j]-1
        if m % 3 == 0:
            mod_0[color] += 1
        elif m % 3 == 1:
            mod_1[color] += 1
        else:
            mod_2[color] += 1


# もしかしたら、貪欲かも
# 書き換え方を全通ためして、min
def brute_force():
    ans = float('inf')
    per = permutations(list(range(C)), 3)

    for c1, c2, c3 in per:
        cost = 0
        for i in range(C):
            cost += D[i][c1]*mod_0[i]
        for i in range(C):
            cost += D[i][c2]*mod_1[i]
        for i in range(C):
            cost += D[i][c3]*mod_2[i]
        ans = min(ans, cost)

    return ans


print((brute_force()))


# Optimized solution
from itertools import permutations
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

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

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


# それぞれのグループで、color_iが何個あるかをカウント
mod_0 = [0]*C
mod_1 = [0]*C
mod_2 = [0]*C

for i in range(N):
    l = list(map(int, input().split()))
    for j in range(N):
        m = i+j
        color = l[j]-1
        if m % 3 == 0:
            mod_0[color] += 1
        elif m % 3 == 1:
            mod_1[color] += 1
        else:
            mod_2[color] += 1


# 書き換え方を全通ためして、min
def solve():
    ans = float('inf')
    per = permutations(list(range(C)), 3)

    for c1, c2, c3 in per:
        cost = 0
        for i in range(C):
            cost += D[i][c1]*mod_0[i]
        for i in range(C):
            cost += D[i][c2]*mod_1[i]
        for i in range(C):
            cost += D[i][c3]*mod_2[i]
        ans = min(ans, cost)

    return ans


print((solve()))
# Original solution
N,C = list(map(int,input().split()))
D = [list(map(int,input().split())) for _ in range(C)]
c = [list([int(x)-1 for x in input().split()]) for _ in range(N)]

ans = 10000*N*N
for i in range(C):
    cur_score = 0
    for x in range(N):
        for y in range((-x)%3,N,3):
            cur_score += D[c[x][y]][i]
    if cur_score>ans:
        continue
    for j in range(C):
        cur_score2 = 0
        if j == i:
            continue
        for x in range(N):
            for y in range((1-x)%3,N,3):
                cur_score2 += D[c[x][y]][j]
        if cur_score+cur_score2>ans:
            continue
        
        for k in range(C):
            cur_score3 = 0
            if k == j or k == i:
                continue
            for x in range(N):
                for y in range((2-x)%3,N,3):
                    cur_score3 += D[c[x][y]][k]
            ans = min(ans,cur_score+cur_score2+cur_score3)
print(ans)

# Optimized solution
N,C = list(map(int,input().split()))
D = [list(map(int,input().split())) for _ in range(C)]
c = [list([int(x)-1 for x in input().split()]) for _ in range(N)]

n = [[0]*C for _ in range(3)]
for i in range(N):
    for j in range(N):
        n[(i+j)%3][c[i][j]] += 1

ans = 500*500*1000
for c0 in range(C):
    for c1 in range(C):
        if c1 == c0:
            continue
        for c2 in range(C):
            if c2 in (c0,c1):
                continue
            score = 0
            for a in range(C):
                score += n[0][a] * D[a][c0] + n[1][a] * D[a][c1] + n[2][a] * D[a][c2]
            ans = min(ans,score)
print(ans)
# Original solution
s=input().split()
N=int(s[0])
C=int(s[1])
d=[]
for i in range(C):
    s=input().split()
    temp=[]
    for item in s:
        temp.append(int(item))
    d.append(temp)
if N==1:
    print("0")
else:
    c=[[],[],[]]
    cost=[[],[],[]]
    for j in range(N):
        s=input().split()
        for i in range(len(s)):
            c[(i+j)%3].append(int(s[i])-1)
    for i in range(C):
        s=0
        for col in c[0]:
            s+=d[col][i]
        cost[0].append((s,i))
        s=0
        for col in c[1]:
            s+=d[col][i]
        cost[1].append((s,i))
        s=0
        for col in c[2]:
            s+=d[col][i]
        cost[2].append((s,i))
    res=100000000000
    for i in range(C):
        for j in range(C):
            for k in range(C):
                if cost[0][i][1]!=cost[1][j][1] and cost[1][j][1]!=cost[2][k][1] and cost[2][k][1]!=cost[0][i][1]:
                    res=min(res,cost[0][i][0]+cost[1][j][0]+cost[2][k][0])
    print(res)


# Optimized solution
s=input().split()
n=int(s[0])
c=int(s[1])
d=[[0 for i in range(c+1)] for j in range(c+1)]
for i in range(c):
	s=input().split()
	for j in range(c):
		d[i+1][j+1]=int(s[j])
cnt=[[0 for i in range(c+1)],[0 for i in range(c+1)],[0 for i in range(c+1)]]
for i in range(n):
	s=input().split()
	for j in range(n):
		cnt[(i+j)%3][int(s[j])]+=1
		cnt[(i+j)%3][0]+=1
iwa=[[0 for i in range(c+1)],[0 for i in range(c+1)],[0 for i in range(c+1)]]
for i in range(1,c+1):
    for j in range(1,c+1):
        for k in range(3):
            iwa[k][i]+=d[j][i]*cnt[k][j]
m=10**9
for i in range(1,1+c):
    for j in range(1,1+c):
        for k in range(1,1+c):
            if i==j or j==k or k==i:
                continue
            m=min(m,iwa[0][i]+iwa[1][j]+iwa[2][k])
print(m)
# Original solution
N,C = list(map(int,input().split()))
D = []
for i in range(C):
    D.append(list(map(int,input().split())))
c = []
for i in range(N):
    c.append(list(map(int,input().split())))

current_minimum = 1000 * 500 * 500 + 1

import itertools
for (c0,c1,c2) in itertools.permutations(list(range(C)), 3):
	color = [c0,c1,c2]
	tmp = 0
	for (i,j) in itertools.product(list(range(N)),list(range(N))):
		current_color = c[i][j]
		target_color = color[((i+1)+(j+1)) % 3]
		tmp += D[current_color - 1][target_color-1]
	if current_minimum >= tmp:
		current_minimum = tmp
print(current_minimum)

# Optimized solution
N,C = list(map(int,input().split()))
D = []
for i in range(C):
    D.append(list(map(int,input().split())))
c = []
for i in range(N):
    c.append(list(map(int,input().split())))

import itertools
current_minimum = 1000 * 500 * 500 + 1
Z = []
Z.append([0]*C)
Z.append([0]*C)
Z.append([0]*C)
#print(Z)

for (i,j) in itertools.product(list(range(N)),list(range(N))):
	cur_mod = ((i+1)+(j+1)) % 3
	cur_color = c[i][j]
	#print(cur_mod)
	#print(cur_color)
	Z[cur_mod][cur_color-1] += 1

current_minimum = 1000 * 500 * 500 + 1
for (c0,c1,c2) in itertools.permutations(list(range(C)), 3):
	tmp = 0
	color = [c0,c1,c2]
	for cur_mod in range(3):
		target_color = color[cur_mod]
		for cur_color in range(C):
			tmp += Z[cur_mod][cur_color] * D[cur_color][target_color]
	if current_minimum >= tmp:
		current_minimum = tmp
print(current_minimum)
# Original solution
N,C = list(map(int,input().split()))
D = [list(map(int,input().split())) for _ in range(C)]

G = [list(map(int,input().split())) for _ in range(N)]
g1 = []
g2 = []
g3 = []
for i in range(N):
    for j in range(N):
        G[i][j] -= 1
        if (i+j)%3 == 0:
            g1.append(G[i][j])
        if (i+j)%3 == 1:
            g2.append(G[i][j])
        if (i+j)%3 == 2:
            g3.append(G[i][j])
# print(g1) # グループ1の色リスト
# print(g2)
# print(g3)
# for k in range(C):
#     for i in range(C):
#         for j in range(C):
#             D[i][j] = min(D[i][j], D[i][k] + D[k][j])

ans = float('inf')
for c1 in range(C):
    for c2 in range(C):
        for c3 in range(C):
            temp = 0
            if c1 == c2 or c2 == c3 or c3 == c1:
                continue
            temp += sum(D[clr_g1][c1] for clr_g1 in g1)
            temp += sum(D[clr_g2][c2] for clr_g2 in g2)
            temp += sum(D[clr_g3][c3] for clr_g3 in g3)
            ans = min(ans,temp)
print(ans)

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

G = [list(map(int,input().split())) for _ in range(N)]
g1 = []
g2 = []
g3 = []
for i in range(N):
    for j in range(N):
        G[i][j] -= 1
        if (i+j)%3 == 0:
            g1.append(G[i][j])
        if (i+j)%3 == 1:
            g2.append(G[i][j])
        if (i+j)%3 == 2:
            g3.append(G[i][j])



cost = [[0]*3 for _ in range(C)]
for c in range(C):
    for g in range(3):
        if g == 0:
            cost[c][0] = sum(D[clr_g1][c] for clr_g1 in g1)
        if g == 1:
            cost[c][1] = sum(D[clr_g2][c] for clr_g2 in g2)
        if g == 2:
            cost[c][2] = sum(D[clr_g3][c] for clr_g3 in g3)



ans = float('inf')
for c1 in range(C):
    for c2 in range(C):
        for c3 in range(C):
            temp = 0
            if c1 == c2 or c2 == c3 or c3 == c1:
                continue
            # temp += sum(D[clr_g1][c1] for clr_g1 in g1)
            # temp += sum(D[clr_g2][c2] for clr_g2 in g2)
            # temp += sum(D[clr_g3][c3] for clr_g3 in g3)
            temp = cost[c1][0] + cost[c2][1] + cost[c3][2]
            ans = min(ans,temp)
print(ans)
# Original solution
N,C = list(map(int,input().split()))
D = [[int(i) for i in input().split()] for _ in range(C)]
c = [[int(i) for i in input().split()] for _ in range(N)]
modl = [[] for i in range(3)]
for i in range(N):
    for j in range(N):
        modl[((i+1)+(j+1))%3].append(c[i][j])
ans = float('inf')
for i in range(C):
    total = 0
    tmp1 = 0
    for e in modl[0]:
        tmp1 += D[e-1][i]
    total += tmp1
    for j in range(C):
        if i!=j:
            tmp2 = 0
            for e in modl[1]:
                tmp2 += D[e-1][j]
            total += tmp2
            for k in range(C):
                tmp3 = 0
                if i!=k and j!=k:
                    for e in modl[2]:
                        tmp3 += D[e-1][k]
                    total += tmp3
                    ans = min(ans,total)
                    total -= tmp3
            total -= tmp2
print(ans)


# Optimized solution
N,C = list(map(int,input().split()))
D = [[int(i) for i in input().split()] for _ in range(C)]
c = [[int(i) for i in input().split()] for _ in range(N)]
modl = [[0 for i in range(C)] for i in range(3)]
for i in range(N):
    for j in range(N):
        modl[((i+1)+(j+1))%3][c[i][j]-1] += 1
ans = float('inf')
for i in range(C):
    for j in range(C):
        for k in range(C):
            if i==j or i==k or j==k:
                continue
            total = 0
            for l in range(C):
                total += D[l][i]*modl[0][l]+D[l][j]*modl[1][l]+D[l][k]*modl[2][l]
            ans = min(ans,total)
print(ans)
# Original solution
lngN, lngC = list(map(int, input().split()))

##  lstD[x][y]
##  1 オリジン の C × C グリッド として扱うために、[False] * lngC を第一行と第一列に追加
##  x -> y に塗り替えたときの違和感
##  x, y: 0 ... lngC - 1
lstD = []
lstD.append([False] * lngC)
for i in range(lngC):
    lstD.append([False] + list(map(int, input().split())))
##  lstC[i][j]
##  1 オリジン の N × N グリッド として扱うために、[False] * lngN を第一行と第一列に追加
##  マス(i, j)の初期色
##  1 から C までの値を取る
lstC = []
lstC.append([False] * lngN)
for i in range(lngN):
    lstC.append([False] + list(map(int, input().split())))
##  lstDSum[r][c]
##  剰余 r のグループを 色 c で塗るときの違和感の和
##　[c]を 1 オリジン として扱うために、[False] * lngC を第一行に追加
##  r: 0, 1, 2
##  c: 1 ... lngC
lstDSum = [False] * 3
for r in range(3):
    lstDSum[r] = [False] * (lngC + 1)
    for c in range(1, lngC + 1):
        lstDSum[r][c] = 0##違和感の初期値

for i in range(1, lngN + 1):
    for j in range(1, lngN + 1):
        for c in range(1, lngC + 1):
            r = (i + j) % 3##　剰余
            oriC = lstC[i][j]##初期色
            lstDSum[r][c] += lstD[oriC][c]

ans = float('inf')
for i in range(1, lngC + 1):
    for j in range(1, lngC + 1):
        if (i != j):
            for k in range(1, lngC + 1):
                if (i != k) and (j != k):
                    ans = min(ans, lstDSum[0][i] + lstDSum[1][j] + lstDSum[2][k])
print(ans)

##グリッドのマス(i, j)とすると,
##(i + j) % 3 の値によって, マスは 3 グループに分かれる (0, 1, 2)

##各グループの色分けの仕方は, itertools.permutations(c, 3) 通り
##内訳は次の通り
    ##(0)色 1 から C までの C 通りに色分け
    ##(1)色 1 から C までの (C - 1) 通りに色分け ((0) 分を除外)
    ##(2)色 1 から C までの (C - 2) 通りに色分け ((0, 1) 分を除外)

##各グループについて
##色 1 から C までの C 通りに色分けしたときの違和感の和を配列化
##lstDSum[グループ番号][色]
    ##グループ番号: 0, 1, 2
    ##色: 1 から C まで

##for 文で, 各グループを塗る色を全探索
##(グループ番号, 色) = (0, i), (1, j), (2, k)とするときの違和感の和
##lstDSum[0][i] + lstDSum[1][j] + lstDSum[2][k]
##これの最小値を取ればよい


# Optimized solution
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)
# Original solution
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)]

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(N):
                for j in range(N):
                    next_color = [c0, c1, c2][(i + j) % 3]
                    cost += d[c[i][j] - 1][next_color - 1]
            ans = min(ans, cost)
print(ans)

# Optimized solution
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)
# Original solution
# -*- coding: utf-8 -*-
from itertools import permutations
N, C = list(map(int, input().split()))
D = [int(_) for c in range(C) for _ in input().split()]
G = [int(_) for n in range(N) for _ in input().split()]

table = [0] * C * 3
for row in range(N):
    for col in range(N):
        i = (row + col) % 3
        for c in range(C):
            table[i * C + c] += D[C * (G[N * row + col] - 1) + c]


def cost(comb):
    return table[C * 0 + comb[0]] + table[C * 1 + comb[1]] + table[C * 2
                                                                   + comb[2]]


combs = permutations(list(range(C)), 3)
min_cst = cost(min(combs, key=lambda comb: cost(comb)))
print(min_cst)


# Optimized solution
# -*- coding: utf-8 -*-
N, C = list(map(int, input().split()))

D = []
for c in range(C):
    D.append([int(d) for d in input().split()])

Gmod = [[0 for i in range(C)] for i in range(3)]
for n in range(N):
    for m, g in enumerate(input().split()):
        Gmod[(n + m) % 3][int(g) - 1] += 1

K = []
for c0 in range(C):
    d0 = sum([Gmod[0][c] * D[c][c0] for c in range(C)])
    for c1 in [i for i in range(C) if not i == c0]:
        d1 = sum([Gmod[1][c] * D[c][c1] for c in range(C)])
        for c2 in [i for i in range(C) if (not i == c0) and (not i == c1)]:
            d2 = sum([Gmod[2][c] * D[c][c2] for c in range(C)])
            K.append(d0 + d1 + d2)

print((min(K)))
# Original solution
from itertools import permutations 
n,m=list(map(int,input().split()))
cost=[]
for i in range(m):
    cost.append([int(i) for i in input().split()])
clr=[]
for i in range(n):
    clr.append([int(i) for i in input().split()])
l=[i for i in range(m)]
ind=[[] for i in range(4)]
for i in range(n):
    for j in range(n):
        curr=(i+1+j+1)%3 
        ind[curr].append(clr[i][j]-1)
mini=10**9 
for i in range(m):
    for j in range(m):
        for k in range(m):
            if i==j or i==k or j==k:
                continue 
            curr=0 
            for x in ind[0]:
                curr+=cost[x][i]
            for x in ind[1]:
                curr+=cost[x][j]
            for x in ind[2]:
                curr+=cost[x][k]
            mini=min(mini,curr)
print(mini)


# Optimized solution
from itertools import permutations 
n,m=list(map(int,input().split()))
cost=[]
for i in range(m):
    cost.append([int(i) for i in input().split()])
clr=[]
for i in range(n):
    clr.append([int(i) for i in input().split()])
l=[i for i in range(m)]
ind=[[0 for j in range(m)] for i in range(4)]
for i in range(n):
    for j in range(n):
        curr=(i+1+j+1)%3 
        ind[curr][clr[i][j]-1]+=1 
mini=10**9 
for i in range(m):
    for j in range(m):
        for k in range(m):
            if i==j or i==k or j==k:
                continue 
            curr=0 
            for it in range(3): 
                for it1 in range(m): 
                    if it==0:
                        k1=i 
                    elif it==1:
                        k1=j 
                    else:
                        k1=k 
                    curr+=ind[it][it1]*cost[it1][k1] 
            mini=min(mini,curr)
print(mini)