Harryxun/stf_run huggingface dataset:
import numpy
n, m = map(int, input().split())
a = numpy.array([0] + list(map(int, input().split())), dtype=numpy.float64)

acs = numpy.cumsum(a)
cnt = 0

for l in range(n):
    for r in range(l, n):
        if (acs[r] - acs[l - 1]) % m == 0:
            cnt += 1
print(cnt)

### FIXED CODE ###

n, m = map(int, input().split())
a = list(map(int, input().split()))
table = {0: 0}
buf = 0
cnt = 0
for i in range(n):
    buf = (buf + a[i]) % m
    if buf in table:
        cnt += table[buf]
        table[buf] += 1
    else:
        table[buf] = 1
print(cnt + table[0])
from functools import reduce
from math import gcd
n=int(input())
t=[int(input()) for i in range(n)]
lcm=t[0]*t[1]//gcd(t[0],t[1])
for i in range(2,n):
  lcm=lcm*t[i]//gcd(lcm,t[i])
print(lcm)

### FIXED CODE ###

from fractions import gcd
def lcm(a,b): return a*b//gcd(a,b)
N=int(input())
ans=1
for i in range(N):
    t=int(input())
    ans=lcm(ans,t)
print(ans)
y= list(map(str,input().rstrip().split()))
print(y[1]+y[0])

### FIXED CODE ###

x = list(map(str,input().rstrip().split()))
print(x[1]+x[0])
N = int(input())
T = [int(input()) for i in range(N)]

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

def lcm(a, b):
    return a * b / gcd(a, b)

now = lcm(T[0], T[1])
for i in range(1, N):
    now = lcm(now, T[i])

print(now)


### FIXED CODE ###

N = int(input())
T = [int(input()) for i in range(N)]


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):
    return a * b // gcd(a, b)


if N == 1:
    print(T[0])
    exit()

now = lcm(T[0], T[1])
for i in range(1, N):
    now = lcm(now, T[i])

print(now)
from collections import Counter


def main():
    N = int(input())
    lst_a = list(map(int, input().split()))

    cnt_a = Counter(lst_a)

    if len(cnt_a) > 3:
        print("No")
        exit()
    
    if len(cnt_a) == 1:
        print("Yes" if lst_a[0] == 0 else "No")
        exit()
    
    if N % 3:
        print("No")
        exit()
    
    if len(cnt_a) == 2:
        if cnt_a[0] != N//3:
            print("No")
        else:
            print("Yes")
    else:
        if any(v != N//3 for v in C.values()):
            print("No")
        else:
            x, y, z = list(cnt_a)
            print("Yes" if a ^ b ^ c == 0 else "No")


if __name__ == "__main__":
    main()

### FIXED CODE ###

from collections import Counter


def main():
    N = int(input())
    lst_a = list(map(int, input().split()))

    cnt_a = Counter(lst_a)

    if len(cnt_a) > 3:
        print("No")
        exit()
    
    if len(cnt_a) == 1:
        print("Yes" if lst_a[0] == 0 else "No")
        exit()
    
    if N % 3:
        print("No")
        exit()
    
    if len(cnt_a) == 2:
        if cnt_a[0] != N//3:
            print("No")
        else:
            print("Yes")
    else:
        if any(v != N//3 for v in cnt_a.values()):
            print("No")
        else:
            x, y, z = list(cnt_a)
            print("Yes" if x ^ y ^ z == 0 else "No")


if __name__ == "__main__":
    main()
def main():
  X = int(input())
  
  a = -1
  b1 = -1
  b2 = +1
  while True:
    a += 1
    while True:
    	b1 += 1
        b2 -= 1
        if (X/(a-b1)).is_integer():
          if int(X/(a-b1)) == a**4 + b1*(a**3) + (a**2)*(b1**2) + a*(b1**2) + b1**4:
            print("{} {}\n".format(a, b1))
            return 0
          
        if (X/(a-b2)).is_integer():
          if int(X/(a-b2)) == a**4 + b2*(a**3) + (a**2)*(b2**2) + a*(b2**2) + b2**4:
            print("{} {}\n".format(a, b2))
            return 0
          
          
if __name__ == "__main__":
  main()
         
          

### FIXED CODE ###

def main():
    X = int(input())

    a = -1
    
    while True:
        b1 = 0
        b2 = 0
        while b1 < a:
            
            if (X/(a-b1)).is_integer():
                if int(X/(a-b1)) == a**4 + b1*(a**3) + (a**2)*(b1**2) + a*(b1**3) + b1**4:
                    print("{} {}\n".format(a, b1))
                    return 0

            if (X/(a-b2)).is_integer():
                if int(X/(a-b2)) == a**4 + b2*(a**3) + (a**2)*(b2**2) + a*(b2**3) + b2**4:
                    print("{} {}\n".format(a, b2))
                    return 0

            b1 += 1
            b2 -= 1
            
        a += 1


if __name__ == "__main__":
    main()
def solve(string):
    return str(sum(i for i in range(n + 1) if i % 3 > 1 and i % 5 > 1))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))


### FIXED CODE ###

def solve(string):
    return str(sum(i for i in range(int(string) + 1) if i % 3 > 0 and i % 5 > 0))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))
def popcnt(n):
    return bin(n).count("1")

def f(n):
    k = n
    cnt = 0
    while k>0:
        k = k%popcnt(k)
        cnt+= 1
    return cnt

def solve(X):
    for i in reversed(range(N)):
        if [N-1-i] == 0:
            print(0)
        else:
            K = X ^ (1 << i)
            K2 = K%P[N-1-i]
            print(f(K2) + 1)




N = int(input())
X = input()
X = X.zfill(N)
count_1 = X.count("1")

P = [0]*N
for i in range(N):
    if X[i] == "1":
        P[i] = count_1 - 1
    else:
        P[i] = count_1 + 1
Y = int(X,2)

solve(Y)

### FIXED CODE ###

def popcnt(n):
    return bin(n).count("1")

def f(n):
    cnt = 0
    while n>0:
        n = n%popcnt(n)
        cnt += 1
    return cnt

def solve(X):
    Y = int(X, 2)
    c = X.count("1")
    m, p = c-1, c+1
    Ym, Yp = Y%m if m else -1, Y%p
    for i in range(N):
        if X[i] == "1":
            Y = (Ym - pow(2,N-i-1,m))%m if Ym!=-1 else -1
        else:
            Y = (Yp + pow(2,N-i-1,p))%p
        print(f(Y) + 1 if Y!=-1 else 0)

N = int(input())
solve(input())
import numpy as np  

n, w = map(int, input().split())
dp = np.full(n*1000+1, float('inf'))
dp[0] = 0

for _ in range(n):
    weight, value = map(int, input().split())
    dp[:value] = np.minimum(dp[:value], dp[:-value] + weight) 

print(dp[-1])



### FIXED CODE ###

n, w = map(int, input().split())

import numpy as np 

dp = np.full(1000*n+1, w+1, dtype=int)

dp[0] = 0

for i in range(n):
    weight, value = map(int, input().split())
    dp[value:] = np.minimum(dp[value:], dp[:-value] + weight)

print(np.max(np.where(dp <= w)))
n, m = map(int, input().split())
a = [[0] * m for _ in range(n)]
b = [0] * m
for i in range(n):
    a[i] = list(map(int, input().split()))
for i in range(m):
    b[i] = int(input())
for i in range(n):
    c = None
    for j in range(m):
        c += a[i][j] * b[j]
    print(c)



### FIXED CODE ###

n, m = map(int, input().split())
A = [map(int, input().split()) for _ in [None] * n]
b = [int(input()) for _ in [None] * m]
for a in A:
    print(sum(i * j for i, j in zip(a, b)))
A,B,C,D = map( int,input().split() )
while A > 0 and c > 0:
    A -= D 
    if A <= 0 :
        print( 'No' )
        exit()
    C -= B
    if C <= 0 :
        print( 'Yes' )
        exit()


### FIXED CODE ###

A,B,C,D = map(int, input().split())

while A > 0 and C > 0:
    C -= B
    if C <= 0:
        print("Yes")
        exit()
    A -= D
    if A <= 0:
        print("No")
        exit()
a = input().split()

if a[0]>=10 or a[1]>=10:
  return -1
elif:
  return a[0]*a[1]

### FIXED CODE ###

a = list(map(int, input().split()))

if a[0]>=10 or a[1]>=10:
    print(-1)
else:
    print(a[0]*a[1])
n, k = map(int, input().split())

if n % K == 0:
    print(0)
else:
    print(1)

### FIXED CODE ###

n, k = map(int, input().split())

if n % k == 0:
    print(0)
else:
    print(1)
r = map(int, input().split())
b = 3*r*r
print(b)

### FIXED CODE ###

r = int(input())
b = 3*r**2
print(b)
import sys
import os
import math
import bisect
import collections
import itertools
import heapq
import re
import queue
import fractions from gcd

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

# lcm = lambda x, y: x * y / math.gcd(x, y)
lcm = lambda x, y: x * y / gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    A, B, C, D = il()
    E = B - A + 1
    cnt = 0

    cnt += B // C
    cnt += B // D
    cnt -= A // C
    cnt -= A // D

    cnt -= B // lcm(C, D)
    cnt += A // lcm(C, D)
    print(E - int(cnt))


if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys
import os
import fractions

MOD = 10 ** 9 + 7


def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    A, B, C, D = list(map(int, sys.stdin.readline().split()))
    E = B - A + 1

    ret = 0
    ret += B // C
    ret += B // D
    ret -= (A - 1) // C
    ret -= (A - 1) // D
    ret -= B // lcm(C, D)
    ret += (A-1) // lcm(C, D)
    print(E - ret)


if __name__ == '__main__':
    main()
from itertools import product


n, m = [int(i) for i in input().rstrip().split()]
AA = [[0 if i == '.' else 1 for i in input().rstrip()] for _ in range(n)]
BB = [[0 if i == '.' else 1 for i in input().rstrip()] for _ in range(m)]

for i_r, i_c in product(range(n - m + 1), range(n - m + 1)):
    i_r -= 1
    i_c -= 1
    for k_r, k_c in product(range(m), range(m)):
        if AA[i_r + k_r][i_c + k_c] == BB[k_r][k_c]:
            continue
        else:
            break
    else:
        print('Yes')
        break
else:
    print('No')

### FIXED CODE ###

table = {'2': 0,
         '3': 1,
         '4': 2,
         '5': 3,
         '6': 4,
         '7': 5,
         '8': 6,
         '9': 7,
         '10': 8,
         '11': 9,
         '12': 10,
         '13': 11,
         '1': 12, }
ary = [table[i] for i in input().strip().split()]
if (ary[1] - ary[0]) > 0:
    print('Bob')
elif ary[1] == ary[0]:
    print('Draw')
else:
    print('Alice')
a,b=int(input().replace(" ",""))
flg=False
i=0
while i**2<=ab:
    if i**2==ab:
        flg=True
        break
    i+=1

print("Yes" if flg else "No")

### FIXED CODE ###

ab=int(input().replace(" ",""))
flg=False
i=0
while i**2<=ab:
    if i**2==ab:
        flg=True
        break
    i+=1

print("Yes" if flg else "No")
a = list(map(int,input().split()))

cnt = 1
for i in range(a):
  if i == 0:
    print(cnt)
    cnt += 1


### FIXED CODE ###

a,b,c,d,e = map(int,input().split())

if a == 0 :
  print(1)
elif b == 0 : 
  print(2)
elif c == 0 :
  print(3)
elif d == 0 : 
  print(4)
else :
  print(5)
A, B = map(int, input().split()
           
import math

n = (B-A) / (A-1) + 1
print(math.ceil(n))

### FIXED CODE ###

A, B = map(int, input().split())

cnt = 0
tap = 1

while tap < B:
    tap += A-1
    cnt += 1
    
print(cnt)
n=int(input())
a = [int(i) for i in input().split()]
mod = 10**9+7
ans = 3

red=1
gre=0
blu=0
for i in range(1,n):
    temp=0
    if a[i] == red:
        temp+=1
    if a[i] == gre:
        temp+=1        
    if a[i] == blu:
        temp+=1
    ans*=temp
    ans=ans%mod
    if a[i]==red:
        red+=1
        continue
    if a[i]==gre:
        gre+=1
        continue
    if a[i]==blu:
        blu+=1
        continue
    print(asdf)
    
print(ans)

### FIXED CODE ###

n=int(input())
a = [int(i) for i in input().split()]
mod = 10**9+7
ans = 3

red=1
gre=0
blu=0
for i in range(1,n):
    temp=0
    if a[i] == red:
        temp+=1
    if a[i] == gre:
        temp+=1        
    if a[i] == blu:
        temp+=1
    if temp!=0:
        ans*=temp
    
        ans=ans%mod
    if a[i]==red:
        red+=1
        continue
    if a[i]==gre:
        gre+=1
        continue
    if a[i]==blu:
        blu+=1
        continue
    else:
      ans=0
    
if a[0]!=0:
  ans=0
    
print(ans)
cond = [int(n) for n in input().split(" ")]

while True:
    case = []
    cards = [n for n in range(1,cond[0] + 1)]
    for c in range(cond[1]):
        case = [int(n) for n in input().split(" ")]
        disp = cards[case[0]:case[0]+case[1]]
        cards[case[0]:case[0]+case[1]] = cards[case[0]-case[1]:case[0]]
        cards[case[0]-case[1]:case[0]] = disp
    else:
        print(cards[0])
        cond = [int(n) for n in input().split(" ")]
        if cond[0] == 0 and cond[1] == 0:
            break
        else:
            continue

### FIXED CODE ###

cards,trial = (int(n) for n in input().split(" "))
while True:
    cards = list(reversed([int(n) for n in range(1,(cards)+1)]))
    for n in range(trial):
        num,change = (int(n) for n in input().split(" "))
        num -= 1
        disp = cards[num:num+change]
        cards[change:change+num] = cards[:num]
        cards[:change] = disp
    print(cards[0])
    cards,trial = (int(n) for n in input().split(" "))
    if cards == 0 and trial == 0:
        break
S = int(input())
h = S//3600
m = (S%3600)//60
s = (S%3600)%60 
print(h:m:s)

### FIXED CODE ###

S = int(input())
h = int(S//3600)
m = int(S%3600)//60
s = int(S%3600)%60 
print("{}:{}:{}".format(h,m,s))
n=raw_input()
x=raw_input().split()

x(reversed(range(10)))

### FIXED CODE ###

n = int(raw_input())
a = map(int,raw_input().split())
a.reverse()
for i in a:
    print i,
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int,input().split()))
b = 0
num = 1000000007
N_1 = N.pop(-1)
N_2 = N.pop(0)
for i in N_1:
    for j in N_2:
        b += N_1 ^ N_2
print(b%num)


### FIXED CODE ###

N = int(input())
A = list(map(int,input().split()))
mod = 10**9 + 7
ans = 0
for k in range(60):
    A_0 = 0
    A_1 = 0
    for i in range(N):
        if A[i]>>k&1:
            A_1 += 1
        else:
            A_0 += 1
    num = A_1 * A_0
    ans += (num * 2 ** k)
ans %=  mod
print(ans)
import sys
input = sys.stdin.readline

def main():
    n , m = map( int, input().split() )
    Sets = []

    Dicto = [ -1 for i in range(n + 1)]

    for i in range( m ):
        a , b = map( int , input().split() )
        if Dicto[a] == -1 and Dicto[b] == -1:
            Sets.append( set( [a,b] ) )
            x_i = len(Sets)
            Dicto[ a ] = x_i - 1
            Dicto[ b ] = x_i - 1
        elif Dicto[ a ] != -1 and Dicto[ b ] == -1:
            Sets[ Dicto[a] ].add( b )
            Dicto[ b ] = Dicto[a]
        elif Dicto[ b ] != -1 and Dicto[ a ] == -1:
            Sets[ Dicto[b] ].add( a )
            Dicto[ a ] = Dicto[b]
        elif Dicto[ b ] != -1 and Dicto[ a ] != -1 and Dicto[ a ] != Dicto[ b ]:
            if Dicto[ b ] > Dicto[ a ]:
                set_x = Sets.pop( Dicto[b] )
                Sets[ Dicto[a] ] |= ( set_x )
                l_set_x = list( set_x )
                for j in l_set_x:
                    Dicto[j] = Dicto[a]

            else:
                set_x = Sets.pop( Dicto[a] )
                Sets[ Dicto[b] ] |= ( set_x )
                l_set_x = list( set_x )
                for j in l_set_x:
                    Dicto[j] = Dicto[b]

    #print(Sets)
    #print(Dicto)

    max_len = 1

    for i in range( len(Sets) ):
        if max_len < len( Sets[i] ):
            max_len = len( Sets[i] )

    print(max_len)


main()

### FIXED CODE ###

import sys
input = sys.stdin.readline

def main():
    n , m = map( int, input().split() )
    Sets = []

    Dicto = [ -1 for i in range(n + 1)]

    for i in range( m ):
        a , b = map( int , input().split() )
        if Dicto[a] == -1 and Dicto[b] == -1:
            Sets.append( set( [a,b] ) )
            x_i = len(Sets)
            Dicto[ a ] = x_i - 1
            Dicto[ b ] = x_i - 1
        elif Dicto[ a ] != -1 and Dicto[ b ] == -1:
            Sets[ Dicto[a] ].add( b )
            Dicto[ b ] = Dicto[a]
        elif Dicto[ b ] != -1 and Dicto[ a ] == -1:
            Sets[ Dicto[b] ].add( a )
            Dicto[ a ] = Dicto[b]
        elif Dicto[ b ] != -1 and Dicto[ a ] != -1 and Dicto[ a ] != Dicto[ b ]:
            if Dicto[ b ] > Dicto[ a ]:
                set_x = Sets[Dicto[b]]
                Sets[ Dicto[a] ] |= ( set_x )
                l_set_x = list( set_x )
                for j in l_set_x:
                    Dicto[j] = Dicto[a]

            else:
                set_x = Sets[Dicto[a]]
                Sets[ Dicto[b] ] |= ( set_x )
                l_set_x = list( set_x )
                for j in l_set_x:
                    Dicto[j] = Dicto[b]

    #print(Sets)
    #print(Dicto)

    max_len = 1

    for i in range( len(Sets) ):
        if max_len < len( Sets[i] ):
            max_len = len( Sets[i] )

    print(max_len)


main()
a=int(input())
b=int(input())
k=int(1)
z=0
while(k<b):
    z+=1
    k+=a-1

print(z)




### FIXED CODE ###

(a,b)=map(int,input().split())
k=int(1)
z=0
while(k<b):
    z+=1
    k+=a-1

print(z)
from collections import Counter
n = int(input())
diffs = []
xi, yi = map(int, input().split())
for _ in range(n - 1):
    xn, yn = map(int, input().split())
    diffs.append((xi - xn, yi - yn))
    xi, yi = xn, yn

c = Counter(diffs)
cv = c.most_common(1)
(p, q), val = c.most_common(1)[0]
print(n - val)




### FIXED CODE ###

from itertools import permutations
from collections import Counter


n = int(input())
balls = []

for _ in range(n):
    x, y = map(int, input().split())
    balls.append((x, y))

if n == 1:
    print(1)
else:
    counter = Counter()
    for b1, b2 in permutations(balls, 2):
        p, q = b1[0] - b2[0], b1[1] - b2[1]
        counter[(p, q)] += 1

    (p, q), val = counter.most_common(1)[0]
    print(n - val)
class Dice:
    def __init__ (self,data):
        self.data = data

    def move(self,direction):
        if direction == 'E':
            self.data[0],self.data[3],self.data[5],self.data[2] = \
                    self.data[3],self.data[5],self.data[2],self.data[0]
        elif direction == 'N':
            self.data[0],self.data[4],self.data[5],self.data[1] = \
                    self.data[1],elf.data[0],self.data[4],self.data[5]
        elif direction == 'S':
            self.data[0],self.data[1],self.data[5],self.data[4] = \
                    self.data[4],self.data[0],self.data[1],self.data[5]
        elif direction == 'W':
            self.data[0],self.data[2],self.data[5],self.data[3] = \
                    self.data[2],self.data[5],self.data[3],self.data[0]

    def getTop(self):
        return self.data[0]

from Dice
dice = Dice(input().split())
cmd = input()
for i in range(len(cmd)):
    dice.move(cmd[i])
    print(dice.getTop())

### FIXED CODE ###

class Dice:
    def __init__ (self,data):
        self.data = data

    def move(self,direction):
        if direction == 'E':
            self.data[0],self.data[3],self.data[5],self.data[2] = \
                    self.data[3],self.data[5],self.data[2],self.data[0]
        elif direction == 'N':
            self.data[0],self.data[4],self.data[5],self.data[1] = \
                    self.data[1],self.data[0],self.data[4],self.data[5]
        elif direction == 'S':
            self.data[0],self.data[1],self.data[5],self.data[4] = \
                    self.data[4],self.data[0],self.data[1],self.data[5]
        elif direction == 'W':
            self.data[0],self.data[2],self.data[5],self.data[3] = \
                    self.data[2],self.data[5],self.data[3],self.data[0]

    def getTop(self):
        return self.data[0]

dice = Dice(input().split())
cmd = input()
for i in range(len(cmd)):
    dice.move(cmd[i])
print(dice.getTop())
from itertools import combinations

n = int(input())
ls_str_l = input().split()

if n < 3:
  print(0)
else:
  ls_l = []
  for l in ls_str_l:
    ls_l.append(int(l))
  
  count = 0
  ls_tri_n = list(combinations(ls_l, 3))
  for ls_tri in ls_tri_n:
    if ls_tri[0] == ls_tri[1] or ls_tri[0] == ls_tri[2] or ls_tri[1] == ls_tri[2]:
      continue
    if ls_tri[0] + ls_tri[1] > ls_tri[2] and ls_tri[0] + ls_tri[2] > ls_tri[1] and \
        ls_tri[1] + ls_tri[2] > ls_tri[0] :
      count += 1

print(count)

### FIXED CODE ###

from itertools import combinations

n = int(input())
ls_str_l = input().split()

if n < 3:
  print(0)
else:
  ls_l = []
  for l in ls_str_l:
    ls_l.append(int(l))
  
  count = 0
  ls_tri_n = list(combinations(ls_l, 3))
  for ls_tri in ls_tri_n:
    if ls_tri[0] == ls_tri[1] or ls_tri[0] == ls_tri[2] or ls_tri[1] == ls_tri[2]:
      continue
    if ls_tri[0] + ls_tri[1] > ls_tri[2] and ls_tri[0] + ls_tri[2] > ls_tri[1] and \
        ls_tri[1] + ls_tri[2] > ls_tri[0] :
      count += 1

  print(count)
#!/usr/bin/env python3

from itertools import product

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

colors = [[0]*C for i in range(3)]
for i in range(N):
    tmp = list(map(int, input().split()))
    for j, color in enumerate(tmp):
        num = ((i+1) + (j+1)) % 3
        colors[num][color-1] += 1
# print(colors)

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


for i in range(3):
    # color[i]
    for j in range(C):
        for k in range(C):
            data[i][k] += colors[i][j] * D[j][k]
# print(data)

ans = 10**10
for iter_ in product([0, 1, 2], repeat=C):
    if len(set(iter_)) != 3:
        continue
    tmp = []
    for i, j in enumerate(iter_):
        tmp.append(data[i][j])
    ans = min(ans, sum(tmp))
print(ans)


### FIXED CODE ###

#!/usr/bin/env python3

from itertools import permutations

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

colors = [[0]*C for i in range(3)]
for i in range(N):
    tmp = list(map(int, input().split()))
    for j, color in enumerate(tmp):
        num = ((i+1) + (j+1)) % 3
        colors[num][color-1] += 1
# print(colors)

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


for i in range(3):
    # color[i]
    for j in range(C):
        for k in range(C):
            data[i][k] += colors[i][j] * D[j][k]
# print(data)

ans = 10**10
for iter_ in permutations(list(range(C)), 3):
    # print(iter_)
    tmp = []
    for i, j in enumerate(iter_):
        tmp.append(data[i][j])
    ans = min(ans, sum(tmp))
print(ans)
n,m = map(int,input().split())
edges = []
for _ in range(m):
    edges.append(list(map(int,input().split())))
ans = []
cur = ((n-1)*n)//2
par = {i:i for i in range(1,n+1)}
size = {i:1 for i in range(1,n+1)}
rank = {i:0 for i in range(1,n+1)}
def find(x):
    if x != par[x]:
        par[x] = find(par[x])
    return par[x]

def union(x,y):
    px,py = find(x),find(y)
    res = 0
    if px != py:
        res = size[px] * size[py]
        if rank[x] < rank[y]:
            par[px] = py
            size[py] += size[px]
        else:
            par[py] = px
            size[px] += size[py]
            if rank[px] == rank[py]:
                rank[px] += 1
    return res

for i in range(m-1,-1,-1):
    ans.append(cur)
    x,y = edges[i]
    cur -= union(x,y)
for i in range(m-1,-1,-1):
    print(ans[i])

### FIXED CODE ###

n,m = map(int,input().split())
edges = []
for _ in range(m):
    edges.append(list(map(int,input().split())))
ans = []
cur = ((n-1)*n)//2
par = {i:i for i in range(1,n+1)}
size = {i:1 for i in range(1,n+1)}
rank = {i:0 for i in range(1,n+1)}
def find(x):
    if x != par[x]:
        par[x] = find(par[x])
    return par[x]

def union(x,y):
    px,py = find(x),find(y)
    res = 0
    if px != py:
        res = size[px] * size[py]
        if rank[px] < rank[py]:
            par[px] = py
            size[py] += size[px]
        else:
            par[py] = px
            size[px] += size[py]
            if rank[px] == rank[py]:
                rank[px] += 1
    return res

for i in range(m-1,-1,-1):
    ans.append(cur)
    x,y = edges[i]
    cur -= union(x,y)
for i in range(m-1,-1,-1):
    print(ans[i])
N = int(input())
P = []
for i in range(N):
  P.append(int(input())
print(sum(P) - max(P) // 2)

### FIXED CODE ###

N = int(input())
P = []
for i in range(N):
  P.append(int(input()))
print(sum(P) - max(P) // 2)
k,x=map(int,input().split())
for i in range(x-k+1,x+k):
  if i<x+k-1:
    print(i,end'')
  else:
    print(i)

### FIXED CODE ###

k,x=map(int,input().split())
for i in range(x-k+1,x+k):
  if i<x+k-1:
    print(i,end=' ')
  else:
    print(i)