Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
from itertools import permutations
N, C = map(int, input().split())
D = [int(_) for c in range(C) for _ in input().split()]
G = [int(_) for n in range(N) for _ in input().split()]

table = [0] * C * 3
for row in range(N):
    for col in range(N):
        i = (row + col) % 3
        for c in range(C):
            table[i * C + c] += D[C * (G[N * row + col] - 1) + c]


def cost(comb):
    return table[C * 0 + comb[0]] + table[C * 1 + comb[1]] + table[C * 2
                                                                   + comb[2]]


combs = permutations(range(C), 3)
min_cst = cost(min(combs, key=lambda comb: cost(comb)))
print(min_cst)


Fixed submission:
# -*- coding: utf-8 -*-
N, C = map(int, input().split())

D = []
for c in range(C):
    D.append([int(d) for d in input().split()])

Gmod = [[0 for i in range(C)] for i in range(3)]
for n in range(N):
    for m, g in enumerate(input().split()):
        Gmod[(n + m) % 3][int(g) - 1] += 1

K = []
for c0 in range(C):
    d0 = sum([Gmod[0][c] * D[c][c0] for c in range(C)])
    for c1 in [i for i in range(C) if not i == c0]:
        d1 = sum([Gmod[1][c] * D[c][c1] for c in range(C)])
        for c2 in [i for i in range(C) if (not i == c0) and (not i == c1)]:
            d2 = sum([Gmod[2][c] * D[c][c2] for c in range(C)])
            K.append(d0 + d1 + d2)

print(min(K))

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
import sys
# from itertools import permutations

i = sys.stdin
N, C = map(int, i.readline().strip().split())
D = [[int(_) for _ in i.readline().strip().split()] for c in range(C)]
G = [[int(_) for _ in i.readline().strip().split()] for c in range(N)]

table = [[0 for c in range(C)] for _ in range(3)]
for row in range(N):
    for col in range(N):
        i = (row + col) % 3
        for c in range(C):
            table[i][c] += D[G[row][col] - 1][c]

cst = 1000 * 500**2
for c0 in range(C):
    for c1 in range(C):
        if c0 == c1:
            continue
        for c2 in range(C):
            if c1 == c2 or c2 == c0:
                continue
            cst = min(cst, table[0][c0] + table[1][c1] + table[2][c2])

# combs = permutations(range(C), 3)
# for comb in combs:
#     cst = min(cst, table[0][comb[0]] + table[1][comb[1]] + table[2][comb[2]])

print(cst)


Fixed submission:
# -*- coding: utf-8 -*-
N, C = map(int, input().split())

D = []
for c in range(C):
    D.append([int(d) for d in input().split()])

Gmod = [[0 for i in range(C)] for i in range(3)]
for n in range(N):
    for m, g in enumerate(input().split()):
        Gmod[(n + m) % 3][int(g) - 1] += 1

K = []
for c0 in range(C):
    d0 = sum([Gmod[0][c] * D[c][c0] for c in range(C)])
    for c1 in [i for i in range(C) if not i == c0]:
        d1 = sum([Gmod[1][c] * D[c][c1] for c in range(C)])
        for c2 in [i for i in range(C) if (not i == c0) and (not i == c1)]:
            d2 = sum([Gmod[2][c] * D[c][c2] for c in range(C)])
            K.append(d0 + d1 + d2)

print(min(K))

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
import sys
from itertools import permutations

i = sys.stdin
N, C = map(int, i.readline().strip().split())
D, G = [], []
for c in range(C):
    D += [int(_) for _ in i.readline().strip().split()]
for n in range(N):
    G += [int(_) for _ in i.readline().strip().split()]

table0 = [0 for c in range(C)]
table1 = [0 for c in range(C)]
table2 = [0 for c in range(C)]
for row in range(N):
    for col in range(N):
        i = (row + col) % 3
        for c in range(C):
            if i == 0:
                table0[c] += D[(G[row * N + col] - 1) * C + c]
            elif i == 1:
                table1[c] += D[(G[row * N + col] - 1) * C + c]
            else:
                table2[c] += D[(G[row * N + col] - 1) * C + c]

cst = 1000 * 500**2
combs = permutations(range(C), 3)
for comb in combs:
    cst = min(cst, table0[comb[0]] + table1[comb[1]] + table2[comb[2]])

print(cst)


Fixed submission:
# -*- coding: utf-8 -*-
N, C = map(int, input().split())

D = []
for c in range(C):
    D.append([int(d) for d in input().split()])

Gmod = [[0 for i in range(C)] for i in range(3)]
for n in range(N):
    for m, g in enumerate(input().split()):
        Gmod[(n + m) % 3][int(g) - 1] += 1

K = []
for c0 in range(C):
    d0 = sum([Gmod[0][c] * D[c][c0] for c in range(C)])
    for c1 in [i for i in range(C) if not i == c0]:
        d1 = sum([Gmod[1][c] * D[c][c1] for c in range(C)])
        for c2 in [i for i in range(C) if (not i == c0) and (not i == c1)]:
            d2 = sum([Gmod[2][c] * D[c][c2] for c in range(C)])
            K.append(d0 + d1 + d2)

print(min(K))

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
import sys
from itertools import permutations

i = sys.stdin
N, C = map(int, i.readline().strip().split())
D = [[int(_) for _ in i.readline().strip().split()] for c in range(C)]
G = [[int(_) for _ in i.readline().strip().split()] for n in range(N)]

table0 = [0 for c in range(C)]
table1 = [0 for c in range(C)]
table2 = [0 for c in range(C)]
for row in range(N):
    for col in range(N):
        i = (row + col) % 3
        for c in range(C):
            if i == 0:
                table0[c] += D[G[row][col] - 1][c]
            elif i == 1:
                table1[c] += D[G[row][col] - 1][c]
            else:
                table2[c] += D[G[row][col] - 1][c]

cst = 1000 * 500**2
combs = permutations(range(C), 3)
for comb in combs:
    cst = min(cst, table0[comb[0]] + table1[comb[1]] + table2[comb[2]])

print(cst)


Fixed submission:
# -*- coding: utf-8 -*-
N, C = map(int, input().split())

D = []
for c in range(C):
    D.append([int(d) for d in input().split()])

Gmod = [[0 for i in range(C)] for i in range(3)]
for n in range(N):
    for m, g in enumerate(input().split()):
        Gmod[(n + m) % 3][int(g) - 1] += 1

K = []
for c0 in range(C):
    d0 = sum([Gmod[0][c] * D[c][c0] for c in range(C)])
    for c1 in [i for i in range(C) if not i == c0]:
        d1 = sum([Gmod[1][c] * D[c][c1] for c in range(C)])
        for c2 in [i for i in range(C) if (not i == c0) and (not i == c1)]:
            d2 = sum([Gmod[2][c] * D[c][c2] for c in range(C)])
            K.append(d0 + d1 + d2)

print(min(K))

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
import sys
from itertools import permutations

i = sys.stdin
N, C = map(int, i.readline().strip().split())
D = [[int(_) for _ in i.readline().strip().split()] for c in range(C)]
G = [[int(_) for _ in i.readline().strip().split()] for n in range(N)]

table = [[0 for c in range(C)] for _ in range(3)]
for c in range(C):
    for row in range(N):
        for col in range(N):
            table[(row + col) % 3][c] += D[G[row][col] - 1][c]

cst = 1000 * 500**2
combs = permutations(range(C), 3)
for comb in combs:
    cst = min(cst, table[0][comb[0]] + table[1][comb[1]] + table[2][comb[2]])

print(cst)


Fixed submission:
# -*- coding: utf-8 -*-
N, C = map(int, input().split())

D = []
for c in range(C):
    D.append([int(d) for d in input().split()])

Gmod = [[0 for i in range(C)] for i in range(3)]
for n in range(N):
    for m, g in enumerate(input().split()):
        Gmod[(n + m) % 3][int(g) - 1] += 1

K = []
for c0 in range(C):
    d0 = sum([Gmod[0][c] * D[c][c0] for c in range(C)])
    for c1 in [i for i in range(C) if not i == c0]:
        d1 = sum([Gmod[1][c] * D[c][c1] for c in range(C)])
        for c2 in [i for i in range(C) if (not i == c0) and (not i == c1)]:
            d2 = sum([Gmod[2][c] * D[c][c2] for c in range(C)])
            K.append(d0 + d1 + d2)

print(min(K))

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
from itertools import permutations
N, C = map(int, input().split())
D = [[int(_) for _ in input().split()] for i in range(C)]
G = [[int(_) for _ in input().split()] for i in range(N)]

table = [[0 for c in range(C)] for _ in range(3)]
for c in range(C):
    for row in range(N):
        for col in range(N):
            table[(row + col) % 3][c] += D[G[row][col] - 1][c]

cst = 1000 * 500**2
combs = permutations(range(C), 3)
for comb in combs:
    tmp = table[0][comb[0]] + table[1][comb[1]] + table[2][comb[2]]
    if tmp < cst:
        cst = tmp

print(cst)


Fixed submission:
# -*- coding: utf-8 -*-
N, C = map(int, input().split())

D = []
for c in range(C):
    D.append([int(d) for d in input().split()])

Gmod = [[0 for i in range(C)] for i in range(3)]
for n in range(N):
    for m, g in enumerate(input().split()):
        Gmod[(n + m) % 3][int(g) - 1] += 1

K = []
for c0 in range(C):
    d0 = sum([Gmod[0][c] * D[c][c0] for c in range(C)])
    for c1 in [i for i in range(C) if not i == c0]:
        d1 = sum([Gmod[1][c] * D[c][c1] for c in range(C)])
        for c2 in [i for i in range(C) if (not i == c0) and (not i == c1)]:
            d2 = sum([Gmod[2][c] * D[c][c2] for c in range(C)])
            K.append(d0 + d1 + d2)

print(min(K))

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
import functools
from itertools import permutations


@functools.lru_cache(maxsize=None)
def cost1(i, color):
    cost = 0
    for r in range(N):
        for c in range(N):
            if (r + c) % 3 == i:
                cost += D[G[r][c] - 1][color - 1]

    return cost


def cost2(comb):
    cost = 0
    for i, color in enumerate(comb):
        cost += cost1(i, color)

    return cost


N, C = map(int, input().split())
D = [[int(_) for _ in input().split()] for i in range(C)]
G = [[int(_) for _ in input().split()] for i in range(N)]
combs = permutations(range(1, C + 1), 3)
min_cost = min([cost2(comb) for comb in combs])
print(min_cost)


Fixed submission:
# -*- coding: utf-8 -*-
N, C = map(int, input().split())

D = []
for c in range(C):
    D.append([int(d) for d in input().split()])

Gmod = [[0 for i in range(C)] for i in range(3)]
for n in range(N):
    for m, g in enumerate(input().split()):
        Gmod[(n + m) % 3][int(g) - 1] += 1

K = []
for c0 in range(C):
    d0 = sum([Gmod[0][c] * D[c][c0] for c in range(C)])
    for c1 in [i for i in range(C) if not i == c0]:
        d1 = sum([Gmod[1][c] * D[c][c1] for c in range(C)])
        for c2 in [i for i in range(C) if (not i == c0) and (not i == c1)]:
            d2 = sum([Gmod[2][c] * D[c][c2] for c in range(C)])
            K.append(d0 + d1 + d2)

print(min(K))

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
import functools
from itertools import permutations


@functools.lru_cache(maxsize=None)
def cost1(i, color):
    cost = 0
    for r in range(N):
        for c in range(N):
            if (r + c) % 3 == i:
                cost += D[G[r][c] - 1][color - 1]

    return cost


def cost2(comb):
    cost = 0
    for i, color in enumerate(comb):
        cost += cost1(i, color)

    return cost


N, C = map(int, input().split())
D = [[int(_) for _ in input().split()] for i in range(C)]
G = [[int(_) for _ in input().split()] for i in range(N)]
combs = permutations(range(1, C + 1), 3)
min_cost = min([cost2(comb) for comb in combs])
print(min_cost)


Fixed submission:
# -*- coding: utf-8 -*-
N, C = map(int, input().split())

D = []
for c in range(C):
    D.append([int(d) for d in input().split()])

Gmod = [[0 for i in range(C)] for i in range(3)]
for n in range(N):
    for m, g in enumerate(input().split()):
        Gmod[(n + m) % 3][int(g) - 1] += 1

K = []
for c0 in range(C):
    d0 = sum([Gmod[0][c] * D[c][c0] for c in range(C)])
    for c1 in [i for i in range(C) if not i == c0]:
        d1 = sum([Gmod[1][c] * D[c][c1] for c in range(C)])
        for c2 in [i for i in range(C) if (not i == c0) and (not i == c1)]:
            d2 = sum([Gmod[2][c] * D[c][c2] for c in range(C)])
            K.append(d0 + d1 + d2)

print(min(K))

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
from itertools import permutations


def cost(comb):
    cost = 0
    for r in range(N):
        for c in range(N):
            if (r + c) % 3 == 0:
                cost += D[G[r][c] - 1][comb[0] - 1]
            elif (r + c) % 3 == 1:
                cost += D[G[r][c] - 1][comb[1] - 1]
            else:
                cost += D[G[r][c] - 1][comb[2] - 1]

    return cost


N, C = map(int, input().split())
D = [[int(_) for _ in input().split()] for i in range(C)]
G = [[int(_) for _ in input().split()] for i in range(N)]
combs = permutations(range(1, C + 1), 3)
min_cost = min([cost(comb) for comb in combs])
print(min_cost)


Fixed submission:
# -*- coding: utf-8 -*-
N, C = map(int, input().split())

D = []
for c in range(C):
    D.append([int(d) for d in input().split()])

Gmod = [[0 for i in range(C)] for i in range(3)]
for n in range(N):
    for m, g in enumerate(input().split()):
        Gmod[(n + m) % 3][int(g) - 1] += 1

K = []
for c0 in range(C):
    d0 = sum([Gmod[0][c] * D[c][c0] for c in range(C)])
    for c1 in [i for i in range(C) if not i == c0]:
        d1 = sum([Gmod[1][c] * D[c][c1] for c in range(C)])
        for c2 in [i for i in range(C) if (not i == c0) and (not i == c1)]:
            d2 = sum([Gmod[2][c] * D[c][c2] for c in range(C)])
            K.append(d0 + d1 + d2)

print(min(K))

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
# import functools

# @functools.lru_cache(maxsize=None)
# def paint(i, c):  # i = 0, 1, 2
#     cost = 0
#     for row in range(N):
#         for col in range(N):
#             if (row + col) % 3 == i:
#                 cost += D[G[row][col] - 1][c - 1]

#     return cost

# def cost(comb):
#     cost = 0
#     for i, c in enumerate(comb):
#         cost += paint(i, c)

#     return cost


def cost(comb):
    cost = 0
    for r in range(N):
        for c in range(N):
            if (r + c) % 3 == 0:
                cost += D[G[r][c] - 1][comb[0]]
            elif (r + c) % 3 == 1:
                cost += D[G[r][c] - 1][comb[1]]
            else:
                cost += D[G[r][c] - 1][comb[2]]


if __name__ == '__main__':
    from itertools import permutations
    N, C = map(int, input().split())
    D = [[int(_) for _ in input().split()] for i in range(C)]
    G = [[int(_) for _ in input().split()] for i in range(N)]
    combs = permutations(range(1, C + 1), 3)
    min_cost = min([cost(comb) for comb in combs])
    print(min_cost)


Fixed submission:
# -*- coding: utf-8 -*-
N, C = map(int, input().split())

D = []
for c in range(C):
    D.append([int(d) for d in input().split()])

Gmod = [[0 for i in range(C)] for i in range(3)]
for n in range(N):
    for m, g in enumerate(input().split()):
        Gmod[(n + m) % 3][int(g) - 1] += 1

K = []
for c0 in range(C):
    d0 = sum([Gmod[0][c] * D[c][c0] for c in range(C)])
    for c1 in [i for i in range(C) if not i == c0]:
        d1 = sum([Gmod[1][c] * D[c][c1] for c in range(C)])
        for c2 in [i for i in range(C) if (not i == c0) and (not i == c1)]:
            d2 = sum([Gmod[2][c] * D[c][c2] for c in range(C)])
            K.append(d0 + d1 + d2)

print(min(K))

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
import functools


@functools.lru_cache(maxsize=None)
def paint(i, c):  # i = 0, 1, 2
    cost = 0
    for row in range(N):
        for col in range(N):
            if (row + col) % 3 == i:
                cost += D[G[row][col] - 1][c - 1]

    return cost


def cost(comb):
    cost = 0
    for i in range(3):
        cost += paint(i, comb[i])

    return cost


if __name__ == '__main__':
    from itertools import permutations
    N, C = map(int, input().split())
    D = [[int(_) for _ in input().split()] for i in range(C)]
    G = [[int(_) for _ in input().split()] for i in range(N)]
    combs = permutations(range(1, C + 1), 3)
    min_cost = min([cost(comb) for comb in combs])
    print(min_cost)


Fixed submission:
# -*- coding: utf-8 -*-
N, C = map(int, input().split())

D = []
for c in range(C):
    D.append([int(d) for d in input().split()])

Gmod = [[0 for i in range(C)] for i in range(3)]
for n in range(N):
    for m, g in enumerate(input().split()):
        Gmod[(n + m) % 3][int(g) - 1] += 1

K = []
for c0 in range(C):
    d0 = sum([Gmod[0][c] * D[c][c0] for c in range(C)])
    for c1 in [i for i in range(C) if not i == c0]:
        d1 = sum([Gmod[1][c] * D[c][c1] for c in range(C)])
        for c2 in [i for i in range(C) if (not i == c0) and (not i == c1)]:
            d2 = sum([Gmod[2][c] * D[c][c2] for c in range(C)])
            K.append(d0 + d1 + d2)

print(min(K))

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
import functools


@functools.lru_cache(maxsize=None)
def paint(i, c):  # i = 0, 1, 2
    cost = 0
    for row in range(N):
        for col in range(N):
            if (row + col) % 3 == i:
                cost += D[G[row][col] - 1][c - 1]

    return cost


def cost(comb):
    cost = 0
    for i in range(3):
        cost += paint(i, comb[i])

    return cost


if __name__ == '__main__':
    from itertools import permutations
    N, C = map(int, input().split())
    D = [[int(_) for _ in input().split()] for i in range(C)]
    G = [[int(_) for _ in input().split()] for i in range(N)]
    combs = permutations(range(1, C + 1), 3)
    min_cost = min([cost(comb) for comb in combs])
    print(min_cost)


Fixed submission:
# -*- coding: utf-8 -*-
N, C = map(int, input().split())

D = []
for c in range(C):
    D.append([int(d) for d in input().split()])

Gmod = [[0 for i in range(C)] for i in range(3)]
for n in range(N):
    for m, g in enumerate(input().split()):
        Gmod[(n + m) % 3][int(g) - 1] += 1

K = []
for c0 in range(C):
    d0 = sum([Gmod[0][c] * D[c][c0] for c in range(C)])
    for c1 in [i for i in range(C) if not i == c0]:
        d1 = sum([Gmod[1][c] * D[c][c1] for c in range(C)])
        for c2 in [i for i in range(C) if (not i == c0) and (not i == c1)]:
            d2 = sum([Gmod[2][c] * D[c][c2] for c in range(C)])
            K.append(d0 + d1 + d2)

print(min(K))

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
def paint(i, c):  # i = 0, 1, 2
    cost = 0
    for row in range(N):
        for col in range(N):
            if (row + col) % 3 == i:
                cost += D[G[row][col] - 1][c - 1]

    return cost


def cost(comb):
    cost = 0
    for i in range(3):
        cost += paint(i, comb[i])

    return cost


if __name__ == '__main__':
    from itertools import permutations
    N, C = map(int, input().split())
    D = [[int(_) for _ in input().split()] for i in range(C)]
    G = [[int(_) for _ in input().split()] for i in range(N)]
    combs = permutations(range(1, C + 1), 3)
    min_cost = min([cost(comb) for comb in combs])
    print(min_cost)


Fixed submission:
# -*- coding: utf-8 -*-
N, C = map(int, input().split())

D = []
for c in range(C):
    D.append([int(d) for d in input().split()])

Gmod = [[0 for i in range(C)] for i in range(3)]
for n in range(N):
    for m, g in enumerate(input().split()):
        Gmod[(n + m) % 3][int(g) - 1] += 1

K = []
for c0 in range(C):
    d0 = sum([Gmod[0][c] * D[c][c0] for c in range(C)])
    for c1 in [i for i in range(C) if not i == c0]:
        d1 = sum([Gmod[1][c] * D[c][c1] for c in range(C)])
        for c2 in [i for i in range(C) if (not i == c0) and (not i == c1)]:
            d2 = sum([Gmod[2][c] * D[c][c2] for c in range(C)])
            K.append(d0 + d1 + d2)

print(min(K))