from itertools import permutations

from collections import defaultdict



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

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



remains = {0: defaultdict(int), 1: defaultdict(int), 2: defaultdict(int)}

for i in range(1, N+1):

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

    for j in range(N):

        remains[(i+j+1)%3][line[j]] += 1



INF = float('inf')



def recolor(x, y, count):

    return D[x-1][y-1] * count



answer = INF

for choice in permutations(list(range(1, C+1)), 3):

    _answer = 0

    for i, c in enumerate(choice):

        _answer += sum([recolor(r, c, remains[i][r]) for r in remains[i] if c != r])

    answer = min(answer, _answer)



print(answer)
from itertools import permutations

from collections import defaultdict



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

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



remains = {0: defaultdict(int), 1: defaultdict(int), 2: defaultdict(int)}

for i in range(1, N+1):

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

    for j in range(N):

        remains[(i+j+1)%3][line[j]] += 1



INF = float('inf')



answer = INF

for choice in permutations(list(range(1, C+1)), 3):

    _answer = 0

    for i, c in enumerate(choice):

        _answer += sum([D[r-1][c-1] * remains[i][r] for r in remains[i] if c != r])

    answer = min(answer, _answer)



print(answer)
from itertools import permutations
from collections import defaultdict

N, C = list(map(int, input().split()))
D = [list(map(int, input().split())) for _ in range(C)]
remains = {0: defaultdict(int), 1: defaultdict(int), 2: defaultdict(int)}
for i in range(1, N + 1):
    line = list(map(int, input().split()))
    for j in range(N):
        remains[(i + j + 1) % 3][line[j]] += 1
INF = float("inf")


def recolor(x, y, count):
    return D[x - 1][y - 1] * count


answer = INF
for choice in permutations(list(range(1, C + 1)), 3):
    _answer = 0
    for i, c in enumerate(choice):
        _answer += sum([recolor(r, c, remains[i][r]) for r in remains[i] if c != r])
    answer = min(answer, _answer)
print(answer)

from itertools import permutations
from collections import defaultdict

N, C = list(map(int, input().split()))
D = [list(map(int, input().split())) for _ in range(C)]
remains = {0: defaultdict(int), 1: defaultdict(int), 2: defaultdict(int)}
for i in range(1, N + 1):
    line = list(map(int, input().split()))
    for j in range(N):
        remains[(i + j + 1) % 3][line[j]] += 1
INF = float("inf")
answer = INF
for choice in permutations(list(range(1, C + 1)), 3):
    _answer = 0
    for i, c in enumerate(choice):
        _answer += sum([D[r - 1][c - 1] * remains[i][r] for r in remains[i] if c != r])
    answer = min(answer, _answer)
print(answer)

- - -def recolor(x, y, count): -    return D[x - 1][y - 1] * count - - -        _answer += sum([recolor(r, c, remains[i][r]) for r in remains[i] if c != r]) +        _answer += sum([D[r - 1][c - 1] * remains[i][r] for r in remains[i] if c != r])