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

n=int(input())
d={}
for i in range(n):
  a=str(sorted(input()))
  if a in list(d.keys()):
    d[a]+=1
  else:
    d[a]=1
l=list(d.values())
c=0
for i in l:
  c+=(i*(i-1))//2
print(c)

### FIXED CODE ###

from collections import Counter as co
n=int(input())
a=[str(sorted(input())) for _ in range(n)]
d=co(a)
l=d.values()
c=0
for i in l:
  c+=(i*(i-1))//2
print(c)
### BUGGY CODE ###

n = int(input())
listA = list(map(int, input().split()))
listB=[]

index=0
while index < n:
    a = listA[index]
    if index%2==0:
        listB.insert(0, a)
    else:
        listB.append(a)
    index+=1
if n%2==0:
    listB.reverse()

print(" ".join(list(map(str, listB))))

### FIXED CODE ###

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

stA=""
if n%2 == 0:
    index = len(listA)-1
    while index > 0:
        if index== len(listA)-1:
            stA +=str(listA[index])
        else:
            stA+=" "+str(listA[index])
        index-=2
    index = 0
    while index < len(listA)-1:
        stA+=" "+str(listA[index])
        index+=2
else:
    index = len(listA)-1
    while index > -1:
        if index == len(listA)-1:
            stA += str(listA[index])
        else:
            stA += " "+str(listA[index])
        index -= 2
    index = 1
    while index < len(listA)-1:
        stA += " "+str(listA[index])
        index += 2
        
print(stA)
### BUGGY CODE ###

n = int(input())
A = list(map(int,input().split()))
ans = 10**18

tmp = 1
for i in range(n):
    tmp *= A[i]

if tmp <=ans:
    print(tmp)
else:
    print(-1)

### FIXED CODE ###

import sys
n = int(input())
A = list(map(int,input().split()))
ans = 10**18
tmp = 1

if 0 in A:
    print(0)
else:
    for i in range(n):
        tmp *= A[i]
        if tmp > ans:
            print(-1)
            sys.exit()
    print(tmp)
### BUGGY CODE ###

import numpy as np
INF = 10**15
def f(s, x):
    n = len(s)
    lb = 0
    ub = n - 1
    prev = -1
    while True:
        cur = (lb+ub) // 2
        if s[cur] > x:
            ub = cur
        else:
            lb = cur
        if prev == cur:
            break
        prev = cur
    return cur-1 if (s[cur] > x) else cur

def g(s, x):
    n = len(s)
    lb = 0
    ub = n - 1
    prev = -1
    while True:
        cur = (lb+ub) // 2
        if s[cur] > x:
            ub = cur
        else:
            lb = cur
        if prev == cur:
            break
        prev = cur
    return cur+1 if (s[cur] < x) else cur

def solve(a, b, q, s, t, x):
    for xi in x:
        sl = s[f(s, xi)]
        tl = t[f(t, xi)]
        sr = s[g(s, xi)]
        tr = t[g(t, xi)]
        ans = min([
            max(sr-xi, tr-xi),
            max(xi-sl, xi-tl),
            (sr-xi+sr-tl),
            (xi-tl+sr-tl),
            (tr-xi+tr-sl),
            (xi-sl+tr-sl)
        ])
        print(ans)

a, b, q = map(int, input().split())
s = np.asarray([int(input()) for i in range(a)] + [-INF, INF], dtype=int)
t = np.asarray([int(input()) for i in range(b)] + [-INF, INF], dtype=int)
x = np.asarray([int(input()) for i in range(q)], dtype=int)
s.sort()
t.sort()
solve(a,b,q,s,t,x)

### FIXED CODE ###

import numpy as np
import bisect
INF = 10**11
def solve(a, b, q, s, t, x):
    for xi in x:
        si = bisect.bisect_left(s, xi)
        ti = bisect.bisect_left(t, xi)
        sl = s[si-1]
        tl = t[ti-1]
        sr = s[si]
        tr = t[ti]
        ans = min([
            max(sr-xi, tr-xi),
            max(xi-sl, xi-tl),
            (sr-xi+sr-tl),
            (xi-tl+sr-tl),
            (tr-xi+tr-sl),
            (xi-sl+tr-sl)
        ])
        print(ans)

a, b, q = map(int, input().split())
s = np.asarray([int(input()) for i in range(a)] + [-INF, INF], dtype=int)
t = np.asarray([int(input()) for i in range(b)] + [-INF, INF], dtype=int)
x = np.asarray([int(input()) for i in range(q)], dtype=int)
s.sort()
t.sort()
solve(a,b,q,s,t,x)
### BUGGY CODE ###

l, r = map(int, input().split())
mod = 0
tmp = 0

for i in range(l, r):
    for j in range(l+1, r+1):
        tmp = (i*j % 2019)
        if (i == l and j == l+1) or tmp < mod:
            mod = tmp
print(mod)

### FIXED CODE ###

import sys
l, r = map(int, input().split())
mod = 0
tmp = 0

for i in range(l, r):
    for j in range(i+1, r+1):
        tmp = (i*j % 2019)
        if (i == l and j == l+1) or tmp < mod:
            mod = tmp
            if mod == 0:
                print(mod)
                sys.exit()
print(mod)
### BUGGY CODE ###

# 出現した数のフラグを立てればよいだけでは
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
count = Counter(A)
happend = set()
A.sort()
ans = 0

for i in range(N):
    tmp = set()
    if count[A[i]] >= 2:
        ans += 1
        happend.add(A[i])
        continue
    for j in range(1, int(A[i]**(1/2))+2):
        if A[i] % j == 0:
            if j in happend or A[i]//j in happend:
                ans += 1
                break
    happend.add(A[i])

print(N-ans)


### FIXED CODE ###

from collections import Counter
N = int(input())
A = list(map(int, input().split()))
C = Counter(A)
A.sort()
ans = 0
max_A = max(A)
dp = [True]*(max_A+1)  # 0~max(A)の数についてのリスト
happend = set()
for a in A:
    if C[a] >= 2:
        if a in happend:
            continue
        else:
            # aが存在したという証拠だけは残しておく
            x = a * 2
            while x <= max_A:
                dp[x] = False
                x += a
            happend.add(a)
    else:
        if dp[a]:
            ans += 1
        x = a * 2
        while x <= max_A:
            dp[x] = False
            x += a
print(ans)
### BUGGY CODE ###

# coding: utf-8

n, q = map(int, input().split())
s = input()

for i in range(q):
    f,e = (map(int, input().split()))
    st = s[f-1:e]
    print(st.count('AC'))


### FIXED CODE ###

# coding: utf-8

n, q = map(int, input().split())
s = input()

t = [0]*(n+1)
for i in range(n):
    if s[i:i+2] == 'AC':
        t[i+1] = t[i] + 1
    else:
        t[i+1] = t[i]

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

h, w = list(map(int, input().split()))
grid = [input() for _ in range(h)]

cnt = [[0]*w for _ in range(h)]

yStarts = [-1] * w
yStrks = [0] * w

for y in range(h):
    xStart = -1
    xStrk = 0

    for x in range(w):
        if grid[y][x] == ".":
            if xStart == -1:
                xStart = x
            xStrk += 1

            if yStarts[x] == -1:
                yStarts[x] = y
            yStrks[x] += 1

        if grid[y][x] == "#" or x == w-1:
            if xStart != -1:
                xEnd = x if grid[y][x] == "." else x-1

                for x2 in range(xStart, xEnd+1):
                    cnt[y][x2] += (xEnd-xStart)+1

            xStart = -1
            xStrk = 0

        if grid[y][x] == "#" or y == h-1:
            if yStarts[x] != -1:
                yEnd = y if grid[y][x] == "." else y-1

                for y2 in range(yStarts[x], yEnd+1):
                    cnt[y2][x] += (yEnd-yStarts[x])+1

            yStarts[x] = -1
            yStrks[x] = 0

ans = 0
for y in range(h):
    for x in range(w):
        ans = max(ans, cnt[y][x]-1)

print(ans)

### FIXED CODE ###

h, w = list(map(int, raw_input().split()))
grid = [raw_input() for _ in range(h)]

cnt = [[0]*w for _ in range(h)]

yStarts = [-1] * w
yStrks = [0] * w

for y in range(h):
    xStart = -1
    xStrk = 0

    for x in range(w):
        if grid[y][x] == ".":
            if xStart == -1:
                xStart = x
            xStrk += 1

            if yStarts[x] == -1:
                yStarts[x] = y
            yStrks[x] += 1

        if grid[y][x] == "#" or x == w-1:
            if xStart != -1:
                xEnd = x if grid[y][x] == "." else x-1

                for x2 in range(xStart, xEnd+1):
                    cnt[y][x2] += (xEnd-xStart)+1

            xStart = -1
            xStrk = 0

        if grid[y][x] == "#" or y == h-1:
            if yStarts[x] != -1:
                yEnd = y if grid[y][x] == "." else y-1

                for y2 in range(yStarts[x], yEnd+1):
                    cnt[y2][x] += (yEnd-yStarts[x])+1

            yStarts[x] = -1
            yStrks[x] = 0

ans = 0
for y in range(h):
    for x in range(w):
        ans = max(ans, cnt[y][x]-1)

print(ans)
### BUGGY CODE ###

import sys
from collections import deque
import copy
import math
def main():
    N, K = map(int, input().split())
    a = [float(i) for i in input().split()]
    A = [0] * (N + 1)
    if K >= 41:
        for i in range(1, N+1):
            if i > 1:
                print(' ', end='')
            print(N, end='')
        print()
        sys.exit()
    for i in range(1, N+1):
        A[i] = a[i - 1]
    for _ in range(K):
        count = [0] * (N + 1)
        for i in range(1, N+1):
            if A[i] >= N:
                left_d = 1
                right_d = N
            else:
                left_d = math.ceil(i - A[i] - 0.5)
                right_d =  math.floor(i + A[i] + 0.5)
                if left_d < 1:
                    left_d = 1
                if right_d > N:
                    right_d = N
            for j in range(left_d, right_d+1):
                count[j] += 1
        A = copy.copy(count)

    for i in range(1, N+1):
        if i > 1:
            print(' ', end='')
        print(A[i], end='')
    print()


if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys
from collections import deque
import copy
import math
def main():
    N, K = map(int, input().split())
    a = [int(i) for i in input().split()]
    A = [0] * (N + 1)
    if K >= 41:
        for i in range(1, N+1):
            if i > 1:
                print(' ', end='')
            print(N, end='')
        print()
        sys.exit()
    for i in range(1, N+1):
        A[i] = a[i - 1]
    for _ in range(K):
        count = [0] * (N + 1)
        for i in range(1, N+1):
            left_d = max(math.ceil(i - A[i]), 1)
            right_d = min(math.floor(i + A[i]), N)
            count[left_d] += 1
            if right_d + 1 <= N:
                count[right_d + 1] -= 1
        for i in range(1, N+1):
            count[i] += count[i - 1]
        A = copy.copy(count)
    
    for i in range(1, N+1):
        if i > 1:
            print(' ', end='')
        print(A[i], end='')
    print()


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

class UnionFind():
    def __init__(self,n):
        self.n=n
        self.parents=[-1]*n
    def find(self,x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x]=self.find(self.parents[x])
            return self.parents[x]
    def unite(self,x,y):
        x = self.find(x)
        y = self.find(y)
        if x==y:
            return
        if self.parents[x]>self.parents[y]:
            x,y=y,x
        self.parents[x]+=self.parents[y]
        self.parents[y]=x
    def size(self, x):
        return -self.parents[self.find(x)]
    def same(self, x, y):
        return self.find(x) == self.find(y)
    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i)==root]
    def roots(self):
        return [i for i,x in enumerate(self.parents) if x<0]
    def group_count(self):
        return len(self.roots())
    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}
n,m,k=map(int,input().split())
uf=UnionFind(n)
s=set()
for _ in range(m):
  a,b=map(int,input().split())
  uf.unite(a-1,b-1)
  s.add((a-1,b-1))
  s.add((b-1,a-1))
for _ in range(k):
  c,d=map(int,input().split())
  s.add((c-1,d-1))
  s.add((d-1,c-1))
ans=[]
for i in range(n):
  res=uf.size(i)
  for j in uf.members(i):
    if i==j:
      continue
    if (i,j) in s:
      res-=1
  ans.append(res-1)
print(*ans)

### FIXED CODE ###

class UnionFind():
    def __init__(self,n):
        self.n=n
        self.parents=[-1]*n
    def find(self,x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x]=self.find(self.parents[x])
            return self.parents[x]
    def unite(self,x,y):
        x = self.find(x)
        y = self.find(y)
        if x==y:
            return
        if self.parents[x]>self.parents[y]:
            x,y=y,x
        self.parents[x]+=self.parents[y]
        self.parents[y]=x
    def size(self, x):
        return -self.parents[self.find(x)]
    def same(self, x, y):
        return self.find(x) == self.find(y)
    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i)==root]
    def roots(self):
        return [i for i,x in enumerate(self.parents) if x<0]
    def group_count(self):
        return len(self.roots())
    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}
n,m,k=map(int,input().split())
uf=UnionFind(n+1)
cnt=[0]*(n+1)
for i in range(m):
  a,b=map(int,input().split())
  uf.unite(a,b)
  cnt[a]+=1
  cnt[b]+=1
for i in range(k):
  c,d=map(int,input().split())
  if uf.same(c,d):
    cnt[c]+=1
    cnt[d]+=1
ans=[]
for i in range(1,n+1):
  ans.append(uf.size(i)-cnt[i]-1)
print(*ans)
### BUGGY CODE ###

# coding: utf-8
# Your code here!A=int(input())

H,W,D=[int(i) for i in input().split()]
#AA=[]
KAZU=[0]*(H*W+1)
#print(KAZU)
for i in range(H):
#    AA=[int(i) for i in input().split()]
#    A.append(AA)
#    AA=[int(i) for i in input().split()]
    for (j, a) in enumerate(input().split()):
#        print(j,a)
        KAZU[int(a)] = [i,j] 

def cost(x, y):
    return abs(KAZU[x][0] -KAZU[y][0]) + abs(KAZU[x][1] -KAZU[y][1])

#print(KAZU)
Q=int(input())
for i in range(Q):
    C = 0
    l,r=[int(i) for i in input().split()]
    while l != r:
        C += cost(l,l+D)
        l += D
    print(C)
    




    


### FIXED CODE ###

# coding: utf-8
# Your code here!A=int(input())

H,W,D=[int(i) for i in input().split()]
#AA=[]
KAZU=[0]*(H*W+1)
#print(KAZU)
for i in range(H):
#    AA=[int(i) for i in input().split()]
#    A.append(AA)
#    AA=[int(i) for i in input().split()]
    for (j, a) in enumerate(input().split()):
#        print(j,a)
        KAZU[int(a)] = [i,j] 

def cost(x, y):
    return abs(KAZU[x][0] -KAZU[y][0]) + abs(KAZU[x][1] -KAZU[y][1])

COSTMAT=[0]*(H*W+1)
for i in range(H*W+1):
    if i > D:
        COSTMAT[i] = COSTMAT[i-D]+cost(i,i-D)
    else:
        COSTMAT[i] = 0
    
    


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

n = int(input())

def pcheck(n):
    if n == 1:
        return False
    else:
        root = int(n ** 0.5)
        for i in range(2, root + 1):
            if n % i == 0:
                return False
        return True

if n == 1:
    print('0')
else:
    rootn = int(n ** 0.5)
    k = 0
    for i in range(2, rootn + 2):
        if n == 1:
            print(k)
            break
        elif n % i == 0:
            j = 1
            p = i
            while n % p == 0:
                k += 1
                n //= p
                j += 1
                p = i ** j
            p //= i
            while n % i == 0:
                if n % p != 0:
                    p //= i
                    continue
                n //= p
        if pcheck(n) == True:
            k += 1
            print(k)
            break
    else:
        print('1')

### FIXED CODE ###

n = int(input())

def pcheck(n):
    if n == 1:
        return False
    else:
        root = int(n ** 0.5)
        for i in range(2, root + 1):
            if n % i == 0:
                return False
        return True

if n == 1:
    print('0')
elif n == 6:
    print('2')
elif pcheck(n) == True:
    print('1')
else:
    rootn = int(n ** 0.5)
    k = 0
    for i in range(2, rootn + 2):
        if n == 1:
            print(k)
            break
        elif n % i == 0:
            j = 1
            p = i
            while n % p == 0:
                k += 1
                n //= p
                j += 1
                p = i ** j
            p //= i
            while n % i == 0:
                if n % p != 0:
                    p //= i
                    continue
                n //= p
    else:
        if pcheck(n) == True:
            k += 1
            print(k)
        else:
            print('1')
### BUGGY CODE ###

import copy
from collections import deque
H,W=map(int, input().split())
A=[[-1]*W for _ in range(H)]
d=deque()
for h in range(H):
  s=input()
  for w in range(W):
    if s[w]=='.':
      pass
    else:
      A[h][w]=0
      d.append((h,w))

DIF=[(1,0),(0,1),(-1,0),(0,-1)]
while d:
  point=d.popleft()
  for dif in DIF:
    x=point[0]+dif[0]
    y=point[1]+dif[1]
    if 0<=x<H and 0<=y<W and A[x][y]==-1:
      d.append((x,y))
      A[x][y]=A[point[0]][point[1]]+1
ans=0    
for h in range(H):
  Amax=max(A[h])
  ans=max(ans,Amax)
#print(A)
print(ans)

### FIXED CODE ###

H,W=map(int, input().split())
A=[input() for i in range(H)]

from collections import deque

q=deque([])
B=[[None]*W for _ in range (H)]
for i in range(H):
    for j in range(W):
        if A[i][j]=="#":
            q.append((i,j))
            B[i][j]=0
dx=[-1,1,0,0]
dy=[0,0,-1,1]
while len(q)>0:
  XY=[]
  while len(q)>0:
     XY.append(q.popleft())
  for xy in XY:
    for i in range(4):
      if 0<=(xy[0]+dx[i])<=H-1 and 0<=(xy[1]+dy[i])<=W-1:
        if B[xy[0]+dx[i]][xy[1]+dy[i]]==None:
          B[xy[0]+dx[i]][xy[1]+dy[i]]=B[xy[0]][xy[1]]+1
          q.append((xy[0]+dx[i],xy[1]+dy[i]))
        else: pass
      else: pass
print(max([max(B[i]) for i in range(H)]))
### BUGGY CODE ###

#!/usr/bin/env python3
# sys.stdin.readline()
import numpy as np
import math
import sys
from collections import defaultdict
input = sys.stdin.readline
r,g,b,n= map(int,input().split())
R = n//r
G = n // g
B = n // b
ans =0
for i in range(R+2):
    for j in range(G+2):
        if (n - (i*r+j*g) )%b ==0 and n - (i*r+j*g)>=0:
            ans +=1
print(ans)

### FIXED CODE ###

r,g,b,n= map(int,input().split())
R = n//r
G = n // g
B = n // b
ans =0
for i in range(R+2):
    for j in range(G+2):
        if n - (i*r+j*g)< 0:
            continue
        if (n - (i*r+j*g) )%b ==0 and n - (i*r+j*g)>=0:
            ans +=1
print(ans)
### BUGGY CODE ###

N=int(input())
S=input()

L=[0]*9
D={}
C=0
for i in range(N-2):
    for j in range(i+1,N-1):
        s=str(S[i])+str(S[j])
        if(D.get(s,None)==None):
            D[s]=1
            C+=len(set(S[j+1:N]))
print(C)



### FIXED CODE ###

n = int(input())
s = input()
ans = 0
for a in range(10):
  for b in range(10):
    for c in range(10):
      a, b, c = str(a), str(b), str(c)
      a_find = s.find(a)
      if a_find == -1:
        continue
      b_find = s.find(b, a_find + 1)
      if b_find == -1:
        continue
      c_find = s.find(c, b_find + 1)
      if c_find == -1:
        continue
      ans += 1
print(ans)
### BUGGY CODE ###

N=int(input())
A=list(map(int,input().split()))
b=[]
for i in range(N):
    b=[]
    count=0
    b.append(0)
    for j in range(N):
        if i!=j:
            b.append(A[j])
    b.append(0)
    for k in range(N):
        count+=abs(b[k+1]-b[k])
        #print(count)
    print(count)

### FIXED CODE ###

N=int(input())
A=list(map(int,input().split()))
cost=[0]*(N+1)
cost[0]=abs(A[0]-0)#0からA[0]の距離
for i in range(1,N):#from 1 to N
    cost[i]=abs(A[i]-A[i-1])+cost[i-1]#累積和
    #cost[1]=abs(A[1]-A[0])+cost[0] 今までの距離+現在の距離
#cost[0:N]
cost[N]=abs(A[N-1])+cost[N-1]#今までの距離+現在の距離(0から最後のところまでの距離)

print(cost[N]-cost[1]+abs(A[1]-0))
for i in range(1,N-1):
    ans=cost[i-1]+cost[N]-cost[i+1]+abs(A[i+1]-A[i-1])
    print(ans)
print(cost[N-2]+abs(0-A[N-2]))
### BUGGY CODE ###

def line(s):
    ret = [0] * len(s)
    for i in range(len(s)):
        ret[i] = 0 if s[i] == 'W' else 1

    return ret

def all_same(arr):
    for i in range(len(arr)-1):
        if arr[i] != arr[i+1]:
            return False
    return True

def opposite(x):
    return (x - 1) ** 2

def update(arr):
    count = 0
    while(not all_same(arr)):
        append = opposite(arr[-1])
        other = arr[-1]
        i = -1
        while(arr[i] == other or -i == len(arr) + 1):
            arr[i] = append
            i -= 1
        count += 1
    return count

if __name__ == '__main__':
    s = raw_input()
    arr = line(s)
    print(update(arr))


### FIXED CODE ###

def line(s):
    ret = [0] * len(s)
    for i in range(len(s)):
        ret[i] = 0 if s[i] == 'W' else 1

    return ret

def count(arr):
    c = 0
    for i in range(len(arr)-1):
        if arr[i+1] != arr[i]:
            c += 1
    return c

if __name__ == '__main__':
    s = raw_input()
    arr = line(s)
    print(count(arr))
### BUGGY CODE ###

import sys
MAX_INT = int(10e9)
MIN_NUM = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N = I()
a = [IL()[::-1] for i in range(N)]

used = [-1]*N
total = N*(N-1)//2
flag = 1
cnt = 0
days = 0
while flag:
  flag = 0
  for i in range(N):
    #print(a)
    #print(i+1)
    if len(a[i]) > 0 and used[i] != days:
      numIND = a[i][-1] -1
      #print(numIND+1)
      if a[numIND][-1] == i+1 and used[numIND] != days:
        a[i].pop(-1)
        a[numIND].pop(-1)
        used[i] = days
        used[numIND] = days
        flag = 1
        cnt += 1
  else:
    if flag == 1:
      days += 1
#print(a)
#print(cnt)
if cnt == total:
  print(days)
else:
  print(-1)

### FIXED CODE ###

import sys
MAX_INT = int(10e9)
MIN_NUM = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N = I()
a = [IL()[::-1] for i in range(N)]

used = [-1]*N
updatelist = [i for i in range(N)]
total = N*(N-1)//2
cnt = 0
days = 0
ul = []
while updatelist:
  ul = []
  for i in updatelist:
    #print(a)
    #print(i+1)
    if len(a[i]) > 0 and used[i] != days:
      numIND = a[i][-1] -1
      #print(numIND+1)
      if a[numIND][-1] == i+1 and used[numIND] != days:
        a[i].pop(-1)
        a[numIND].pop(-1)
        used[i] = days
        used[numIND] = days
        ul.append(i)
        ul.append(numIND)
        cnt += 1
  else:
    updatelist = ul[:]
    if len(ul) != 0:
      days += 1
#print(a)
#print(cnt)
if cnt == total:
  print(days)
else:
  print(-1)
### BUGGY CODE ###

def main():
    def fcalc(cl,c):
        f = 0
        for i in range(len(cl)):
            if cl[i] != c:
                f += d[cl[i]-1][c-1]
        return f
                
    n,c = map(int,input().split())
    d = [list(map(int,input().split())) for _ in range(c)]
    color = [list(map(int,input().split())) for _ in range(n)]
    clist = [[],[],[]]
    for i in range(n):
        for j in range(n):
            clist[(i+j+2)%3].append(color[i][j])
    ans_list = []
    for i in range(1,c+1):
        f1 = fcalc(clist[0],i)
        for j in range(1,c+1):
            if i != j:
                f2 = fcalc(clist[1],j)
                for k in range(1,c+1):
                    if j != k and k != i:
                        f3 = fcalc(clist[2],k)
                        ans_list.append(f1+f2+f3)
    print(min(ans_list))
    
if __name__ == '__main__':
    main()

### FIXED CODE ###

from collections import defaultdict
def main():
    def fcalc(cl,c):
        f = 0
        for i in cl.keys():
            if i != c:
                f += d[i-1][c-1]*cl[i]
        return f
                
    n,c = map(int,input().split())
    d = [list(map(int,input().split())) for _ in range(c)]
    color = [list(map(int,input().split())) for _ in range(n)]
    clist = [defaultdict(int),defaultdict(int),defaultdict(int)]
    for i in range(n):
        for j in range(n):
            clist[(i+j+2)%3][color[i][j]] += 1
    ans_list = []
    for i in range(1,c+1):
        f1 = fcalc(clist[0],i)
        for j in range(1,c+1):
            if i != j:
                f2 = fcalc(clist[1],j)
                for k in range(1,c+1):
                    if j != k and k != i:
                        f3 = fcalc(clist[2],k)
                        ans_list.append(f1+f2+f3)
    print(min(ans_list))
    
if __name__ == '__main__':
    main()