Problem:
 Score : 400 points    Problem Statement There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i -th row from the top and the j -th column from the left.  These squares have to be painted in one of the C colors from Color 1 to Color C . Initially, (i,j) is painted in Color c_{i,j} .  We say the grid is a good grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N :   If (i+j) \% 3=(x+y) \% 3 , the color of (i,j) and the color of (x,y) are the same.  If (i+j) \%  3 \neq (x+y) \% 3 , the color of (i,j) and the color of (x,y) are different.   Here, X \% Y represents X modulo Y .  We will repaint zero or more squares so that the grid will be a good grid.  For a square, the wrongness when the color of the square is X before repainting and Y after repainting, is D_{X,Y} .  Find the minimum possible sum of the wrongness of all the squares.      Constraints  1 \leq N \leq 500  3 \leq C \leq 30  1 \leq D_{i,j} \leq 1000 (i \neq j),D_{i,j}=0 (i=j)  1 \leq c_{i,j} \leq C  All values in input are integers.         Input Input is given from Standard Input in the following format:  N  C  D_{1,1}  ...  D_{1,C}  :  D_{C,1}  ...  D_{C,C}  c_{1,1}  ...  c_{1,N}  :  c_{N,1}  ...  c_{N,N}       Output If the minimum possible sum of the wrongness of all the squares is x , print x .        Sample Input 1 2 3
0 1 1
1 0 1
1 4 0
1 2
3 3      Sample Output 1 3   Repaint (1,1) to Color 2 . The wrongness of (1,1) becomes D_{1,2}=1 .  Repaint (1,2) to Color 3 . The wrongness of (1,2) becomes D_{2,3}=1 .  Repaint (2,2) to Color 1 . The wrongness of (2,2) becomes D_{3,1}=1 .   In this case, the sum of the wrongness of all the squares is 3 .  Note that D_{i,j} \neq D_{j,i} is possible.       Sample Input 2 4 3
0 12 71
81 0 53
14 92 0
1 1 2 1
2 1 1 2
2 2 1 3
1 1 2 2      Sample Output 2 428

Solution (Python):
from itertools import permutations 
n,m=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)

Solution (Python):
import itertools
import sys
sys.setrecursionlimit(10**9)
input = lambda: sys.stdin.readline().rstrip()
inpl = lambda: list(map(int,input().split()))

N, C = inpl()
D = [inpl() for _ in range(C)]
c = [inpl() for _ in range(N)]

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

strangeness = [[sum(nc[k][oldc] * D[oldc][newc] for oldc in range(C))
                for newc in range(C)] for k in range(3)]
ans = 1000 * N * N
for xyz in itertools.product(range(C),repeat=3):
    x, y, z = xyz
    if x != y and y != z and z != x:
        s = sum(strangeness[k][xyz[k]] for k in range(3))
        if s < ans:
            ans = s
print(ans)

Solution (Python):
n,c=map(int,input().split())
d=[list(map(int,input().split())) for i in range(c)]
grid=[list(map(int,input().split())) for i in range(n)]
lst=[[0]*(c+1) for i in range(3)]
for i in range(n):
    for j in range(n):
        num=grid[i][j]
        a=(i+j)%3
        lst[a][num]+=1
li=[[0]*(c+1) for i in range(3)]
for i in range(3):
    u=lst[i]
    for j in range(1,c+1):
        count=0
        for k in range(1,c+1):
            count+=d[k-1][j-1]*u[k]
        li[i][j]=count
ans=float("inf")
for i in range(1,c+1):
    for j in range(1,c+1):
        for k in range(1,c+1):
            if i!=j and j!=k and k!=i:
                a=li[0][i]+li[1][j]+li[2][k]
                ans=min(ans,a)
print(ans)

Solution (Python):
import sys



def naiseki(va,vb):
	return sum([a*b for a,b in zip(va,vb)])

def main():
	n , c = map(int, input().split())
		
	ccc=[0]*c
	for i in range(c):
		ccc[i]=[0]*c
	for i in range(c):
		temp=list(map(int,input().split()))
		
		for j,v in enumerate(temp):
			ccc[j][i]=v
		
	pa(ccc)
		
	ddl=[0,0,0]
	for i in range(3):
		ddl[i]=[0]*c
		
	for i in range(1,n+1):
		ll=list(map(int, input().split()))
		for j, color_v in enumerate(ll,start=1):
			group=(i+j)%3
			ddl[group][color_v-1]+=1
			
	pa('_____ddl_____')
	for i in range(3):
		pa(ddl[i])
		
	rrb=[0]*3
	for i in range(3):
		rrb[i]=[0]*c	
		for j in range(c):
			rrb[i][j]=(naiseki(ddl[i], ccc[j]),j)
			
	pa('____rrb_____')
	for i in range(3):
		pa(rrb[i])
	
	c=[0]	* 3
	for i in range(3):
		rrb[i].sort()
		aa=rrb[i][:3]
		c[i]=aa
		
	pa('____ c _____')
	for i in range(3):
		pa(c[i])
		
	mm=None
	for i in range(3):
		for j in range(3):
			for k in range(3):
				ii=c[0][i][1]
				ji=c[1][j][1]
				ki=c[2][k][1]
				if ii !=ji and ji!=ki and ki!=ii:
					temp=c[0][i][0]+c[1][j][0]+c[2][k][0]
					
					mm = temp if mm is None else min(mm,temp)
					
	return mm
	
	

isTest=False

def pa(v):
	if isTest:
		print(v)

if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

Solution (Python):
import sys



def naiseki(va,vb):
	return sum([a*b for a,b in zip(va,vb)])

def main():
	n , c = map(int, input().split())
		
	ccc=[0]*c
	for i in range(c):
		ccc[i]=[0]*c
	for i in range(c):
		temp=list(map(int,input().split()))
		
		for j,v in enumerate(temp):
			ccc[j][i]=v
		
	pa(ccc)
		
	ddl=[0,0,0]
	for i in range(3):
		ddl[i]=[0]*c
		
	for i in range(1,n+1):
		ll=list(map(int, input().split()))
		for j, color_v in enumerate(ll,start=1):
			group=(i+j)%3
			ddl[group][color_v-1]+=1
			
	pa('_____ddl_____')
	for i in range(3):
		pa(ddl[i])
		
	rrb=[0]*3
	for i in range(3):
		rrb[i]=[0]*c	
		for j in range(c):
			rrb[i][j]=(naiseki(ddl[i], ccc[j]),j)
			
	pa('____rrb_____')
	for i in range(3):
		pa(rrb[i])
	
	c=[0]	* 3
	for i in range(3):
		rrb[i].sort()
		aa=rrb[i][:3]
		c[i]=aa
		
	pa('____ c _____')
	for i in range(3):
		pa(c[i])
		
	mm=None
	for i in range(3):
		for j in range(3):
			for k in range(3):
				ii=c[0][i][1]
				ji=c[1][j][1]
				ki=c[2][k][1]
				if ii !=ji and ji!=ki and ki!=ii:
					temp=c[0][i][0]+c[1][j][0]+c[2][k][0]
					
					mm = temp if mm is None else min(mm,temp)
					
	return mm
	
	

isTest=False

def pa(v):
	if isTest:
		print(v)

if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

Solution (Python):

import sys
input = sys.stdin.readline

from collections import Counter
from itertools import product


def main():
    N, C = map(int, input().split())
    D = [0] + list([0] + list(map(int, input().split())) for _ in range(C))
    grid = tuple(tuple(map(int, input().split())) for _ in range(N))
    cost = [Counter() for _ in range(3)]
    for i, g in enumerate(grid):  
        for j in range(3):
            cost[(i + j) % 3].update(g[j::3])
    dist = [[] for _ in range(3)]
    
    for i, tgt in product(range(3), range(1, C + 1)):
        cur = sum(D[src][tgt] * cnt for src, cnt in cost[i].items())
        dist[i].append(cur)
    ans = 1 << 60
    
    for i, j in product(range(C), repeat=2):
        if i == j:
            continue
        cur = dist[0][i] + dist[1][j]
        cur += min(x for k, x in enumerate(dist[2]) if k not in (i, j))
        ans = min(ans, cur)
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

import sys
input = sys.stdin.readline

from collections import defaultdict
from itertools import product


def main():
    N, C = map(int, input().split())
    D = [0] + list([0] + list(map(int, input().split())) for _ in range(C))
    grid = tuple(tuple(map(int, input().split())) for _ in range(N))
    cost = {i: defaultdict(int) for i in range(3)}
    for i, g in enumerate(grid):
        for j, x in enumerate(g):
            cost[(i + j) % 3][x] += 1
    dist = [[] for _ in range(3)]
    for i, tgt in product(range(3), range(1, C + 1)):
        cur = sum(D[src][tgt] * cnt for src, cnt in cost[i].items())
        dist[i].append(cur)
    ans = 1 << 60
    for i, j in product(range(C), repeat=2):
        if i == j:
            continue
        cur = dist[0][i] + dist[1][j]
        cur += min(x for k, x in enumerate(dist[2]) if k not in (i, j))
        ans = min(ans, cur)
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

import sys

readline = sys.stdin.buffer.readline

from collections import Counter
from itertools import product


def main():
    N, C = map(int, readline().split())
    D = [0] + list([0] + list(map(int, readline().split())) for _ in range(C))
    grid = tuple(tuple(map(int, readline().split())) for _ in range(N))
    cost = [Counter() for _ in range(3)]
    for i, g in enumerate(grid):
        for j in range(3):
            cost[j].update(g[(j - i) % 3 :: 3])
    dist = [[] for _ in range(3)]
    for i, tgt in product(range(3), range(1, C + 1)):
        cur = sum(D[src][tgt] * cnt for src, cnt in cost[i].items())
        dist[i].append(cur)
    ans = 1 << 60
    for i, j in product(range(C), repeat=2):
        if i == j:
            continue
        cur = dist[0][i] + dist[1][j]
        cur += min(x for k, x in enumerate(dist[2]) if k not in (i, j))
        ans = min(ans, cur)
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

import sys
input = sys.stdin.readline

from collections import Counter
from itertools import product


def main():
    N, C = map(int, input().split())
    D = [0] + list([0] + list(map(int, input().split())) for _ in range(C))
    grid = tuple(tuple(map(int, input().split())) for _ in range(N))
    cost = [Counter() for _ in range(3)]
    for i, g in enumerate(grid):
        for j in range(3):
            cost[j].update(g[(j - i) % 3 :: 3])
    dist = [[] for _ in range(3)]
    for i, target in product(range(3), range(1, C + 1)):
        cur = sum(D[source][target] * cnt for source, cnt in cost[i].items())
        dist[i].append(cur)
    ans = 1 << 60
    for i, j in product(range(C), repeat=2):
        if i == j:
            continue
        cur = dist[0][i] + dist[1][j]
        cur += min(x for k, x in enumerate(dist[2]) if k not in (i, j))
        ans = min(ans, cur)
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

import sys
input = sys.stdin.readline

from collections import Counter
from itertools import product


def main():
    N, C = map(int, input().split())
    D = [0] + list([0] + list(map(int, input().split())) for _ in range(C))
    grid = tuple(tuple(map(int, input().split())) for _ in range(N))
    cost = [Counter() for _ in range(3)]
    for i, g in enumerate(grid):
        for j in range(3):
            cost[j].update(g[(j - i) % 3 :: 3])
    dist = [[] for _ in range(3)]
    for i, target in product(range(3), range(1, C + 1)):
        cur = sum(D[source][target] * cnt for source, cnt in cost[i].items())
        dist[i].append(cur)
    ans = 1 << 60
    for i, j in product(range(C), repeat=2):
        if i == j:
            continue
        cur = dist[0][i] + dist[1][j]
        cur += min(dist[2][k] for k in range(C) if k not in (i, j))
        ans = min(ans, cur)
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
N,C = map(int, input().split())
D = [[int(d) for d in input().split()] for _ in range(C)]
clist = [[int(c) for c in input().split()] for _ in range(N)]

L0 = [0]*C
L1 = [0]*C
L2 = [0]*C

for i in range(N):
    for j in range(N):
        if (i+j+2)%3 == 0:
            L0[clist[i][j]-1] += 1
        elif (i+j+2)%3 == 1:
            L1[clist[i][j]-1] += 1
        else:
            L2[clist[i][j]-1] += 1
            
ans = 10**9
for i in range(C):
    for j in range(C):
        for k in range(C):
            if i != j and j != k and k != i:
                c = 0
                for l in range(C):
                    c += L0[l]*D[l][i]
                    c += L1[l]*D[l][j]
                    c += L2[l]*D[l][k]
                ans = min(ans, c)
                
print(ans)

Solution (Python):
n, c = [int(i) for i in input().split()]
D = [[int(i) for i in input().split()] for j in range(c)]
C = [[int(i) for i in input().split()] for j in range(n)]
 
X = [[0]*c for i in range(3)]
 
for i in range(n):
    for j in range(n):
        X[(i+j+2)%3][C[i][j]-1] += 1
 
cost = [[0]*c for i in range(3)]
 
for i in range(c):
    for j in range(c):
        cost[0][j] += X[0][i] * D[i][j]
        cost[1][j] += X[1][i] * D[i][j]
        cost[2][j] += X[2][i] * D[i][j]
 
ans = []
 
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.append(cost[0][i] + cost[1][j] + cost[2][k])
 
print(min(ans))

Solution (Python):
from collections import defaultdict
N, C = map(int, input().split())

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

d = [defaultdict(int) for i in range(3)]


for i in range(N):
    c = [int(x) for x in input().split()]
    for j in range(N):
        for k in range(C):
            d[(i+j) % 3][k] += D[c[j]-1][k]


ans = 10**9
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, d[0][i]+d[1][j]+d[2][k])

print(ans)

Solution (Python):
N, C = map(int, input().split())

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

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

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

def calc(g, color): 
    cost = 0
    for i in range(C):
        cost += group[g][i] * D[i][color]
    return cost




ans0 = []
for i in range(C):
    ans0.append((calc(0, i), i))
ans0.sort()

ans1 = []
for i in range(C):
    ans1.append((calc(1, i), i))
ans1.sort()

ans2 = []
for i in range(C):
    ans2.append((calc(2, i), i))
ans2.sort()





ans = 10 ** 18
for i in range(3):
    for j in range(3):
        for k in range(3):
            if ans0[i][1] != ans1[j][1] and ans1[j][1] != ans2[k][1] and ans2[k][1] != ans0[i][1]:
                ans = min(ans, ans0[i][0] + ans1[j][0] + ans2[k][0])

print (ans)

Solution (Python):
import itertools



def resolve():
    N,C=map(int,input().split())
    cost=[[int(i) for i in input().split()] for j in range(C)]
    count=[[0 for i in range(C)] for i in range(3)]
    grid=[[int(i)-1 for i in input().split()] for j in range(N)]
    for i in range(N):
        for j in range(N):
            count[(i+j)%3][grid[i][j]]+=1
    seq=list(range(C))
    mx=2**29
    for i in itertools.permutations(seq,3):
        temp=0
        temp+=sum([cost[j][i[0]]*count[0][j] for j in range(C)])
        temp+=sum([cost[j][i[1]]*count[1][j] for j in range(C)])
        temp+=sum([cost[j][i[2]]*count[2][j] for j in range(C)])
        mx=min(mx,temp)
    print(mx)

if __name__ == "__main__":
    resolve()

Solution (Python):
N,C = map(int,input().split())
D = [list(map(int,input().split())) for _ in range(C)]
Grid = [list(map(int,input().split())) for _ in range(N)]

SumCost = [[0 for _ in range(C)] for _ in range(3)]
for c in range(1,C+1):
  for i in range(N):
    for j in range(N):
      g = ((i+1)+(j+1))%3
      SumCost[g][c-1] += D[Grid[i][j]-1][c-1]

ans = 10**18
for c1 in range(1,C+1):
  for c2 in range(1,C+1):
    for c3 in range(1,C+1):
      if c1 == c2 or c2 == c3 or c3 == c1:
        continue
      ans = min(ans,SumCost[0][c1-1]+SumCost[1][c2-1]+SumCost[2][c3-1])
print(ans)

Solution (Python):
n,c=map(int,input().split())
d=[list(map(int,input().split())) for i in range(c)]
lis_c=[list(map(int,input().split())) for i in range(n)]
ans=float("inf")
cnt=[[0]*c for i in range(3)]
for i in range(n):
    for j in range(n):
        cnt[(i+j)%3][lis_c[i][j]-1]+=1

for i in range(c):
    for j in range(c):
        for k in range(c):
            if i==j or j==k or i==k:
                continue
            tmp=0
            color=[i,j,k]
            for l in range(3):
                for m in range(c):
                    tmp+=cnt[l][m]*d[m][color[l%3]]
            ans=min(ans,tmp)
print(ans)

Solution (Python):
from itertools import*
n,c=map(int,input().split());d=[list(map(int,input().split()))for _ in range(c)];e=[c*[0]for _ in range(3)]
for i in range(n):
  s=list(map(int,input().split()))
  for j in range(n):e[(i+j)%3][s[j]-1]+=1
if n==1:print(0);exit()
print(min(sum(d[t][i]*e[0][t]+d[t][j]*e[1][t]+d[t][k]*e[2][t]for t in range(c))for i,j,k in permutations((i for i in range(c)),3)))

Solution (Python):
N, C = map(int, input().split())
cost = []
c = []
for i in range(C):
    cost.append(list(map(int, input().split())))

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

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

ans = 10000000000
for i in range(1, C+1):
    for j in range(1, C+1):
        for k in range(1, C+1):
            if i == j or j == k or k == i:
                continue
            temp = 0
            for b in range(1, C+1):
                temp += cost[b-1][i-1] * color[0][b]
            for b in range(1, C+1):
                temp += cost[b-1][j-1] * color[1][b]
            for b in range(1, C+1):
                temp += cost[b-1][k-1] * color[2][b]
            ans = min(temp, ans)

print(ans)

Solution (Python):
n,c = map(int,input().split())
D = [list(map(int,input().split())) for _ in range(c)]
C = [list(map(int,input().split())) for _ in range(n)]

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

min_wrongness = 10**9

for i in range(c):
    for j in range(c):
        for k in range(c):
            if i!=j and j!=k and k!=i:
                wrongness = 0
                for l in range(3):
                    for m in range(c):
                        if l == 0:
                            wrongness += D[m][i]*color_count[l][m]
                        elif l == 1:
                            wrongness += D[m][j]*color_count[l][m]
                        else:
                            wrongness += D[m][k]*color_count[l][m]
                min_wrongness = min(min_wrongness,wrongness)
                
print(min_wrongness)

Solution (Python):
import sys
import numpy as np
input = sys.stdin.readline

def main():
    N, C = map(int, input().split())
    D = np.array([list(map(int, input().split())) for i in range(C)])
    board = [list(map(int, input().split())) for i in range(N)]

    
    cost = np.zeros((3, C), dtype=int)
    for i in range(N):
        for j in range(N):
            cost[(i+j)%3] += D[board[i][j] - 1]

    ans = int(1e20)
    for i in range(C):
        for j in range(C):
            if i == j:
                continue
            for k in range(C):
                if k==i or k==j:
                    continue
                ans = min(ans, cost[0][i] + cost[1][j] + cost[2][k])
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
import sys
import numpy as np
input = sys.stdin.readline

def main():
    N, C = map(int, input().split())
    D = np.array([list(map(int, input().split())) for i in range(C)])
    board = [list(map(int, input().split())) for i in range(N)]

    
    cost = np.zeros((3, C), dtype=int)
    count = np.zeros((3, C), dtype=int)
    for i in range(N):
        for j in range(N):
            count[(i + j)%3][board[i][j] - 1] += 1
    for i in range(3):
        for j in range(C):
            cost[i] += D[j] * count[i][j]

    ans = int(1e20)
    for i in range(C):
        for j in range(C):
            if i == j:
                continue
            for k in range(C):
                if k==i or k==j:
                    continue
                ans = min(ans, cost[0][i] + cost[1][j] + cost[2][k])
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
import itertools
N,C = map(int,input().split())
D = [[0 for _ in range(C)]]
for _ in range(C):
    row = list(map(int,input().split()))
    row.insert(0,0)
    D.append(row)
A = [[0 for _ in range(N)]]
for _ in range(N):
    row = list(map(int,input().split()))
    row.insert(0,0)
    A.append(row)
G = {0:{c:0 for c in range(1,C+1)},1:{c:0 for c in range(1,C+1)},2:{c:0 for c in range(1,C+1)}}
for i in range(1,N+1):
    for j in range(1,N+1):
        a = (i+j)%3
        G[a][A[i][j]] += 1
B = {0:{c:0 for c in range(1,C+1)},1:{c:0 for c in range(1,C+1)},2:{c:0 for c in range(1,C+1)}}
for c in range(1,C+1):
    for i in range(3):
        for d in range(1,C+1):
            B[i][c] += G[i][d]*D[d][c]
dmin = 10**10
for x in itertools.permutations(range(1,C+1),3):
        dmin = min(dmin,B[0][x[0]]+B[1][x[1]]+B[2][x[2]])
print(dmin)

Solution (Python):
N, C = 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)]

ccount = [[0] * C for _ in range(3)]
for y in range(N) :
    for x in range(N) :
        ccount[(x + y) % 3][c[y][x]-1] += 1

ccost = [[0] * C for _ in range(3)]
for i, cc in enumerate(ccount) :
    m = float('inf')
    for j in range(C) :
        ccost[i][j] = sum([cc[k] * D[k][j] for k in range(C)])
        
ret = float('inf')
for i in range(C) :
    for j in range(C) :
        if i == j :
            continue
        for k in range(C) :
            if i == k or j == k :
                continue
            ret = min(ret, ccost[0][i] + ccost[1][j] + ccost[2][k])
print(ret)

Solution (Python):
import collections
import itertools

n, c = map(int, input().split())
ds = [list(map(int, input().split())) for _ in range(c)]
colors = [collections.Counter() for _ in range(3)]
for i in range(n):
    for j, color in enumerate(map(int, input().split())):
        colors[(i + j) % 3][color - 1] += 1
res = float('inf')
for cs in itertools.permutations(range(c), 3):
    s = 0
    for i in range(3):
        for k, v in colors[i].items():
            s += ds[k][cs[i]] * v
    res = min(res, s)
print(res)

Solution (Python):
import collections
import itertools

n, c = map(int, input().split())
ds = [list(map(int, input().split())) for _ in range(c)]
colors = [collections.Counter() for _ in range(3)]
for i in range(n):
    for j, color in enumerate(map(int, input().split())):
        colors[(i + j) % 3][color - 1] += 1
res = float('inf')
for cs in itertools.combinations(range(c), 3):
    for cs2 in itertools.permutations(cs):
        s = 0
        for i in range(3):
            for k, v in colors[i].items():
                s += ds[k][cs2[i]] * v
        res = min(res, s)
print(res)

Solution (Python):


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

g = [[0]*C for _ in range(3)]
for y, e in enumerate(E):
    for x, e_i in enumerate(e):
        g[(x+y)%3][e_i-1] += 1


min_cost = 10**18
for c0 in range(C):
    for c1 in range(C):
        for c2 in range(C):
            if len({c0, c1, c2}) < 3:
                continue
            colors = [c0, c1, c2]
            
            cost = 0
            for i, g_i in enumerate(g):
                for j, g_j in enumerate(g_i):
                    
                    cost += D[j][colors[i]]*g_j
            min_cost = min(min_cost, cost)

print(min_cost)

Solution (Python):
N,C = [int(x) for x in input().split()]
D = [[int(x) for x in input().split()]for _ in range(C)]
C_grid = [[int(x) for x in input().split()] for _ in range(N)]
li=[[0]*C for _ in range(3)]
for i,s in enumerate(C_grid):
    for j,c in enumerate(s):
        li[(i+j)%3][c-1] += 1
ans = float('inf')
for x in range(C):
    for y in range(C):
        for z in range(C):
            diff = 0
            if(x==y or y==z or x==z):
                continue
            for i in range(C):
                diff += li[0][i]*D[i][x]
                diff += li[1][i]*D[i][y]
                diff += li[2][i]*D[i][z]
            ans = min(diff,ans)
print(ans)

Solution (Python):
from collections import defaultdict

def getlist():
	return list(map(int, input().split()))


N, C = getlist()
D = []
x0 = defaultdict(int)
x1 = defaultdict(int)
x2 = defaultdict(int)
for i in range(C):
	d = getlist()
	D.append(d)
for i in range(N):
	c = getlist()
	for j in range(N):
		if (i + j) % 3 == 0:
			x0[c[j]] += 1
		elif (i + j) % 3 == 1:
			x1[c[j]] += 1
		else:
			x2[c[j]] += 1


cost0 = [0] * (C)
cost1 = [0] * (C)
cost2 = [0] * (C)
for i in range(C):
	for j in x0:
		cost0[i] += D[j - 1][i] * x0[j]
for i in range(C):
	for j in x1:
		cost1[i] += D[j - 1][i] * x1[j]
for i in range(C):
	for j in x2:
		cost2[i] += D[j - 1][i] * x2[j]


ans = float("inf")
for i in range(C):
	for j in range(C):
		for k in range(C):
			val = cost0[i] + cost1[j] + cost2[k]
			if i != j and j != k and k != i:
				ans = min(ans, val)

print(ans)

Solution (Python):
N, C = map(int, input().split())
d = []
for i in range(C):
    tmp = [int(x) for x in input().split()]
    d.append(tmp)

c = []
for i in range(N):
    tmp = [int(x)-1 for x in input().split()]
    c.append(tmp)

x = [[], [], []] 
for cc in range(C):
    tx = [0] * 3
    for i in range(N):
        for j in range(N):
            tx[(i+j)%3] += d[c[i][j]][cc]
    for i in range(3):
        x[i].append(tx[i])

ans = 10 ** 9
for i in range(C):
    for j in range(C):
        for k in range(C):
            if i == j or j == k or k == i:
                continue
            tmp = x[0][i] + x[1][j] + x[2][k]
            if tmp < ans:
                ans = tmp
print(ans)

Solution (Python):
from collections import Counter,defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n,C = inpl()
d = []
c = []
for i in range(C):
    d.append(inpl())
for i in range(n):
    c.append(inpl())
d0 = defaultdict(int)
d1 = defaultdict(int)
d2 = defaultdict(int)
dd = [d0,d1,d2]
for y in range(n):
    for x in range(n):
        t = (x+y+2) % 3 
        dd[t][c[x][y]] += 1

res = [[] for i in range(3)]
for i in range(C):
    for j in range(3):
        tmp = 0
        for key in dd[j].keys():
            tmp += d[key-1][i] * dd[j][key]
        res[j].append([i,tmp])   
for i in range(3):
    res[i].sort(key=lambda x:x[1])

ans = mod
for i in range(3):
    for j in range(3):
        for k in range(3):
            if res[0][i][0] == res[1][j][0] or res[0][i][0] == res[2][k][0] or res[2][k][0] == res[1][j][0]:
                continue
            ans = min(res[0][i][1]+res[1][j][1]+res[2][k][1], ans)
print(ans)

Solution (Python):
from collections import Counter,defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n,C = inpl()
d = []
c = []
for i in range(C):
    d.append(inpl())
for i in range(n):
    c.append(inpl())
d0 = defaultdict(int)
d1 = defaultdict(int)
d2 = defaultdict(int)
for y in range(n):
    for x in range(n):
        if (x+y+2) % 3 == 0:
            d0[c[x][y]] += 1
        elif (x+y+2) % 3 == 1:
            d1[c[x][y]] += 1
        else:
            d2[c[x][y]] += 1



res0 = []
res1 = []
res2 = []
for i in range(C):
    tmp = 0
    for key in d0.keys():
        tmp += d[key-1][i] * d0[key]
    res0.append([i,tmp])
    tmp = 0
    for key in d1.keys():
        tmp += d[key-1][i] * d1[key]
    res1.append([i,tmp])
    tmp = 0
    for key in d2.keys():
        tmp += d[key-1][i] * d2[key]
    res2.append([i,tmp])
res0.sort(key=lambda x:x[1])
res1.sort(key=lambda x:x[1])
res2.sort(key=lambda x:x[1])



ans = mod
for i in range(3):
    for j in range(3):
        for k in range(3):
            if res0[i][0] == res1[j][0] or res0[i][0] == res2[k][0] or res2[k][0] == res1[j][0]:
                continue
            ans = min(res0[i][1]+res1[j][1]+res2[k][1], ans)
print(ans)

Solution (Python):
N,C =[int(x) for x in input().split()]
D = [[int(x) for x in input().split()] for _ in range(C)]
c = [[int(x) for x in input().split()] for _ in range(N)]
c1 = []
c2 = []
c3 = []
for i in range(1,N+1):
  for j in range(1,N+1):
    if (i+j)%3==0:
      c1.append(c[i-1][j-1])
    elif (i+j)%3==1:
      c2.append(c[i-1][j-1])
    elif (i+j)%3==2:
      c3.append(c[i-1][j-1])
su = 0
ans = 100000000000000
import collections as col
c1 = col.Counter(c1)
c2 = col.Counter(c2)
c3 = col.Counter(c3)

for i in range(C):
  for j in range(i+1,C):
    for k in range(j+1,C):
      for a,b,c in [[i,j,k],[j,i,k],[k,j,i],[i,k,j],[k,i,j],[j,k,i]]:
        su = 0
        for l in range(1,C+1):
          
          su += D[l-1][a]*c1[l]+D[l-1][b]*c2[l]+D[l-1][c]*c3[l]
        ans = min(ans,su)
print(ans)

Solution (Python):

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


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

for i in range(N):
    for j in range(N):
        idx = (i+j)%3
        colornum[idx][c[i][j]-1] += 1
        
        
colors = list(range(C))
import itertools 

ans = 10**9+7
for v in itertools.permutations(colors,3):
    val = 0

    for i in range(3):
        x = v[i]
        for j in range(C):
            if x == j:continue
            val += colornum[i][j] * D[j][x]
            
    ans = min(ans,val)
    
print(ans)

Solution (Python):
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))



N,C = II()
D = []
for i in range(C):
    D.append(III())
c = []
for i in range(N):
    c.append(III())

d1 = []
d2 = []
d3 = []
for i in range(N):
    for j in range(N):
        if (i+j)%3==0:
            d1.append(c[i][j])
        elif (i+j)%3==1:
            d2.append(c[i][j])
        else:
            d3.append(c[i][j])

d1val = []
d2val = []
d3val = []

for cl in range(C):
    temp1 = 0
    temp2 = 0
    temp3 = 0
    for dcl in d1:
        temp1 += D[dcl-1][cl]
    for dcl in d2:
        temp2 += D[dcl-1][cl]
    for dcl in d3:
        temp3 += D[dcl-1][cl]
    d1val.append(temp1)
    d2val.append(temp2)
    d3val.append(temp3)

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
            else:
                temp = d1val[i]+d2val[j]+d3val[k]
                if temp<ans:
                    ans = temp

print(ans)