from itertools import product

n, c = list(map(int, input().split()))

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

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

colord = {(i+1, j+1):cost for i, row in enumerate(color_costs)

                         for j, cost in enumerate(row)}

 

cellgroups = [[] for _ in range(3)]

for i, row in enumerate(matrix):

  for j, color in enumerate(row):

    cellgroups[(i+1+j+1)%3].append(color)

  

cc = [[(color, sum(colord[cellcolor, color] for cellcolor in cells))

	       									for color in range(1,c+1)]

      										for cells in cellgroups]

 

dd = [sorted(xs, key=lambda x:x[1])[:3] for xs in cc]

ee = (a[1]+b[1]+c[1] for a, b, c in product(*dd) if len(set((a[0], b[0], c[0]))) == 3)

print((min(ee)))
from itertools import product

from collections import Counter

 

n, c = list(map(int, input().split()))

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

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

 

colord = {(i, j):cost 

               for i, row in enumerate(color_costs)

               for j, cost in enumerate(row)}

 

color_counts = []

for n in range(3):

  counter = Counter(c-1 for i, row in enumerate(matrix) 

                                       for j, c in enumerate(row) 

                                       if (i+j)%3==n)

  color_counts.append(counter)

 

cc = [[(color, sum(colord[cellcolor, color]*n for cellcolor, n in list(color_count.items())))

                                                                    for color in range(c)]

                                                                    for color_count in color_counts]

 

dd = [sorted(xs, key=lambda x:x[1])[:3] for xs in cc]

ee = (a[1]+b[1]+c[1] for a, b, c in product(*dd) if len(set((a[0], b[0], c[0]))) == 3)

print((min(ee)))
from itertools import product

n, c = list(map(int, input().split()))
color_costs = [list(map(int, input().split())) for _ in range(c)]
matrix = [list(map(int, input().split())) for _ in range(n)]
colord = {
    (i + 1, j + 1): cost
    for i, row in enumerate(color_costs)
    for j, cost in enumerate(row)
}
cellgroups = [[] for _ in range(3)]
for i, row in enumerate(matrix):
    for j, color in enumerate(row):
        cellgroups[(i + 1 + j + 1) % 3].append(color)
cc = [
    [
        (color, sum(colord[cellcolor, color] for cellcolor in cells))
        for color in range(1, c + 1)
    ]
    for cells in cellgroups
]
dd = [sorted(xs, key=lambda x: x[1])[:3] for xs in cc]
ee = (
    a[1] + b[1] + c[1] for a, b, c in product(*dd) if len(set((a[0], b[0], c[0]))) == 3
)
print((min(ee)))

from itertools import product
from collections import Counter

n, c = list(map(int, input().split()))
color_costs = [list(map(int, input().split())) for _ in range(c)]
matrix = [list(map(int, input().split())) for _ in range(n)]
colord = {
    (i, j): cost for i, row in enumerate(color_costs) for j, cost in enumerate(row)
}
color_counts = []
for n in range(3):
    counter = Counter(
        c - 1
        for i, row in enumerate(matrix)
        for j, c in enumerate(row)
        if (i + j) % 3 == n
    )
    color_counts.append(counter)
cc = [
    [
        (
            color,
            sum(
                colord[cellcolor, color] * n
                for cellcolor, n in list(color_count.items())
            ),
        )
        for color in range(c)
    ]
    for color_count in color_counts
]
dd = [sorted(xs, key=lambda x: x[1])[:3] for xs in cc]
ee = (
    a[1] + b[1] + c[1] for a, b, c in product(*dd) if len(set((a[0], b[0], c[0]))) == 3
)
print((min(ee)))

+from collections import Counter -    (i + 1, j + 1): cost -    for i, row in enumerate(color_costs) -    for j, cost in enumerate(row) +    (i, j): cost for i, row in enumerate(color_costs) for j, cost in enumerate(row) -cellgroups = [[] for _ in range(3)] -for i, row in enumerate(matrix): -    for j, color in enumerate(row): -        cellgroups[(i + 1 + j + 1) % 3].append(color) +color_counts = [] +for n in range(3): +    counter = Counter( +        c - 1 +        for i, row in enumerate(matrix) +        for j, c in enumerate(row) +        if (i + j) % 3 == n +    ) +    color_counts.append(counter) -        (color, sum(colord[cellcolor, color] for cellcolor in cells)) -        for color in range(1, c + 1) +        ( +            color, +            sum( +                colord[cellcolor, color] * n +                for cellcolor, n in list(color_count.items()) +            ), +        ) +        for color in range(c) -    for cells in cellgroups +    for color_count in color_counts