Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

line=input()
n,k=map(int,line.split())
s=list(range(n+1))
sums=0
while k!=(n+2):
     mini=sum(list(s[0:k]))
     maxi=sum(list(s[(-k):]))
     sums+=(maxi-mini+1)
     sums=sums%((10**9)+7)
     k+=1
#jojo=(sums%)
print(sums)

### FIXED CODE ###

line=input()
n,k=map(int,line.split())
sums=0
while k!=(n+2):
     if k==1:
          mini=0
     if k>=2:
          mini=(((k)*(k-1))/2)#%((10**9)+7)
     maxi=( (((n+1)*(n)) - ((n+1-k)*(n-k)) )/2  )#%((10**9)+7)
     sums+=(maxi-mini+1)
     sums=sums%((10**9)+7)
     k+=1
print(int(sums))
### BUGGY CODE ###

from fractions import gcd
from functools import reduce

def gcd_list(numbers):
    return reduce(gcd, numbers)

def main():
    n = int(input())
    A = list(map(int, input().split()))

    gcd_max = 1
    for i in range(n):
        if i == n-1:
            temp = A[i]
            A[i] = A[i-1]
            gcd_max = max(gcd_max, gcd_list(A))
            A[i] = temp
            break
        temp = A[i]
        A[i] = A[i+1]
        gcd_max = max(gcd_max, gcd_list(A))
        A[i] = temp
    print(gcd_max)


if __name__ == '__main__':
    main()


### FIXED CODE ###

from fractions import gcd


def main():
    n = int(input())
    A = list(map(int, input().split()))

    L = []
    R = []
    for i in range(n):
        if i == 0:
            L.append(A[0])
            R.append(A[-1])
            continue
        L.append(gcd(L[i-1],A[i]))
        R.append(gcd(R[i-1],A[-i-1]))
    R.reverse()

    for i in range(n):
        if i == 0:
            gcd_max = R[1]
        elif i != n-1:
            gcd_max = max(gcd_max, gcd(L[i-1], R[i+1]))
        else:
            gcd_max = max(gcd_max, L[-2])
    print(gcd_max)


if __name__ == '__main__':
    main()
### BUGGY CODE ###

H,W,K = map(int,input().split())
A = [0]*H
for i in range(H):
    A[i] = input()
s = 10000
for i in range(2**(H-1)):
    count = 0
    B = [0]*H
    for j in range(H-1):
        if (i >> j & 1):
            B[j] = 1
            count += 1
    C = [0]*(count + 1)
    for j in range(W):
        a = [0]*(count + 1)
        n = 0
        for k in range(H):
            a[n] += int(A[k][j])
            C[n] += int(A[k][j])
            if B[k] == 1:
                n += 1      
        if max(C) > K:
            count += 1
            C[:] = a[:]
    if s > count:
        s = count
print(s)

### FIXED CODE ###

H,W,K = map(int,input().split())
A = [0]*H
for i in range(H):
    A[i] = input()
s = 10000
for i in range(2**(H-1)):
    count = 0
    B = [0]*H
    B[H-1] = 1
    for j in range(H-1):
        if ((i >> j) & 1):
            B[j] = 1
            count += 1
    C = [0]*(count + 1)
    for j in range(W):
        a = [0]*(count + 1)
        n = 0
        max = C[0]
        maxa = a[0]
        for k in range(H):
            a[n] += int(A[k][j])
            C[n] += int(A[k][j])
            if B[k] == 1:
                if max < C[n]:
                    max = C[n]
                if maxa < a[n]:
                    maxa = a[n]
                n += 1
        if max > K:
            count += 1
            C[:] = a[:]
        if maxa > K:
            count = 10000
            break
    if s > count:
        s = count
print(s)
### BUGGY CODE ###

import sys

n = int(sys.stdin.readline().rstrip())
ab = map(int, sys.stdin.read().split())
ab = list(zip(ab, ab))

def main():
    c_a = ab[0][0]
    c_b = ab[0][1]
    for a, b in ab[1:]:
        ratio = a / b
        while c_a / c_b != ratio:
            if c_a / c_b < ratio:
                c_a += 1
            else:
                c_b += 1
    
    ans = c_a + c_b
    return ans

if __name__ == '__main__':
    ans = main()
    print(ans)

### FIXED CODE ###

import sys

n = int(sys.stdin.readline().rstrip())
ab = map(int, sys.stdin.read().split())
ab = zip(ab, ab)

def main():
    c_a = c_b = 1
    for a, b in ab:
        t_a = (c_a + a - 1) // a
        t_b = (c_b + b - 1) // b
        t = max(t_a, t_b)
        c_a = a * t
        c_b = b * t
    
    ans = c_a + c_b
    return ans

if __name__ == '__main__':
    ans = main()
    print(ans)
### BUGGY CODE ###

import math
N,D,A=map(int,input().split())
l=[]
for i in range(N):
  k=list(map(int,input().split()))
  l.append(k)
l.sort()
ma=[0]*N
tmp=1
for i in range(N):
  x=l[i][0]
  h=l[i][1]
  tmp=max(tmp,i+1)
  for j in range(tmp,N):
    if abs(l[j][0]-(x+D))>D:
      ma[i]=j
      tmp=j
      break
  else:
    ma[i]=N
    tmp=N
ans=0
for i in range(N):
  h=l[i][1]
  if h<=0:continue
  d=l[i][0]+D
  time=math.ceil(h/A)
  for j in range(i,ma[i]):
    l[j][1]-=time*A
  ans+=time
print(ans)

### FIXED CODE ###

import math
N,D,A=map(int,input().split())
l=[]
for i in range(N):
  k=list(map(int,input().split()))
  l.append(k)
l.sort()
S=[0]*N
ma=[0]*N
tmp=N-1
for i in reversed(range(N)):
  x=l[i][0]
  for j in reversed(range(0,tmp)):
    if abs(l[j][0]-(x-D))>D:
      ma[i]=j+1
      tmp=j+1
      break
  else:
    ma[i]=0
    tmp=0
ans=0
S[0]=math.ceil(l[0][1]/A)
for i in range(1,N):
  h=l[i][1]
  left=S[ma[i]-1] if ma[i]>0 else 0
  right=S[i-1]
  sum_=(right-left)*A
  if h<=sum_:
    S[i]=S[i-1]
    continue
  time=math.ceil((h-sum_)/A)
  S[i]=time+S[i-1]
print(S[N-1])
### BUGGY CODE ###

import sys
input = sys.stdin.readline

N, K=map(int,input().strip().split(' '))
xl=[]
yl=[]
for i in range(N):
    xt,yt=map(int,input().strip().split(' '))
    xl.append(xt)
    yl.append(yt)
ans=10**20
x=sorted(xl)
y=sorted(yl)
for i in range(N):
    for j in range(i+1,N):
        xmin=x[i]
        xmax=x[j]
        for k in range(N):
            for l in range(k+1,N):
                ymin=y[k]
                ymax=y[l]
                A=(xmax-xmin)*(ymax-ymin)
                ct=0
                for m in range(N):
                    if xmin<=xl[m] and xl[m]<=xmax and ymin<=yl[m] and yl[m]<=ymax:
                        ct+=1
                if ct>=K:
                    ans=min(ans,A)
print(ans)



### FIXED CODE ###

import sys
input = sys.stdin.readline

N, K=map(int,input().strip().split(' '))
x=[]
y=[]
point=[]
for i in range(N):
    xt,yt=map(int,input().strip().split(' '))
    x.append(xt)
    y.append(yt)
    point.append([xt,yt])
ans=10**20
x.sort()
y.sort()
point.sort(key=lambda x:x[1])


for i in range(N):
    for j in range(i+1,N):
        xmin=x[i]
        xmax=x[j]
        for k in range(N):
            ymin=y[k]
#            ymax=y[l]
#            
            ct=0
            for p in point:
                if xmin<=p[0] and p[0]<=xmax and ymin<=p[1]:
                    ct+=1
                if ct==K:
                    A=(xmax-xmin)*(p[1]-ymin)
                    ans=min(ans,A)
                    break
print(ans)
### BUGGY CODE ###

N, Q = input().split()
S = input()
for i in range(int(Q)):
    str_l, str_r = input().split()
    l = int(str_l)
    r = int(str_r)
    count = 0
    for j in range(0, r - l + 1, 2):
        if 'A' == S[j+l-1]:
            if 'C' == S[j+l]:
                count += 1
        elif 'C' == S[j+l-1]:
            if j == 0:
                continue
            if 'A' == S[j+l-2]:
                count += 1
    print(count)

### FIXED CODE ###

N, Q = map(int, input().split())
S = input()

AC_appearance = [0] * len(S)
counter = 0
for i in range(1, len(S)):
    if S[i-1] == 'A' and S[i] == 'C':
        counter += 1
    AC_appearance[i] = counter 

for i in range(Q):
    l, r = map(int, input().split())
    l = l - 1
    r = r - 1
    print(AC_appearance[r] - AC_appearance[l])
### BUGGY CODE ###

n = int(input())
lis = list(map(int, input().split()))
count = 0
for i in range(n):
    newlis = lis[0:i+1]
    if min(newlis) == lis[i]:
        count += 1
print(count)

### FIXED CODE ###

n = int(input())
lis = list(map(int, input().split()))
count = 0
minn = lis[0]
minl = min(lis)
flug = 0
for i in range(n):
    if flug == 0:
        if  minn >= lis[i]:
            count += 1
            minn = lis[i]
            if minn == minl:
                count += lis[i+1:].count(minl)
                flug = 1
print(count)
### BUGGY CODE ###

import sys
input = sys.stdin.readline
def main():
    n,m = map(int,input().split())
    edge = [[] for _ in [0]*n]
    for _ in range(m):
        a,b = map(int,input().split())
        edge[a-1].append(b-1)


    new = [tuple([i]) for i in range(n)]
    res = []
    tank = []
    res2 = -1

    while len(new) > 0:
        tank = []
        for i in range(len(new)):
            el = new[i]

            for go in edge[el[-1]]:
                if not go in el:
                    if el[0] in edge[go]:
                        print(len(el)+1)
                        for e in el:
                            print(e+1)
                        print(go+1)

                        exit()
                    tank.append(el+(go,))
        new = tank
        
    print(-1)

if __name__ == '__main__':
    main()

### FIXED CODE ###

import sys
input = sys.stdin.readline

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

edge = [[] for _ in [0]*n]
back = [[] for _ in [0]*n]

for _ in [0]*m:
    a,b = map(int,input().split())
    edge[a-1].append(b-1)
    back[b-1].append(a-1)


res = [None]*(n+1)

for end in range(n):
    for start in edge[end]:
        cost = [-1]*n
        cost[start] = 0
        new = [start]
        tank = []
        judge = True

        while len(new) > 0 and judge:
            tank = []
            for fr in new:
                for go in edge[fr]:
                    if cost[go] < 0:
                        cost[go] = cost[fr] + 1
                        tank.append(go)
                        if go == end:
                            judge = False
            new = tank

        
        if cost[end] >= 0 and cost[end] < len(res):
            p = end
            res = [p+1]
            while p != start:
                for e in back[p]:
                    if cost[p] == cost[e]+1:
                        p = e
                        res.append(p+1)
                        break
if res[0] == None:
    print(-1)
else:                      
    print(len(res))
    for e in res:
        print(e)
### BUGGY CODE ###

from operator import mul
from functools import reduce

def cmb(n, r, p):
    mem_key = str(n) + '-' + str(r)
    if mem_key in mem:
        return mem[mem_key]

    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)

    val = fact[n] * factinv[r] * factinv[n-r] % p
    mem[mem_key] = val
    return val

MOD = 10 ** 9 + 7
N, K = map(int, input().split())
if K == 1:
    print(0)
    exit()

A = sorted(list(map(int, input().split())))

N = len(A)
fact = [1, 1]
factinv = [1, 1]
inv = [0, 1]
for i in range(2, N + 1):
    fact.append((fact[-1] * i) % MOD)
    inv.append((-inv[MOD % i] * (MOD // i)) % MOD)
    factinv.append((factinv[-1] * inv[-1]) % MOD)

ans = 0
sum_num = 0
mem = {}
for i in range(N - (K - 1)):
    for j in range(i+K-1, N):
        ans += (A[j] - A[i]) * cmb(j-i-1, K-2, MOD) % MOD
print(ans % MOD)


### FIXED CODE ###

MOD = 10 ** 9 + 7

def ncr(n, r, mod=MOD):
    return (fct[n] * invf[r] * invf[n - r]) % mod


def make_tables(n, mod=MOD):
    fct = [1 for _ in range(n + 1)]
    invf = [1 for _ in range(n + 1)]

    for i in range(2, n + 1):
        fct[i] = (fct[i - 1] * i) % mod
        invf[i] = divmod(fct[i])
    return fct, invf


def divmod(a, mod=MOD):
    return pow(a, mod - 2, mod)


N, K = map(int, input().split())
A = sorted(list(map(int, input().split())))
for i in range(N):
    A[i] %= MOD

fct, invf = make_tables(N, mod=MOD)

ans = 0
for i in range(N - K + 1):
    ans += ((A[-1-i] - A[i]) % MOD * ncr(N - i - 1, K - 1)) % MOD
print(ans % MOD)
### BUGGY CODE ###

import sys

def max_heapify(A, i):
  l = i * 2
  r = l + 1
  largest = i
  H = len(A) -1
  if l <= H and A[l] > A[i]:
      largest = l
  if r <= H and A[r] > A[largest]:
      largest = r
  if largest != i:
    A[i], A[largest] = A[largest], A[i]
    max_heapify(A, largest)

def build_max_heap(A):
    for i in range(len(A)-1 // 2, 0, -1):
        max_heapify(A, i)

def extract(A):
    out = A[1]
    A[1] = A[-1]
    del A[-1]
    max_heapify(A, 1)
    return out

_input = sys.stdin.readlines()
A = [0]

def main():
    out = []
    for l in _input:
        if l[0] == "e":
            if l[1] == "n":
                #end
                print(*out, sep = "\n")
                return
            else:
                #print(A)
                out.append(extract(A))
        else:
            #insert k
            k = int(l.split()[1])
            A.append(k)
            build_max_heap(A)
        #print(A)

if __name__ == "__main__":
    main()

### FIXED CODE ###

import sys

#@profile
def max_heapify(A, i):
    j = i
    largest = i
    H = len(A) -1
    while True:
        l = j * 2
        r = l + 1
        if l <= H and A[l] > A[j]:
            largest = l
        if r <= H and A[r] > A[largest]:
            largest = r
        if largest != j:
            A[j], A[largest] = A[largest], A[j]
            #max_heapify(A, largest)
            j = largest
            
        else:
            break
        

#@profile
def build_max_heap(A):
    i = len(A)-1
    last = A[i]
    parent = i//2
    while  parent > 0 and A[parent] < last:
        A[i] = A[parent]
        i = parent
        parent = i //2
    A[i] = last

#@profile
def extract(A):
    print(A[1])
    A[1] = A[-1]
    del A[-1]
    max_heapify(A, 1)
    
A = [0]

#@profile
def main():
    out = ""
    for l in sys.stdin.readlines():
        if l[0] == "i":
            #insert k
            A.append(int(l[7:]))
            build_max_heap(A)
        #print(A)
        elif l[1] == "n":
            #end
            print(out, end = "")
            return
        else:
            if len(A) <= 1:
                pass
            elif len(A) == 2:
                out += str(A.pop()) + "\n"
                #print(A.pop())
            #extract(A)
            else:
                out += str(A[1]) + "\n"
                A[1] = A.pop()
                max_heapify(A, 1)
                
if __name__ == "__main__":
    main()
### BUGGY CODE ###

N, M = map(int, input().split())
S = input()
T = input()

def gcd(a, b):
    if a < b:
        return gcd(b, a)
    else:
        return a if b == 0 else gcd(b, a % b)

def lcm(a, b):
    return a * b // gcd(b, a%b)
L = lcm(N, M)

r = ["*" for i in range(L)]
f = False

s_step = L // N
t_step = L // M
sL = lcm(s_step, t_step)
s = 0
t = 0

for i in range(0, L, sL):
    if i == 0:
        if S[i] == T[i]:
            continue
        else:
            f = True
            break
    else:
        s = i // s_step
        t = i // t_step
        if s > N-1:
            break
        if t > M -1:
            break
        if S[s] == T[t]:
            continue
        else:
            f = True
            break


if f:
    print(-1)
else:
    print(L)


### FIXED CODE ###

N, M = map(int, input().split())
S = input()
T = input()
# def gcd(a, b):
#     if a < b:
#         return gcd(b, a)
#     else:
#         return a if b == 0 else gcd(b, a % b)

import fractions
def lcm(a, b):
    return a * b // fractions.gcd(b, a % b)


L = lcm(N, M)

f = False

s_step = L // N
t_step = L // M
sL = lcm(s_step, t_step)
s = 0
t = 0

for i in range(0, L, sL):
    if i == 0:
        if S[i] == T[i]:
            continue
        else:
            f = True
            break
    else:
        s = i // s_step
        t = i // t_step
        if s > N - 1:
            break
        if t > M - 1:
            break
        if S[s] == T[t]:
            continue
        else:
            f = True
            break

if f:
    print(-1)
else:
    print(L)
### BUGGY CODE ###


N = int(input())

a = []

for i in range(N):

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

    a.append(A)

day = 0
flag = True
end = 0

while end < N:

    day += 1

    Q = []
    flag = False

    for i in range(N):


        if len(a[i]) > 0  and a[ a[i][0]-1 ][0] == i + 1 and a[i][0] > i + 1:

            Q.append(a[i][0])
            Q.append(i + 1)

            flag = True

    if not flag:
        break

    #print (a,Q)

    for q in Q:
        del a[q-1][0]

        if len(a[q-1]) == 0:
            end += 1



if flag:
    print (day)
else:
    print (-1)
        
        

### FIXED CODE ###


N = int(input())

A = []

for i in range(N):

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

    A.append(a)

Q = []
for i in range(N):

    if A[ A[i][0]-1 ][0] == i + 1 and A[i][0]-1 > i:
        Q.append([min(A[i][0]-1,i),max(A[i][0]-1,i),1])

day = 0

while len(Q) > 0:

    q = Q.pop(0)

    del A[q[0]][0]
    del A[q[1]][0]




    if len(A[q[0]]) > 0 and A[ A[q[0]][0] - 1 ][0] == q[0] + 1:
        Q.append([min(A[q[0]][0] - 1,q[0]),max(A[q[0]][0] - 1,q[0]),q[2]+1])

    if len(A[q[1]]) > 0 and A[ A[q[1]][0] - 1 ][0] == q[1] + 1:
        Q.append([min(A[q[1]][0] - 1,q[1]),max(A[q[1]][0] - 1,q[1]),q[2]+1])

    day = max(day,q[2])

flag = True

for a in A:
    if len(a) > 0:
        flag = False
        
if flag:
    print (day)
else:
    print (-1)
### BUGGY CODE ###

s = list(input())
q = int(input())
rev = False
for i in range(q):
    inp = input()
    if inp[0] == "1":
        if rev:
            rev = False
        else:
            rev = True
    elif inp[2] == "1":
        a = inp[4]
        if rev:
            s.append(a)
        else:
            s.insert(0, a)
    else:
        a = inp[4]
        if rev:
            s.insert(0, a)
        else:
            s.append(a)
if rev:
    s.reverse()
print("".join(s))

### FIXED CODE ###

s = list(input())
q = int(input())
l1 = []
l2 = []
rev = False
for i in range(q):
    inp = input()
    if inp[0] == "1":
        if rev:
            rev = False
        else:
            rev = True
    elif inp[2] == "1":
        a = inp[4]
        if rev:
            l2.append(a)
        else:
            l1.append(a)
    else:
        a = inp[4]
        if rev:
            l1.append(a)
        else:
            l2.append(a)
l1.reverse()
ans = l1 + s + l2
if rev:
    ans.reverse()
print("".join(ans))
### BUGGY CODE ###

# coding: utf-8
import sys


def change_into_int(lists):
    tmps = lists.split(' ')
    num_list = []
    for n in tmps:
        num_list.append(int(n))
    return num_list


def create_result(one_list):
    ans = 1
    for n in one_list:
        ans = ans * n
    return ans

            
def create_lists(data, point):
    num_lists = []
    flag = 0
    count = 0
    while(flag == 0):
        tmp_list = []
        for i in range(point):
            tmp_list.append(data[i+count])
            # print(i+count)
            if i+count == len(data)-1:
                flag = 1
        count = count + 1
        num_lists.append(tmp_list)
    return num_lists


if __name__ == "__main__":
    lines = []
    result_list = []
    for l in sys.stdin:
        lines.append(l.rstrip('\r\n'))

    num1 = change_into_int(lines[0])
    data = change_into_int(lines[1])
    lists = create_lists(data, num1[1])
    for one_list in lists:
        result_list.append(create_result(one_list))

    for n in range(len(result_list)-1):
        if result_list[n] < result_list[n+1]:
            print("Yes")
        else:
            print("No")


### FIXED CODE ###

# coding: utf-8
import sys
from math import log


def change_into_int(lists):
    tmps = lists.split(' ')
    num_list = []
    for n in tmps:
        num_list.append(int(n))
    return num_list


if __name__ == "__main__":
    lines = []
    result_list = []
    # 入力
    for l in sys.stdin:
        lines.append(l.rstrip('\r\n'))
    # int
    num1 = change_into_int(lines[0])
    data = change_into_int(lines[1])
    i = 0
    while(True):
        ans = data[i] - data[i+num1[1]]
        if ans < 0:
            print("Yes")
        else:
            print("No") 
        i = i + 1
        if i+num1[1] == len(data):
            break
### BUGGY CODE ###

import bisect
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
A.sort()
B.sort()
C.sort()

bc = [None]*N
for i in range(N):
    bc[i] = N - bisect.bisect_right(C, B[i])
ans = 0
for i in range(N):
    big = bisect.bisect_right(B, A[i])
    for j in range(big, N):
        ans += bc[j]
print(ans)


### FIXED CODE ###

import bisect
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
A.sort()
B.sort()
C.sort()

bc = [None]*(N+1)
bc[N] = 0
for i in range(N-1, -1, -1):
    bc[i] = bc[i+1] + N - bisect.bisect_right(C, B[i])
ans = 0
for i in range(N):
    ans += bc[bisect.bisect_right(B, A[i])]
print(ans)
### BUGGY CODE ###

#import numpy as np
n, k = map(int, input().split())
p = list(map(int, input().split()))
m = 0
for i in range(len(p)-k+1):
  s = sum(p[i:i+k])
  if s>m: m=s  
print((m+k)/2)
#c = [sum(p[i:i+k]) for i in range(len(p)-k+1)]
#print(c)
#pn = np.array(p)
#v = np.array([1]*k)
#c = np.convolve(pn, v, mode='same')
#print((max(c)+k)/2)

### FIXED CODE ###

n, k = map(int, input().split())
p = list(map(int, input().split()))
s = 0
rs = sum(p[0:k])
ms = rs
for i in range(1, n - k + 1):
  rs = rs - p[i-1] + p[i+k-1]
  if rs > ms:
    s = i
    ms = rs

ans = 0.0
for pp in p[s:s+k]:
  ans += (1+pp)/2
print(ans)

#for i in range(len(p)-k+1):
#  s = sum(p[i:i+k])
#  if s>m: m=s  

#c = [sum(p[i:i+k]) for i in range(len(p)-k+1)]
#print(c)
#pn = np.array(p)
#v = np.array([1]*k)
#c = np.convolve(pn, v, mode='same')
#print((max(c)+k)/2)
### BUGGY CODE ###

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

Lists=[]
for i in range(n):
    Lists.append(a.count(i+1))
#print(a)
#print(Lists)
for i in range(n):
    ans=0
    Lists[(a[i])-1]-=1
    for j in range(n):
        if ((Lists[j])>1):
            ans+=Lists[j]*(Lists[j]-1)//2
    Lists[a[i]-1]+=1
    print(ans)

### FIXED CODE ###

n=int(input())
a=list(map(int,input().split()))
List={}
x=0
for i in range(n):
    if not a[i] in List:
        List[a[i]] = 1
    else:
        List[a[i]]+=1
    
sigma=0

for key,item in List.items():
    sigma += item*(item-1)//2
for i in range(n):
    print(sigma-List[a[i]]+1)
### BUGGY CODE ###

import sys
def main():
    input = sys.stdin.readline
    N,C=map(int, input().split())
    D=[tuple(map(int, input().split())) for _ in range(C)]
    c=[tuple(map(int, input().split())) for _ in range(N)]

    ans = 10**10
    from itertools import permutations
    for colors in permutations(range(C), 3):
        tmp = 0
        for i in range(N):
            for j in range(N):
                tmp += D[c[i][j]-1][colors[(i+j)%3]]
        ans = min(ans, tmp)
    print(ans)


if __name__ == '__main__':
    main()

### FIXED CODE ###

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

d = [[] for _ in range(3)]

for color in range(1,C+1):
    t = [0]*3
    for i in range(N):
        for j in range(N):
            if c[i][j] != color:
                t[(i+j)%3] += D[c[i][j]-1][color-1]
    for k in range(3):
        d[k].append((color, t[k]))

for k in range(3):
    d[k].sort(key=lambda x:x[1])

ans = 10**9
for i in range(3):
    for j in range(3):
        for k in range(3):
            if len({d[0][i][0], d[1][j][0], d[2][k][0]}) != 3: continue
            ans = min(ans, d[0][i][1] + d[1][j][1] + d[2][k][1])
print(ans)