Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p02675 (python)

Fixed submission:
n = int(input())
if n % 10 == 3:
  print('bon')
elif n % 10 == 0 or n % 10 == 1 or n % 10 == 6 or n % 10 == 8:
  print('pon')
else:
  print('hon')

Buggy submission (verdict: Runtime Error):
import sys
n = int(raw_input())
if n % 10 == 3:
  print('bon')
elif n % 10 == 0 or n % 10 ==10 or n % 10 == 6 or n % 10 == 8:
  print('pon')
else:
  print('hon')
Problem p02675 (python)

Fixed submission:
n = str(input())
m = n[-1]
if m == '2' or m == '4' or m == '5' or m == '7' or m == '9':
    print('hon')
elif m == '0' or m == '1' or m == '6' or m == '8':
    print('pon')
else:
    print('bon')

Buggy submission (verdict: Runtime Error):
n = str(input())
m = n[2]
if m == '2' or m == '4' or m == '5' or m == '7' or m == '9':
    print('hon')
elif m == '0' or m == '1' or m == '6' or m == '8':
    print('pon')
else:
    print('bon')
Problem p02698 (python)

Fixed submission:
#F
import bisect
import sys

sys.setrecursionlimit(202020)

inf = float("inf")
N = int(input())
A = list(map(int,input().split()))

on = [[] for _ in range(N)]
for _ in range(N-1):
    u,v = map(int,input().split())
    on[u-1].append(v-1)
    on[v-1].append(u-1)
    
    

DP = [inf]*(N+1)
stack = []
ans = [-1]*N
seen = [False]*N

#DFS
def dfs(gragh, v): #v = start
    """
    global A
    global DP
    global ans
    global stack 
    global seen
    """
    
    seen[v] = True
    ind = bisect.bisect_left(DP,A[v])
    mae = DP[ind]
    DP[ind] = A[v]
    stack.append([ind,mae])
    
    ans[v] = bisect.bisect_left(DP,inf)
    
    for next_v in gragh[v]:
        if seen[next_v]: 
            continue
        
        dfs(gragh,next_v)
        
    ind,mae = stack.pop()
    DP[ind] = mae
    
    return



dfs(on,0)
for a in ans:
    print(a)

Buggy submission (verdict: Runtime Error):
#F
import bisect

inf = float("inf")
N = int(input())
A = list(map(int,input().split()))

on = [[] for _ in range(N)]
for _ in range(N-1):
    u,v = map(int,input().split())
    on[u-1].append(v-1)
    on[v-1].append(u-1)
    
    

DP = [inf]*(N+1)
stack = []
ans = [-1]*N
seen = [False]*N

#DFS
def dfs(gragh, v): #v = start
    """
    global A
    global DP
    global ans
    global stack 
    global seen
    """
    
    seen[v] = True
    ind = bisect.bisect_left(DP,A[v])
    mae = DP[ind]
    DP[ind] = A[v]
    stack.append([ind,mae])
    
    ans[v] = bisect.bisect_left(DP,inf)
    
    for next_v in gragh[v]:
        if seen[next_v]: 
            continue
        
        dfs(gragh,next_v)
        
    ind,mae = stack.pop()
    DP[ind] = mae
    
    return



dfs(on,0)
for a in ans:
    print(a)

Buggy submission (verdict: Runtime Error):
#F
from collections import deque
import bisect

inf = float("inf")
N = int(input())
A = list(map(int,input().split()))

on = [[] for _ in range(N)]
for _ in range(N-1):
    u,v = map(int,input().split())
    on[u-1].append(v-1)
    on[v-1].append(u-1)
    
    

DP = [inf]*N
stack = []
ans = [-1]*N
seen = [False]*N

#DFS
def dfs(gragh, v): #v = start
    global A
    global DP
    global ans
    global stack 
    global seen
    
    seen[v] = True
    ind = bisect.bisect_left(DP,A[v])
    mae = DP[ind]
    DP[ind] = A[v]
    stack.append([ind,mae])
    
    ans[v] = bisect.bisect_left(DP,inf)
    
    for next_v in gragh[v]:
        if seen[next_v]: 
            continue
        
        dfs(gragh,next_v)
        
    ind,mae = stack.pop()
    DP[ind] = mae
    
    return



dfs(on,0)
for a in ans:
    print(a)
Problem p02850 (python)

Fixed submission:
#D
from collections import deque
#import bisect
#import math
#import heapq
#import sys
#input = sys.stdin.readline

#inf = float("inf")
#mod = 10**9 + 7
#mod = 998244353

#list(map(int,input().split()))

N = int(input())
AB = []
for i in range(N-1):
    a,b = map(int,input().split())
    AB.append([a,b,i])

K = [0]*N
for ab in AB:
    a,b,i = ab
    K[a-1]+=1
    K[b-1]+=1
k = max(K)
print(k)
AB.sort()
C = [0]*N
D = deque(AB)
flag = 0
visited = [-1]*N
ans = [0]*(N-1)
while D:
    a,b,i = D.popleft()
    if visited[a-1] == -1 and visited[b-1] == -1:
        if flag == 0:
            flag = 1
            visited[a-1] = 0
            visited[b-1] = 0
            ans[i] = 1
            C[a-1] = 1
            C[b-1] = 1
        else:
            D.append([a,b,i])
    else:
        if visited[b-1] == -1:
            if C[a-1] == k:
                ans[i] = 1
                C[a-1] = 1
                C[b-1] = 1
            else:
                ans[i] = C[a-1]+1
                C[a-1]+=1
                C[b-1] = C[a-1]
            visited[b-1] = 0
        else:
            visited[a-1] = 0
            if C[b-1] == k:
                ans[i] = 1
                C[a-1] = 1
                C[b-1] = 1
            else:
                ans[i] = C[b-1]+1
                C[b-1]+=1
                C[a-1] = C[b-1]
                
for a in ans:
    print(a)

Buggy submission (verdict: Runtime Error):
#D
from collections import deque
#import bisect
#import math
#import heapq
#import sys
#input = sys.stdin.readline

#inf = float("inf")
#mod = 10**9 + 7
#mod = 998244353

#list(map(int,input().split()))

N = int(input())
AB = [list(map(int,input().split())) for i in range(N-1)]
K = [0]*N
for ab in AB:
    a,b = ab
    K[a-1]+=1
    K[b-1]+=1
k = max(K)
print(k)

C = [0]*N
D = deque(AB)
flag = 0
visited = [-1]*N
#print(D)
while D:
    a,b = D.popleft()
    if visited[a-1] == -1 and visited[b-1] == -1:
        if flag == 0:
            flag = 1
            visited[a-1] = 0
            visited[b-1] = 0
            print(1)
            C[a-1] = 1
            C[b-1] = 1
        else:
            D.append([a,b])
    else:
        if visited[b-1] == -1:
            if C[a-1] == k:
                print(1)
                C[a-1] = 1
                C[b-1] = 1
            else:
                print(C[a-1]+1)
                C[a-1]+=1
                C[b-1] = C[a-1]
            visited[b-1] = 0
        else:
            visite[a-1] = 0
            if C[b-1] == k:
                print(1)
                C[a-1] = 1
                C[b-1] = 1
            else:
                print(C[b-1]+1)
                C[b-1]+=1
                C[a-1] = C[b-1]
Problem p02960 (python)

Fixed submission:
#D問題
mod = 1000000007
S = str(input())
N = len(S)


dp = [[0 for i in range(13)] for j in range(N+1)]
dp[0][0] = 1
for i,s in enumerate(S):
    if s == "?":
        for j in range(10):
            for k in range(13):
                ind = (10*k+j)%13
                dp[i+1][ind] += dp[i][k]
    else:
        j = int(s)
        for k in range(13):
            ind = (10*k+j)%13
            dp[i+1][ind] += dp[i][k]

    for k in range(13):
        dp[i+1][k]%=mod
            
print(dp[N][5]%mod)

Buggy submission (verdict: Runtime Error):
#D問題
mod = 1000000007
S = str(input())


dp = [[0 for i in range(13)] for j in range(N+1)]
dp[0][0] = 1
for i,s in enumerate(S):
    if s == "?":
        for j in range(10):
            for k in range(13):
                ind = (10*k+j)%13
                dp[i+1][ind] += dp[i][k]
    else:
        j = int(s)
        for k in range(13):
            ind = (10*k+j)%13
            dp[i+1][ind] += dp[i][k]
            
    for k in range(13):
        dp[i+1][k]%=mod
            
print(dp[N][5]%mod)

Buggy submission (verdict: Runtime Error):
#D問題
mod = 1000000007
S = str(input())
N = len(S)

dp = [[0 for i in range(13)] for j in range(N+1)]
dp[0][0] = 1
for i in range(N):
    if S[i] == "?":
        for j in range(10):
            for k in range(13):
                ind = (10*k+j)%13
                dp[i+1][ind] += dp[i][k]
    else:
        s = int(S[i])
        for k in range(13):
            ind = (10*k+j)%13
            dp[i+1][ind] += dp[i][k]
            
print(dp[N][5]%mod)

Buggy submission (verdict: Runtime Error):
#D問題
mod = 1000000007
S = str(input())
N = len(S)

dp = [[0 for i in range(13)] for j in range(N+1)]
dp[0][0] = 1
for i in range(N):
    if S[i] == "?":
        for j in range(10):
            for k in range(13):
                mod13 = (10*k+j)%13
                dp[i+1][mod13] += dp[i][k]
    else:
        s = int(S[i])
        for k in range(13):
            mod13 = (10*k+j)%13
            dp[i+1][mod13] += dp[i][k]
            
print(dp[N][5]%mod)

Buggy submission (verdict: Runtime Error):
#D問題
mod = 1000000007
S = str(input())
N = len(S)

dp = [[0 for i in range(13)] for j in range(N+1)]
dp[0][0] = 1
for i in range(N):
    if S[i] == "?":
        for j in range(10):
            for k in range(13):
                mod13 = (10*k+j)%13
                dp[i+1][mod13] += dp[i][k]
    else:
        s = int(S[i])
        for k in range(13):
            mod13 = (10*k+j)%13
            dp[i+1][mod13] += dp[i][k]
            
print(dp[N][5]%mod)
Problem p03000 (python)

Fixed submission:
#B問題
N,X = map(int,input().split())
L = list(map(int,input().split()))
land = [0]
for i in range(N):
    land.append(land[-1]+L[i])
land.sort()
count = 0
for l in land:
    if l <= X:
        count+=1
        
print(count)

Buggy submission (verdict: Runtime Error):
#B問題
N,X = map(int,input().split())
L = list(map(int,input().split()))
land = [0]
for i in range(N):
    land.append(land[-1]+L[i])
land.sort()
count = 0
for l in land:
    if land <= X:
        count+=1
        
print(count)
Problem p03006 (python)

Fixed submission:
#B問題
N = int(input())
XY = [list(map(int,input().split())) for i in range(N)]
edges = []
for i in range(N):
    x1,y1 = XY[i][0],XY[i][1]
    for j in range(N-i-1):
        J = i+j+1
        x2,y2 = XY[J][0],XY[J][1]
        dx = x2-x1
        dy = y2-y1
        if dx <= 0:
            if dx == 0:
                if dy < 0:
                    dy = -dy
            else:
                dx = -dx
                dy = -dy
        edges.append([dx,dy])

listcount = []
count = []
if N == 1:
    listcount.append([0,0])
    count.append(1)

for edge in edges:
    if edge == [0,0]:
        if [0,0] not in listcount:
            listcount.append([0,0])
            count.append(1)
    elif edge in listcount:
        count[listcount.index(edge)]+=1
    else:
        listcount.append(edge)
        count.append(1)
        
most = max(count)
if N == 1:
    print(1)
else:
    print(N-most)

Buggy submission (verdict: Runtime Error):
#B問題
N = int(input())
XY = [list(map(int,input().split())) for i in range(N)]
edges = []
for i in range(N):
    x1,y1 = XY[i][0],XY[i][1]
    for j in range(N-i-1):
        J = i+j+1
        x2,y2 = XY[J][0],XY[J][1]
        dx = x2-x1
        dy = y2-y1
        if dx < 0:
            dx = -dx
            dy = -dy
        edges.append([dx,dy])
        
listcount = []
count = []
for edge in edges:
    if edge == [0,0]:
        pass
    elif edge in listcount:
        count[listcount.index(edge)]+=1
    else:
        listcount.append(edge)
        count.append(1)
        
most = max(count)
print(N-most)
Problem p03031 (python)

Fixed submission:
#C問題
N,M = map(int,input().split())
K = []
S = []
for i in range(M):
    ks =list(map(int,input().split()))
    ss = []
    kk = ks[0]
    K.append(kk)
    for j in range(kk):
        ss.append(ks[j+1])
    
    S.append(ss)
P = list(map(int,input().split()))
Bit = 2**N
ans = 0
for i in range(Bit):
    b = bin(i)
    b = b.lstrip("0b")
    lenb = len(b)
    j = 0
    for j in range(N-lenb):
        b = "0"+b
        
    sw = []
    j = 0

    for j in range(N):
        if b[j] == "1":
            sw.append(1)
        else:
            sw.append(0)
    
    j = 0
    flag = 0
    for j in range(M):
        cond = P[j]
        now = 0
        for k in range(K[j]):
            now+=sw[S[j][k]-1]
            
        if now%2 == cond:
            pass
        else:
            flag = 1
            break
            
    if flag == 0:
        ans+=1
    
print(ans)

Buggy submission (verdict: Runtime Error):
#C問題
N,M = map(int,input().split())
K = []
S = []
for i in range(M):
    ks =list(map(int,input().split()))
    ss = []
    kk = ks[0]
    K.append(kk)
    for j in range(kk):
        ss.append(ks[j+1])
    
    S.append(ss)
P = list(map(int,input().split()))
Bit = 2**N
ans = 0
for i in range(Bit):
    b = bin(i)
    b = b.lstrip("0b")
    lenb = len(b)
    j = 0
    for j in range(N-lenb):
        b = "0"+b
        
    sw = []
    j = 0

    for j in range(M):
        if b[j] == "1":
            sw.append(1)
        else:
            sw.append(0)
    
    j = 0
    flag = 0
    for j in range(M):
        cond = P[j]
        now = 0
        for k in range(K[j]):
            now+=sw[S[j][k]-1]
            
        if now%2 == cond:
            pass
        else:
            flag = 1
            break
            
    if flag == 0:
        ans+=1
    
print(ans)
Problem p03059 (python)

Fixed submission:
#A問題
A,B,T=map(int,input().split())
print(B*int(T/A))

Buggy submission (verdict: Runtime Error):
#C問題
N=int(input())
A=list(map(int,input().split()))
def gcd(GCD1,GCD2):
    x = min(GCD1,GCD2)
    y = max(GCD1,GCD2)
    if y%x == 0:
        return x
    else:
        return gcd(x,y%x)
def gcdlist(xlist):
    xlen=len(xlist)
    GCD=0
    for i in range(xlen-1):
        GCD=gcd(xlist[i],xlist[i+1])
        xlist[i+1]=GCD
    return GCD
MAX=0
kari=0
for i in range(N):
    B=A[::]
    del B[i]
    kari=gcdlist(B)
    MAX=max(MAX,kari)
if N == 2:
    MA=max(A)
    g=gcd(A[0],A[1])
    MAX=max(MA,g)
print(MAX)
Problem p03212 (python)

Fixed submission:
N = int(input())
def shichi(n):
    if int(n) > N:
        return 0
    ret=1 if all(n.count(c)>0 for c in "753") else 0
    for c in "753":
        ret += shichi(n+c)
    return ret
print(shichi("0"))

Buggy submission (verdict: Runtime Error):
N = int(input())
def dfs(s): # 文字列 s で始まる七五三数の個数
if int(s) > N:
  return 0
  ret = 1 if all(s.count(c) > 0 for c in '753') else 0 # s 自体が七五三数なら +1
  for c in '753':
    ret += dfs(s + c)
    return ret
print(dfs('0'))

Buggy submission (verdict: Runtime Error):
 N = int(input())

 def dfs(s): # 文字列 s で始まる七五三数の個数
 if int(s) > N:
 return 0
 ret = 1 if all(s.count(c) > 0 for c in '753') else 0 # s 自体が七五三数なら +1
 for c in '753':
 ret += dfs(s + c)
 return ret

 print(dfs('0'))
Problem p03219 (python)

Fixed submission:
X,Y = map(int,input().split(" "))
print(int(X+(1/2)*Y))

Buggy submission (verdict: Runtime Error):
X,Y = int(input())
print(int(X+(1/2)*Y))
Problem p03220 (python)

Fixed submission:
N = int(input())
T,A = map(int,input().split(" "))
H = list(map(int,input().split(" ")))
j = 0
k = 0
flag = 0
s = []
while j < N:
  k = T - (0.006)*H[j]
  if k >= A:
    s.append(k - A)
  else:
    s.append(A - k)
  if s[j] <= s[flag]:
    flag = j
  j = j + 1
print(flag+1)

Buggy submission (verdict: Runtime Error):
N = int(input())
T,A = map(int,input().split(" "))
temp = []
for i in range(N):
  temp.append(map(int,input().split(" ")))
j = 0
k = 0
flag = 0
s = []
while j < N:
  k = T - (0.006)*temp[j]
  if k >= A:
    s.append(k - A)
  else:
    s.append(A - k)
  if s[j] <= s[flag]:
    flag = j
  j = j + 1
print(flag)

Buggy submission (verdict: Runtime Error):
N = int(input())
T,A = map(int,input().split(" "))
temp = []
for i in range(N):
  temp.append(map(int,input().split(" ")))
j = 0
k = 0
flag = 0
s = []
while j < N:
  k = T - (0.006)*temp[j]
    if k >= A:
      s.append(k - A)
    else:
      s.append(A - k)
  if s[j] <= s[flag]:
    flag = j
  j = j + 1
print(j)
Problem p03261 (python)

Fixed submission:
N = int(input())
shiri = []
flag = 0
for i in range(N):
    tugi = str(input())
    tlist = list(tugi)
    if i!=0:
        if tugi in shiri:
            flag = 1
            break
        else:
            mae = list(shiri[i-1])
            if tlist[0]!=mae[-1]:
                flag = 1
                break
            else:
                shiri.append(tugi)
    else:
        shiri.append(tugi)
if flag == 0:
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
N = int(input())
shiri = []
flag = 0
for i in range(N):
    tugi = str(input())
    tlist = list(tugi)
    if i!=0:
        if tugi in shiri:
            flag = 1
            break
        else:
            mae = list(shiri[i-1])
            if tlist[0]!=mae[-1]:
                flag = 1
                break
            else:
                shiri,append(tugi)
    else:
        shiri.append(tugi)
if flag == 0:
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
N = int(input())
shiri = []
for i in range(N):
    tugi = str(input())
    tlist = list(tugi)
    if i!=0:
        if tugi in shiri:
            break
        else:
            mae = list(shiri[i-1])
            if tlist[0]!=mae[-1]:
                break
            else:
                shiri,append(tugi)
    else:
        shiri.append(tugi)
Problem p03263 (python)

Fixed submission:
H,W=map(int,input().split())
A=[list(map(int,input().split())) for i in range(H)]
ans=[]
for i in range(H):
  for j in range(W):
    if i%2 == 0:
      if j != W-1:
        if A[i][j]%2 == 1:
          A[i][j+1]+=1
          ans.append([i+1,j+1,i+1,j+2])
      else:
        if A[i][j]%2 == 1 and i < H-1:
          A[i+1][j]+=1
          ans.append([i+1,j+1,i+2,j+1])
    else:
      if j != W-1:
        if A[i][W-1-j]%2 == 1:
          A[i][W-2-j]+=1
          ans.append([i+1,W-j,i+1,W-j-1])
      else:
        if A[i][W-1-j]%2 == 1 and i < H-1:
          A[i+1][W-1-j]+=1
          ans.append([i+1,W-j,i+2,W-j])
print(len(ans))
for i in range(len(ans)):
  print(ans[i][0],ans[i][1],ans[i][2],ans[i][3])

Buggy submission (verdict: Runtime Error):
H,W=map(int,input().split())
A=[list(map(int,input().split())) for i in range(H)]
ans=[]
for i in range(H):
  for j in range(W):
    if i%2 == 0:
      if j != W-1:
        if A[i][j]%2 == 1:
          A[i][j+1]+=1
          ans.append([i+1,j+1,i+1,j+2])
      else:
        if A[i][j]%2 == 1 and i < H-1:
          A[i+1][j]+=1
          ans.append([i+1,j+1,i+2,j+1])
    else:
      if j != W-1:
        if A[i][W-1-j]%2 == 1:
          A[i][W-j]+=1
          ans.append([i+1,W-j,i+1,W-j+1])
      else:
        if A[i][W-1-j]%2 == 1 and i < H-1:
          A[i+1][W-1-j]+=1
          ans.append([i+1,W-j,i+2,W-j+1])
print(len(ans))
for i in range(len(ans)):
  print(ans[i][0],ans[i][1],ans[i][2],ans[i][3])
Problem p03284 (python)

Fixed submission:
N,K = map(int,input().split(" "))
if N%K == 0:
  print(0)
else:
  print(1)

Buggy submission (verdict: Runtime Error):
N,K = (int,input().split(" "))
if N%K == 0:
  print(0)
else:
  print(1)
Problem p03288 (python)

Fixed submission:
R = int(input())
if R < 1200:
  print("ABC")
elif R < 2800:
  print("ARC")
else:
  print("AGC")

Buggy submission (verdict: Runtime Error):
R = int(input())
if R < 1200:
  print(ABC)
elif R < 2800:
  print(ARC)
else:
  print(AGC)
Problem p03318 (python)

Fixed submission:
#D問題
K=int(input())
keta=1
now=1
count=0

def sunuke(n):
    a=0
    for i in str(n):
        a+=int(i)
    return(n/a)

while count < K:
    if sunuke(now) <= sunuke(now+keta):
        print(now)
        now+=keta
        count+=1
    else:
        now+=9*keta
        keta*=10

Buggy submission (verdict: Runtime Error):
#D問題
K = int(input())
keta = 1
now = 1
def sunuke(n):
    a = 0
    for i in str(n):
        a+=int(i)
    return(n/a)
count=0
while count < k:
    if sunuke(now) <= sunuke(now+keta):
        print(now)
        now+=keta
        count+=1
    else:
        now+=9*keta
        keta *= 10

Buggy submission (verdict: Runtime Error):
#D問題
K = int(input())
keta = 1
now = 1
def sunuke(n):
    a = 0
    for i in str(n):
        a+=int(i)
    return(n/a)
count=0
while count < k:
    if sunuke(ima) <= sunuke(ima+keta):
        print(ima)
        ima += keta
        count += 1
    else:
        ima += 9*keta
        keta *= 10

Buggy submission (verdict: Runtime Error):
K = int(input())
sunuke = [1,2,3,4,5,6,7,8,9,19,29,39,49,59,69,79,89,99,199,299,399,499,599,699,799,899,999,1999,2999,3999,4999,5999,6999,7999,8999,9999,19999,29999,39999,49999,59999,69999,79999,89999,99999,199999,299999,399999,499999,599999,699999,799999,899999,999999]
i = 0
while i < K:
  print(sunuke[i])
  i+=1

Buggy submission (verdict: Runtime Error):
K = int(input())
sunuke = [1,2,3,4,5,6,7,8,9,19,29,39,49,59,69,79,89,99,199,299,399,499,599,699,799,899,999]
i = 0
while i < K:
  print(sunuke[i])
  i+=1
Problem p03330 (python)

Fixed submission:
#D問題
N,C = 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)]
for i in range(N):
    for j in range(N):
        c[i][j]-=1
    

mod3 = [[0 for i in range(C)] for j in range(3)]
for i in range(N):
    for j in range(N):
        mod3[(i+j)%3][c[i][j]]+=1
        
c3 = [[0 for i in range(C)] for j in range(3)]
for i in range(3):
    for j in range(C):
        kari = 0
        for k in range(C):#kからjへ
            kari+=mod3[i][k]*D[k][j]
        c3[i][j] = kari
        
inf = float("inf")
MIN = inf

for i in range(C):
    C1 = c3[0][i]
    for j in range(C):
        if j != i:
            C2 = c3[1][j]
            for k in range(C):
                if k != i and k != j:
                    C3 = c3[2][k]
                    MIN = min(MIN,C1+C2+C3)
                    #print(C1,C2,C3)
                
if MIN != inf:
    print(MIN)
else:
    print(0)

Buggy submission (verdict: Runtime Error):
#D問題
N,C = 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)]
for i in range(N):
    for j in range(N):
        c[i][j]-=1
    

mod3 = [[0 for i in range(C)] for j in range(3)]
for i in range(N):
    for j in range(N):
        mod3[(i+j)%3][c[i][j]]+=1
        
c3 = [[0 for i in range(3)] for j in range(3)]
for i in range(3):
    for j in range(C):
        kari = 0
        for k in range(C):#kからjへ
            kari+=mod3[i][k]*D[k][j]
        c3[i][j] = kari
        
inf = float("inf")
MIN = inf

for i in range(C):
    C1 = c3[0][i]
    for j in range(C):
        if j != i:
            C2 = c3[1][j]
            for k in range(C):
                if k != i and k != j:
                    C3 = c3[2][k]
                    MIN = min(MIN,C1+C2+C3)
                    #print(C1,C2,C3)
                
if MIN != inf:
    print(MIN)
else:
    print(0)

Buggy submission (verdict: Runtime Error):
#D問題
N,C = 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)]
for i in range(N):
    for j in range(N):
        c[i][j]-=1
    

mod3 = [[0 for i in range(C)] for j in range(3)]
for i in range(N):
    for j in range(N):
        mod3[(i+j)%3][c[i][j]]+=1
        
c3 = [[0 for i in range(3)] for j in range(3)]
for i in range(3):
    for j in range(C):
        kari = 0
        for k in range(C):#kからjへ
            kari+=mod3[i][k]*D[k][j]
        c3[i][j] = kari
        
inf = float("inf")
MIN = inf
for i in range(C):
    C1 = c3[0][i]
    for j in range(C):
        if j != i:
            C2 = c3[1][j]
            for k in range(C):
                if k != i and k != j:
                    C3 = c3[2][k]
                    MIN = min(MIN,C1+C2+C3)
                    #print(C1,C2,C3)

print(MIN)