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)

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)
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)

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)

-N = int(s[0]) -C = int(s[1]) -d = [] -for i in range(C): +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): -    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) +    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)