CodeEff/ECCO huggingface dataset:
# Original solution
# 1. 入出力 (二次元配列)

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

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

ans = 1 << 60
for i in range(C):
    for j in range(C):
        for k in range(C):
            if i == j or j == k or k == i:
                continue

            temp = 0
            lst = [i, j, k]

            for x in range(N):
                for y in range(N):
                    mod = (x + y) % 3
                    temp += D[ col[x][y] - 1 ][ lst[mod] ]

            ans = min(ans, temp)
            # print(lst, temp)

print(ans)

# Optimized solution
# 1. 入出力 (二次元配列)

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

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

ans = 1 << 60
info = [ [0 for j in range(C)] for i in range(3) ]

for i in range(N):
    for j in range(N):
        mod = (i + j) % 3
        info[mod][ col[i][j] - 1 ] += 1

for i in range(C):
    for j in range(C):
        for k in range(C):
            if i == j or j == k or k == i:
                continue

            temp = 0
            lst = [i, j, k]

            for x in range(3):
                for y in range(C):
                    temp += D[y][ lst[x] ] * info[x][y]

            ans = min(ans, temp)
            # print(lst, temp)

print(ans)
# Original solution
from collections import Counter
from itertools import permutations
import sys
input = sys.stdin.buffer.readline
N, C = list(map(int, input().split()))
D = [[int(x) for x in input().split()] for _ in range(C)]

cnt = [Counter() for _ in range(3)]

for i in range(N):
    c = [int(x)-1 for x in input().split()]
    for j in range(3):
        cnt[j].update(c[(3-(i+2)+j)%3::3])

ans = 1000*500*500+5
for p in permutations(list(range(C)), 3):
    s = 0
    for j in range(3):
        for k, v in list(cnt[j].items()):
            s += D[k][p[j]] * v
    ans = min(ans, s)

print(ans)

# Optimized solution
from collections import Counter
from itertools import permutations

def main():
    N, C = list(map(int, input().split()))
    D = [[int(x) for x in input().split()] for _ in range(C)]

    cnt = [Counter() for _ in range(3)]

    for i in range(N):
        c = [int(x)-1 for x in input().split()]
        for j in range(3):
            cnt[j].update(c[(3-(i+2)+j)%3::3])

    ans = 1000*500*500+5
    for p in permutations(list(range(C)), 3):
        s = 0
        for j in range(3):
            for k, v in list(cnt[j].items()):
                s += D[k][p[j]] * v
        ans = min(ans, s)

    print(ans)

main()
# Original solution
from itertools import product, permutations
n, c = list(map(int, input().split()))
d = [list(map(int, input().split())) for _ in range(c)]
grid = [list(map(int, input().split())) for _ in range(n)]
cost = [[0] * c for _ in range(3)]

for i, j, k in product(list(range(n)), list(range(n)), list(range(c))):
    # cost[0][k] -> 余りが0になる場所の色をkに変えるときのコスト
    cost[(i + j + 2) % 3][k] += d[grid[i][j] - 1][k]

ans = []
for s, t, u in permutations(list(range(c)), r=3):
    ans.append(cost[0][s] + cost[1][t] + cost[2][u])
print((min(ans)))

# Optimized solution
#%% D
from itertools import product, permutations
n, c = list(map(int, input().split()))
D = [list(map(int, input().split())) for _ in range(c)]
C = [list(map(int, input().split())) for _ in range(n)]


X = [[0] * c for i in range(3)]

for i in range(n):
    for j in range(n):
        X[(i + j + 2) % 3][C[i][j] - 1] += 1

cost = [[0] * c for i in range(3)]

for i in range(c):
    for j in range(c):
        cost[0][j] += X[0][i] * D[i][j]
        cost[1][j] += X[1][i] * D[i][j]
        cost[2][j] += X[2][i] * D[i][j]

ans = []
for s, t, u in permutations(list(range(c)), r=3):
    ans.append(cost[0][s] + cost[1][t] + cost[2][u])
print((min(ans)))
# Original solution
import sys
input = sys.stdin.buffer.readline

def main():
    n, c = list(map(int, input().split()))
    D = [list(map(int, input().split())) for _ in range(c)]
    C_ = [list(map(int, input().split())) for _ in range(n)]
    C = [0]*n
    for i, c_ in enumerate(C_):
        c_ = [x-1 for x in c_]
        C[i] = c_

    #print(C)
    L = [[0]*c for _ in range(3)]
    for k in range(c):
        for i in range(n):
            for j in range(n):
                m = (i+j)%3
                x = C[i][j]
                L[m][k] += D[x][k]
                
    X = [[] for _ in range(3)]
    for j in range(3):
        for i, x in enumerate(L[j]):
            X[j].append((x, i))
    for i, x in enumerate(X):
        x.sort()
        x = x[0:3]
        X[i] = x

    #print(X)
    import itertools
    ans = 10**18
    for p in itertools.product(list(range(3)), repeat=3):
        used_col = set()
        temp = 0
        for j in range(3):
            x, i =X[j][p[j]]
            if i in used_col:
                break
            else:
                temp += x
                used_col.add(i)
        else:
            ans = min(temp, ans)
    print(ans)

if __name__ == '__main__':
    main()


# Optimized solution
import sys
input = sys.stdin.buffer.readline

def main():
    n, c = list(map(int, input().split()))
    D = [list(map(int, input().split())) for _ in range(c)]
    C_ = [list(map(int, input().split())) for _ in range(n)]
    C = [0]*n
    for i, c_ in enumerate(C_):
        c_ = [x-1 for x in c_]
        C[i] = c_

    X = [[0]*c for _ in range(3)]
    for i in range(n):
        for j in range(n):
            m = (i+j)%3
            X[m][C[i][j]] += 1

    #print(C)
    import itertools
    ans = 10**18
    for p in itertools.permutations(list(range(c)), 3):
        temp = 0
        for i in range(3):
            for j in range(c):
                temp += D[j][p[i]]*X[i][j]
        ans = min(ans, temp)
    print(ans)

if __name__ == '__main__':
    main()
# Original solution
n, c = list(map(int, input().split()))
D = [list(map(int, input().split())) for _ in range(c)]
C = [list(map(int, input().split())) for _ in range(n)]

for i, l in enumerate(C):
    l = [x-1 for x in l]
    C[i] = l

import itertools
ans = 10**18
for p in itertools.permutations(list(range(c)), 3):
    temp = 0
    for i in range(n):
        for j in range(n):
            x = C[i][j]
            y = p[(i+j)%3]
            temp += D[x][y]
    ans = min(temp, ans)
print(ans)


# Optimized solution
n, c = list(map(int, input().split()))
D = [list(map(int, input().split())) for _ in range(c)]
C = [list(map(int, input().split())) for _ in range(n)]

for i, l in enumerate(C):
    l = [x-1 for x in l]
    C[i] = l

import itertools
ans = 10**18

X = [[0]*3 for _ in range(c)]
for i in range(n):
    for j in range(n):
        x = C[i][j]
        X[x][(i+j)%3] += 1


for p in itertools.permutations(list(range(c)), 3):
    temp = 0
    for i in range(3):
        for j in range(c):
            temp += X[j][i]*(D[j][p[i]])
    ans = min(temp, ans)
print(ans)
# Original solution
N,C=list(map(int,input().split()))

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

a=[[0]*C for i in range(3)]


for i in range(N):
    tmp=i%3
    for j in range(N):
        a[tmp][grid[i][j]-1]+=1
        tmp=(tmp+1)%3

b=[[0]*C for i in range(3)]
for i in range(3):
    for j in range(C):
        b[i][j]=sum([ D[k][j] * a[i][k] for k in range(C)])

b=[ sorted([ [b[i][j],j] for j in range(C)]) for i in range(3)]

ans=10**15
for i in range(3):
    for j in range(3):
        for k in range(3):
            if b[0][i][1]!=b[1][j][1] and b[1][j][1]!=b[2][k][1] and b[2][k][1]!=b[0][i][1]:
                ans=min(ans,b[0][i][0]+b[1][j][0]+b[2][k][0])

print(ans)

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

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

a=[[0]*C for i in range(3)]


for i in range(N):
    for j in range(N):
        a[(i+j)%3][grid[i][j]-1]+=1

b=[[0]*C for i in range(3)]
for i in range(3):
    for j in range(C):
        b[i][j]=[ sum([ D[k][j] * a[i][k] for k in range(C)]), j]

b=[ sorted(b[i]) for i in range(3)]

ans=10**10
for i in range(3):
    for j in range(3):
        for k in range(3):
            if b[0][i][1]!=b[1][j][1] and b[1][j][1]!=b[2][k][1] and b[2][k][1]!=b[0][i][1]:
                ans=min(ans,b[0][i][0]+b[1][j][0]+b[2][k][0])

print(ans)
# Original solution
N,C=list(map(int,input().split()))

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

a=[[0]*C for i in range(3)]


for i in range(N):
    for j in range(N):
        a[(i+j)%3][grid[i][j]-1]+=1

b=[[0]*C for i in range(3)]
for i in range(3):
    for j in range(C):
        b[i][j]=[ sum([ D[k][j] * a[i][k] for k in range(C)]), j]

b=[ sorted(b[i]) for i in range(3)]

ans=10**10
for i in range(3):
    for j in range(3):
        for k in range(3):
            if b[0][i][1]!=b[1][j][1] and b[1][j][1]!=b[2][k][1] and b[2][k][1]!=b[0][i][1]:
                ans=min(ans,b[0][i][0]+b[1][j][0]+b[2][k][0])

print(ans)

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

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

a=[[0]*C for i in range(3)]


for i in range(N):
    tmp=i%3
    for j in range(N):
        a[tmp][grid[i][j]-1]+=1
        tmp=(tmp+1)%3

b=[[0]*C for i in range(3)]
for i in range(3):
    for j in range(C):
        b[i][j]=sum([ D[k][j] * a[i][k] for k in range(C)])

b=[ sorted([ [b[i][j],j] for j in range(C)]) for i in range(3)]

ans=1000000000
for i in range(3):
    for j in range(3):
        for k in range(3):
            if b[0][i][1]!=b[1][j][1] and b[1][j][1]!=b[2][k][1] and b[2][k][1]!=b[0][i][1]:
                ans=min(ans,b[0][i][0]+b[1][j][0]+b[2][k][0])

print(ans)
# Original solution
#!/usr/bin/env python3
import sys

def calc(order, groups, D):
    ret = 0
    for i, G in enumerate(groups):
        target = order[i]
        for elm in G:
            #print(elm, target)
            ret += D[elm - 1][target]
    return ret

def dfs(idx, current, used, groups, D):
    if idx == 3:
        ret = calc(current, groups, D)
        return ret
    ret = float('inf')
    for i, u in enumerate(used):
        if not u:
            used[i] = True
            tmp = dfs(idx + 1, current + [i], used, groups, D)
            ret = min(ret, tmp)
            used[i] = False
    return ret

def solve(N: int, C: int, D: "List[List[int]]", c: "List[List[int]]"):
    groups = [[] for _ in range(3)]
    for i in range(N):
        for j in range(N):
            cc = c[i][j]
            groups[(i + j) % 3].append(cc)
    #print(groups)
    ret = dfs(0, [], [False] * C, groups, D)
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    D = [ [ int(next(tokens)) for _ in range(C) ] for _ in range(C) ]  # type: "List[List[int]]"
    c = [ [ int(next(tokens)) for _ in range(N) ] for _ in range(N) ]  # type: "List[List[int]]"
    solve(N, C, D, c)

if __name__ == '__main__':
    main()


# Optimized solution
#!/usr/bin/env python3
import sys

def calc_(order, d):
    ret = 0
    for i in range(3):
        target = order[i]
        ret += d[i][target]
    return ret

def calc(G, target, D):
    ret = 0
    for elm in G:
        ret += D[elm - 1][target]
    return ret

def dfs(idx, current, used, d):
    if idx == 3:
        ret = calc_(current, d)
        return ret
    ret = float('inf')
    for i, u in enumerate(used):
        if not u:
            used[i] = True
            tmp = dfs(idx + 1, current + [i], used, d)
            ret = min(ret, tmp)
            used[i] = False
    return ret

def solve(N: int, C: int, D: "List[List[int]]", c: "List[List[int]]"):
    groups = [[] for _ in range(3)]
    for i in range(N):
        for j in range(N):
            cc = c[i][j]
            groups[(i + j) % 3].append(cc)
    #print(groups)
    d = [[] for _ in range(3)]
    for i, G in enumerate(groups):
        for j in range(C):
            d[i].append(calc(G, j, D))
    ret = dfs(0, [], [False] * C, d)
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    D = [ [ int(next(tokens)) for _ in range(C) ] for _ in range(C) ]  # type: "List[List[int]]"
    c = [ [ int(next(tokens)) for _ in range(N) ] for _ in range(N) ]  # type: "List[List[int]]"
    solve(N, C, D, c)

if __name__ == '__main__':
    main()
# Original solution
N,C=list(map(int,input().split()))

Dmat=[]
for i in range(C):
  array=list(map(int,input().split()))
  Dmat.append(array)
#print(Dmat)

cmat=[]
for i in range(N):
  array=list(map(int,input().split()))
  cmat.append(array)
#print(cmat)
grid_list=[{},{},{}]
for i in range(N):
  for j in range(N):
    c=cmat[i][j]-1
    mod=(i+j)%3
    if c in grid_list[mod]:
      grid_list[mod][c]+=1
    else:
      grid_list[mod][c]=1
#print(grid_list)

min_cost=10**9
for i in range(C):
  for j in range(C):
    if i==j:
      continue
      
    for k in range(C):
      if i==k or j==k:
        continue
          
      cost=0 
      for c in grid_list[0]:
        num=grid_list[0][c]
        cost+=Dmat[c][i]*num
      for c in grid_list[1]:
        num=grid_list[1][c]
        cost+=Dmat[c][j]*num
      for c in grid_list[2]:
        num=grid_list[2][c]
        cost+=Dmat[c][k]*num
          
      if cost<min_cost:
        #print(i,j,k,cost)
        min_cost=cost
          
print(min_cost)

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

Dmat=[]
for i in range(C):
  array=list(map(int,input().split()))
  Dmat.append(array)
#print(Dmat)

grid_list=[{},{},{}]
for i in range(N):
  array=list(map(int,input().split()))
  for j in range(N):
    c=array[j]-1    
    mod=(i+j)%3
    if c in grid_list[mod]:
      grid_list[mod][c]+=1
    else:
      grid_list[mod][c]=1
#print(grid_list)

min_cost=10**9
for i in range(C):
  for j in range(C):
    if i==j:
      continue
      
    for k in range(C):
      if i==k or j==k:
        continue
          
      cost=0 
      for c,num in list(grid_list[0].items()):
        cost+=Dmat[c][i]*num
      for c,num in list(grid_list[1].items()):
        cost+=Dmat[c][j]*num
      for c,num in list(grid_list[2].items()):
        cost+=Dmat[c][k]*num
          
      if cost<min_cost:
        #print(i,j,k,cost)
        min_cost=cost
          
print(min_cost)
# Original solution
from collections import defaultdict
from itertools import permutations

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

dcnt = [defaultdict(int) for _ in range(3)]
for i, ci in enumerate(cmat):
    for j, cij in enumerate(ci):
        mod = (i + j) % 3
        dcnt[mod][cij - 1] += 1

ans = 10 ** 9
for color in permutations(list(range(c)), 3):
    tmp = 0
    for tgt, cnt in zip(color, dcnt):
        tmp += sum(d[k][tgt] * cnt[k] for k in cnt)
    ans = min(ans, tmp)
print(ans)


# Optimized solution
from itertools import permutations

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

dcnt = [[0] * c for _ in range(3)]
for i, ci in enumerate(cmat):
    for j, cij in enumerate(ci):
        mod = (i + j) % 3
        dcnt[mod][cij - 1] += 1

ans = 10 ** 9
for color in permutations(list(range(c)), 3):
    tmp = 0
    for tgt, cnt in zip(color, dcnt):
        tmp += sum(d[i][tgt] * cnt[i] for i in range(c))
    ans = min(ans, tmp)
print(ans)
# Original solution
N, C = list(map(int, input().split()))
D = [list(map(int, input().split())) for i in range(C)]
c = [list(map(int, input().split())) for i in range(N)]
col = [[0]*3 for i in range(C)]
for i in range(N):
    for j in range(N):
        p = c[i][j]-1
        for k in range(C):
            col[k][(i+j)%3] += D[p][k]
ans = 10**18
for i in range(C):
    for j in range(C):
        if i == j:
            continue
        for k in range(C):
            if i == k or j == k:
                continue
            ans = min(ans, col[i][0] + col[j][1] + col[k][2])
print(ans)

# Optimized solution
N, C = list(map(int, input().split()))
D = [list(map(int, input().split())) for i in range(C)]
c = [list(map(int, input().split())) for i in range(N)]
col = [[0]*C for i in range(3)]
for i in range(N):
    ci = c[i]
    for j in range(N):
        cc = col[(i+j)%3]
        dd = D[ci[j]-1]
        for k in range(C):
            cc[k] += dd[k]
ans = 10**18
c0 = col[0]
c1 = col[1]
c2 = col[2]
for i in range(C):
    for j in range(C):
        if i == j:
            continue
        for k in range(C):
            if i == k or j == k:
                continue
            ans = min(ans, c0[i] + c1[j] + c2[k])
print(ans)
# Original solution
from itertools import permutations
n, c = list(map(int, input().split()))
D = [list(map(int, input().split())) for _ in range(c)]
C = [list(map(int, input().split())) for _ in range(n)]

colors = [[0]*c for _ in range(3)]
for i in range(n):
    for j in range(n):
        c_ij = C[i][j]-1
        index = (i+j)%3
        for col in range(c):
            colors[index][col] += D[c_ij][col]

ans = 1000*500**2
for c0, c1, c2 in permutations(list(range(c)), 3):
    diff = colors[0][c0] + colors[1][c1] + colors[2][c2]
    ans = min(ans, diff)
print(ans)


# Optimized solution
from itertools import permutations
n, c = list(map(int, input().split()))
D = [list(map(int, input().split())) for _ in range(c)]
C = [list(map(int, input().split())) for _ in range(n)]

colors = [[0]*c for _ in range(3)]
for i in range(n):
    for j in range(n):
        c_ij = C[i][j]-1
        index = (i+j)%3
        colors[index][c_ij] += 1

costs = [[0]*c for _ in range(3)]
for i in range(3):
    for c_cur in range(c):
        for c_tar in range(c):
            costs[i][c_tar] += D[c_cur][c_tar]*colors[i][c_cur]

ans = 1000*500**2
for c0, c1, c2 in permutations(list(range(c)), 3):
    diff = costs[0][c0] + costs[1][c1] + costs[2][c2]
    ans = min(ans, diff)
print(ans)
# Original solution
N,C = list(map(int,input().split()))
D = []
for i in range(C):
    d = list(map(int,input().split()))
    D.append(d)
A = []
for i in range(N):
    a = list(map(int,input().split()))
    A.append(a)

X0,X1,X2 = [],[],[]
for i in range(N):
    for j in range(N):
        if (i+j)%3 == 0:
            X0.append(A[i][j])
        elif (i+j)%3 == 1:
            X1.append(A[i][j])
        else:
            X2.append(A[i][j])

# それぞれの集合を塗り替える場合のコストを計算
Xn = [X0,X1,X2]
cost = [[0 for _ in range(C)] for _ in range(3)]
for i in range(3):
    for c in range(1,C+1):
        Ans = 0
        for x in Xn[i]:
            if x != c:
                Ans += D[x-1][c-1]
        cost[i][c-1] = Ans

Y0 = cost[0]
Y1 = cost[1]
Y2 = cost[2]
Ans = 1000000000000000
for i in range(C):
    for j in range(C):
        for k in range(C):
            if i != j and j != k and k != i:
                Ans = min(Ans, Y0[i]+Y1[j]+Y2[k])

print(Ans)


# Optimized solution
# Python3 (3.4.3)
import sys
input = sys.stdin.readline

# -------------------------------------------------------------
# function
# -------------------------------------------------------------


# -------------------------------------------------------------
# main
# -------------------------------------------------------------
# 入力の C は M とする
N,M = list(map(int,input().split()))
D = [list(map(int,input().split())) for _ in range(M)]
C = [list(map(int,input().split())) for _ in range(N)]

# X[x][y] : (i+j)%3 == x の, 初期状態での色が y(0-indexed) のマスの数
X = [[0 for _ in range(M)] for _ in range(3)]
for i in range(N):
    for j in range(N):
        x = (i+1 + j+1)%3
        y = C[i][j] - 1
        X[x][y] += 1

# Y[x][y] : (i+j)%3 == x の領域を y で塗る場合に必要なコスト
Y = [[0 for _ in range(M)] for _ in range(3)]
for i in range(3):
    for c_to in range(M):
        total = 0
        for c_from in range(M):
            total += X[i][c_from] * D[c_from][c_to]
        Y[i][c_to] = total

ans = 10**10
# 塗る色の組み合わせで全探索
for i in range(M):
    for j in range(M):
        for k in range(M):
            # 全ての色が異なる場合のみ考える
            if i != j and j != k and k != i:
                ans = min(ans, Y[0][i] +Y[1][j] + Y[2][k] )

print(ans)
# Original solution
n,c = list(map(int, input().split()))
color, grid = [], []
for _ in range(c):
    color.append(list(map(int, input().split())))
grid = {i:{} for i in range(3)}

for i in range(n):
    for j,t in enumerate(map(int, input().split())):
        t -= 1
        if t not in grid[(i+j + 2) % 3]:
            grid[(i+j + 2) % 3][t] = 0
        grid[(i+j + 2) % 3][t] += 1

costs = [[0 for _ in range(c)],[0 for _ in range(c)],[0 for _ in range(c)]]
for cc in range(c):
    for i in range(3):
        for d,cnt in list(grid[i].items()):
            costs[i][cc] += color[d][cc] * cnt

x = sorted(list(range(c)), key=lambda x: costs[0][x])[:3]
y = sorted(list(range(c)), key=lambda x: costs[1][x])[:3]
z = sorted(list(range(c)), key=lambda x: costs[2][x])[:3]
mn = 10**9
for i in x:
    for j in y:
        for k in z:
            if i == j or j == k or k == i:
                continue
            if mn > costs[0][i] + costs[1][j] + costs[2][k]:
                mn = costs[0][i] + costs[1][j] + costs[2][k]
print(mn)

# Optimized solution
import sys
input = sys.stdin.readline

n,c = list(map(int, input().split()))
color = []
for _ in range(c):
    color.append(list(map(int, input().split())))

grid = {i:{} for i in range(3)}
for i in range(n):
    for j,t in enumerate(map(int, input().split())):
        t -= 1
        if t not in grid[(i+j + 2) % 3]:
            grid[(i+j + 2) % 3][t] = 0
        grid[(i+j + 2) % 3][t] += 1

costs = [[0 for _ in range(c)],[0 for _ in range(c)],[0 for _ in range(c)]]
for cc in range(c):
    for i in range(3):
        for d,cnt in list(grid[i].items()):
            costs[i][cc] += color[d][cc] * cnt

rc = list(range(c))
x = sorted(list(rc), key=lambda x: costs[0][x])[:3]
y = sorted(list(rc), key=lambda x: costs[1][x])[:3]
z = sorted(list(rc), key=lambda x: costs[2][x])[:3]

mn = 10**9
for i in x:
    for j in y:
        for k in z:
            if i == j or j == k or k == i:
                continue
            if mn > costs[0][i] + costs[1][j] + costs[2][k]:
                mn = costs[0][i] + costs[1][j] + costs[2][k]
print(mn)