import itertools

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

count_color = [[0 for _ in range(C+1)] for _ in range(3)]

for y in range(N):

  for x in range(N):

    count_color[(x+y)%3][G[y][x]] += 1

ans = 10**12

for colors in itertools.permutations(list(range(1, C+1)), 3):

  iwakan = 0

  for mod in range(3):

    for c in range(1, C+1):

      iwakan += D[c-1][colors[mod]-1]*count_color[mod][c]

  if iwakan < ans:

    ans = iwakan

print(ans)



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

GC = []

GC.append([0]*C)

GC.append([0]*C)

GC.append([0]*C)

for y in range(N):

  for x in range(N):

    GC[(y+x)%3][G[y][x]-1] += 1

ans = 1000000000

for a in range(C):

  for b in range(C):

    if a == b:

      continue

    for c in range(C):

      if a == c or b == c:

        continue

      cost = 0

      for i in range(C):

        cost += GC[0][i]*D[i][a]

        cost += GC[1][i]*D[i][b]

        cost += GC[2][i]*D[i][c]

      ans = min(ans, cost)

print(ans)



import itertools

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)]
count_color = [[0 for _ in range(C + 1)] for _ in range(3)]
for y in range(N):
    for x in range(N):
        count_color[(x + y) % 3][G[y][x]] += 1
ans = 10**12
for colors in itertools.permutations(list(range(1, C + 1)), 3):
    iwakan = 0
    for mod in range(3):
        for c in range(1, C + 1):
            iwakan += D[c - 1][colors[mod] - 1] * count_color[mod][c]
    if iwakan < ans:
        ans = iwakan
print(ans)

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)]
GC = []
GC.append([0] * C)
GC.append([0] * C)
GC.append([0] * C)
for y in range(N):
    for x in range(N):
        GC[(y + x) % 3][G[y][x] - 1] += 1
ans = 1000000000
for a in range(C):
    for b in range(C):
        if a == b:
            continue
        for c in range(C):
            if a == c or b == c:
                continue
            cost = 0
            for i in range(C):
                cost += GC[0][i] * D[i][a]
                cost += GC[1][i] * D[i][b]
                cost += GC[2][i] * D[i][c]
            ans = min(ans, cost)
print(ans)

-import itertools - -count_color = [[0 for _ in range(C + 1)] for _ in range(3)] +GC = [] +GC.append([0] * C) +GC.append([0] * C) +GC.append([0] * C) -        count_color[(x + y) % 3][G[y][x]] += 1 -ans = 10**12 -for colors in itertools.permutations(list(range(1, C + 1)), 3): -    iwakan = 0 -    for mod in range(3): -        for c in range(1, C + 1): -            iwakan += D[c - 1][colors[mod] - 1] * count_color[mod][c] -    if iwakan < ans: -        ans = iwakan +        GC[(y + x) % 3][G[y][x] - 1] += 1 +ans = 1000000000 +for a in range(C): +    for b in range(C): +        if a == b: +            continue +        for c in range(C): +            if a == c or b == c: +                continue +            cost = 0 +            for i in range(C): +                cost += GC[0][i] * D[i][a] +                cost += GC[1][i] * D[i][b] +                cost += GC[2][i] * D[i][c] +            ans = min(ans, cost)