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

import bisect
import heapq
import itertools
import math
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from fractions import gcd
from functools import lru_cache, reduce

import numpy as np

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

sys.setrecursionlimit(2147483647)
INF = float("inf")

N, A, B, C = list(map(int, sys.stdin.readline().split()))
L = [int(sys.stdin.readline()) for _ in range(N)]

L = np.array(L)

# 全部試す
choosing = list(itertools.product([0, 1, 2, 3], repeat=N))
# 0: 選ばない
# 1: 竹 A にする
# 2: 竹 B にする
# 3: 竹 C にする
ans = INF
for choose in np.array(choosing):
    a = L[choose == 1]
    b = L[choose == 2]
    c = L[choose == 3]
    if len(a) == 0 or len(b) == 0 or len(c) == 0:
        continue
    s = 0
    s += abs(A - a.sum()) + (len(a) - 1) * 10
    s += abs(B - b.sum()) + (len(b) - 1) * 10
    s += abs(C - c.sum()) + (len(c) - 1) * 10
    ans = min(ans, s)
print(ans)


### FIXED CODE ###

import functools
import itertools

import os
import sys

sys.setrecursionlimit(10000)
INF = float('inf')


def debug(fn):
    if not os.getenv('LOCAL'):
        return fn

    @functools.wraps(fn)
    def wrapper(*args, **kwargs):
        ret = fn(*args, **kwargs)
        print('DEBUG: {}({}) -> '.format(
            fn.__name__,
            ', '.join(
                list(map(str, args)) +
                ['{}={}'.format(k, str(v)) for k, v in kwargs.items()]
            )
        ), end='')
        print(ret)
        return ret

    return wrapper


def count_mp(target, sizes):
    return (len(sizes) - 1) * 10 + abs(target - sum(sizes))


N, A, B, C = map(int, input().split())
sizes = []
for _ in range(N):
    sizes.append(int(input()))

ans = INF
for allocs in itertools.product('abc_', repeat=N):
    a = []
    b = []
    c = []
    mp = 0
    for i, al in enumerate(allocs):
        if al == 'a':
            a.append(sizes[i])
        if al == 'b':
            b.append(sizes[i])
        if al == 'c':
            c.append(sizes[i])
    if not a or not b or not c:
        continue
    ans = min(ans, count_mp(A, a) + count_mp(B, b) + count_mp(C, c))
    if ans == 0:
        break
print(ans)
### BUGGY CODE ###

A, B, M = map(int, input().split())
a = list(input().split())
b = list(input().split())
l = []
for i in range(M):
  x, y, c = map(int, input().split())
  l.append(int(a[x-1])+int(b[y-1])-c)
for k in range(A):
  for m in range(B):
    l.append(int(a[k])+int(b[m]))
l.sort()
print(l[0])

### FIXED CODE ###

A, B, M = map(int, input().split())
a = list(input().split())
b = list(input().split())
l = []
for i in range(M):
  x, y, c = map(int, input().split())
  l.append(int(a[x-1])+int(b[y-1])-c)
l.sort()
n = int(min(a)) + int(min(b))
print(min(l[0], n))
### BUGGY CODE ###

# import math
import functools

n = int(input())
as_ = [int(i) for i in input().split()]


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


def gcds(list_):
    return functools.reduce(gcd, list_)


def drop(list_, i):
    list_copy = list_.copy()
    del list_copy[i]
    return list_copy


print(max(gcds(drop(as_, i)) for i, _ in enumerate(as_)))


### FIXED CODE ###

import fractions
import itertools

n = int(input())
as_ = [int(i) for i in input().split()]


def gcds(list_):
    return list(itertools.accumulate(list_, fractions.gcd))


def reversed_gcds(list_):
    return list(itertools.accumulate(reversed(list_), fractions.gcd))


as_accum_gcd, as_rev_accum_gcd = gcds(as_), reversed_gcds(as_)

dropped_gcds = []
for i in range(len(as_)):
    if i == 0:
        dropped_gcd = as_rev_accum_gcd[-2]
    elif i == len(as_) - 1:
        dropped_gcd = as_accum_gcd[-2]
    else:
        dropped_gcd = fractions.gcd(as_accum_gcd[i - 1], as_rev_accum_gcd[-2 - i])
    dropped_gcds.append(dropped_gcd)

print(max(dropped_gcds))
### BUGGY CODE ###

import numpy as np
s = input()
dp = np.zeros(13, dtype=int)
dp[0] = 1
m = 1
for ind, i in enumerate(reversed(s)):
    tmp = np.zeros(13, dtype=int)
    for j in range(10):
        if i!='?' and int(i)!=j:
            continue
        tmp += np.roll(dp, j*m%13)
    dp = tmp%(10**9+7)
    m = 10*m%13
print(dp[5])


### FIXED CODE ###

import numpy as np
s = input()

A = []
for i in range(6):
    m = [-(j*10**i%13)%13 for j in range(10)]
    tmp = []
    for j in m:
        tmp.append([[1 if (k-l)%13==j else 0 for k in range(13)] for l in range(13)])
    tmp.append([[1 if (k-l)%13 in m else 0 for k in range(13)] for l in range(13)])
    A.append(tmp)
A = np.array(A)

dp = np.zeros(13, dtype=int)
dp[0] = 1
m = 1
for ind, i in enumerate(reversed(s)):
    i = int(i) if i!='?' else 10
    tmp = np.dot(A[ind%6, i, :, :], dp)
    dp = tmp%(10**9+7)
print(dp[5])
### BUGGY CODE ###

from scipy.sparse import csr_matrix, lil_matrix#pypyだとエラーになるので、pythonを使うこと
from scipy.sparse.csgraph import shortest_path

import sys
def input():
	return sys.stdin.readline().strip()

N, M, L = list(map(int, input().split()))

graph = lil_matrix((N, N))

for i in range(M):
	A, B, C = list(map(int, input().split()))
	graph[A - 1, B - 1] = C
	graph[B - 1, A - 1] = C

graph = csr_matrix(graph)

dist = shortest_path(graph, method='FW')
graph2 = lil_matrix((N, N))
for i in range(N):
	for j in range(N):
		if dist[i, j] <= L:
			graph2[i, j] = 1
graph2 = csr_matrix(graph2)
num = shortest_path(graph2, method="FW")


Q = int(input())
for q in range(Q):
	s, t = list(map(int, input().split()))
	if num[s - 1][t - 1] < 10000000:
		print(int(num[s - 1][t - 1] - 1))
	else:
		print(-1)

### FIXED CODE ###


import sys
from collections import deque, defaultdict
import copy
import bisect
sys.setrecursionlimit(10 ** 9)
import math
import heapq
from itertools import product, permutations,combinations
import fractions
from operator import itemgetter

import sys
def input():
	return sys.stdin.readline().strip()

N, M, L = list(map(int, input().split()))

graph = [[] for _ in range(N)]

for i in range(M):
	A, B, C = list(map(int, input().split()))
	if C <= L:
		graph[A - 1].append((B - 1, C))
		graph[B - 1].append((A - 1, C))

num_matrix = [[0]*N for i in range(N)]


for start in range(N):
	dist = [(1000000, 0)]*N
	que = []
	heapq.heappush(que, (0, 0, start))
	dist[start] = (0, 0)
	while len(que) > 0:

		node = heapq.heappop(que)
		#print(start, que, node)
		if node[0] == dist[node[2]][0] and node[1] == dist[node[2]][1]:
			#print(1)
			for edge in graph[node[2]]:
				#print(edge)
				#print(edge[1] + dist[node[2]][1] )
				if edge[1] + dist[node[2]][1] > L:
					next_dist = (dist[node[2]][0] + 1, edge[1], edge[0])
				else:
					next_dist = (dist[node[2]][0], dist[node[2]][1] + edge[1], edge[0])
				if dist[edge[0]][0] > next_dist[0]:
					dist[next_dist[2]] = (next_dist[0], next_dist[1])
					heapq.heappush(que, next_dist)
				elif dist[edge[0]][0] == next_dist[0] and dist[edge[0]][1] > next_dist[1]:
					heapq.heappush(que, next_dist)
					dist[next_dist[2]] = (next_dist[0], next_dist[1])
	for i in range(N):
		num_matrix[start][i] = dist[i][0]

#print(num_matrix)

Q = int(input())
for q in range(Q):
	s, t = list(map(int, input().split()))
	if num_matrix[t - 1][s - 1] < 1000:
		print(num_matrix[t - 1][s - 1])
	else:
		print(-1)
### BUGGY CODE ###

s = tuple(map(lambda c: ord(c)-ord('a'),input()))

from collections import defaultdict

dp = defaultdict(lambda: len(s))
dp[0] = 0


def it(x):
  for i in range(26):
    yield x ^ (1 << i)

v = 0
for c in s:
  v ^= 1 << c
  dp[v] = min(dp[v],min(dp[i] for i in it(v))+1)

print(dp[v] + (v == 0))

### FIXED CODE ###

from itertools import accumulate
from operator import xor

s = tuple(map(lambda c: 1 << (ord(c)-ord('a')),input()))
L = len(s)
dp = dict()
dp[0] = 0

for v in accumulate(s, xor):
  dp[v] = min(dp.get(v,L), min(dp.get(v ^ (1 << i), L) for i in range(26))+1)

print(dp[v] + (v == 0))
### BUGGY CODE ###

a=int(input())
a_list=[]
amari=0
count=0
flag=False
while(True) :
  k=(amari*10+7)%a
  if k  in a_list :
    flag=True
    break
  elif k==0 :
    count+=1
    break
  else :
    count+=1
    amari=k
    a_list.append(amari)


if flag :
  print('-1')
else :
  print(count)

    

### FIXED CODE ###

a=int(input())
a_list=[]
amari=0
count=0
flag=False
if a%2==0 or a%5==0 :
  flag=True
else :

  while(True) :
    k=(amari*10+7)%a
    if k==0 :
      count+=1
      break
    else :
      count+=1
      amari=k



if flag :
  print('-1')
else :
  print(count)
### BUGGY CODE ###

n, k = map(int, input().split())
p = list(map(int, input().split()))
ans = 0
e = [sum(range(1, pi + 1)) / pi for pi in p]

tmp = 0
for i in range(n):
    tmp += e[i]
    if i >= k:
        tmp -= e[i - k]
    ans = max(ans, tmp)

print(ans)


### FIXED CODE ###

n, k = map(int, input().split())
p = list(map(int, input().split()))
ans = 0
e = [sum(range(1, pi + 1)) / pi for pi in p]

tmp = 0
for i in range(n):
    tmp += e[i]
    if i >= k:
        tmp -= e[i - k]
    ans = max(ans, tmp)

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

n = int(input())
a = list(map(int, input().split()))
for i in range(n):
    b = 0
    bud = 0
    for ai in a:
        if a[i] != ai:
            bud += abs(ai - b)
            b = ai
    bud += abs(b)
    print(bud)


### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
a.append(0)
a.insert(0, 0)
costs = []
for i in range(n + 1):
    costs.append(abs(a[i + 1] - a[i]))

s = sum(costs)
for i in range(n):
    print(s - costs[i] - costs[i + 1] + abs(a[i] - a[i + 2]))
### BUGGY CODE ###

s=input().split("01")
a=len(s)-1
t="".join(s)
while True:
  s=t.split("01")
  if len(s)==1:
    while True:
      s=t.split("10")
      if len(s)==1:
        break
      a+=len(s)-1
    break
  a+=len(s)-1
  t="".join(s)
print(2*a)

### FIXED CODE ###

s=input()
a=s.count("0")
b=len(s)-a
if a>=b:
  print(2*b)
else:
  print(2*a)
### BUGGY CODE ###

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

def sum_of_gcd(k):
  ans = 0
  for i in range(1, k + 1):
    for j in range(1, k + 1):
      for k in range(1, k + 1):
        ans += gcd(i, gcd(j, k))
  return ans

n = int(input())
print(sum_of_gcd(n))

### FIXED CODE ###

def gcd(a, b):
  if b > a:
    temp = b
    b = a
    a = temp
  if a % b == 0:
    return b
  else:
    return gcd(b, a % b)
def sum_of_gcd(k):
  ans = 0
  for i in range(1, k + 1):
    for j in range(1, k + 1):
      for k in range(1, k + 1):
        if i < j and j < k:
          ans += gcd(i, gcd(j, k)) * 6
        elif i < j and j == k:
          ans += gcd(i, gcd(j, k)) * 3
        elif i == j and j < k:
          ans += gcd(i, gcd(j, k)) * 3
        elif i == j and j == k:
          ans += gcd(i, gcd(j, k))
  return ans
 
n = int(input())
print(sum_of_gcd(n))
### BUGGY CODE ###

def main():
    S = input()
    S_list = []
    for i in range(len(S)):
        S_list.append(int(S[i]))
    num = 0
    for i in range(len(S_list)-3):
        sum_temp = 0
        k = 0
        for v in range(i, i+3)[::-1]:
            sum_temp += 10**k*S_list[v]
            k += 1
        for j in range(i+3, len(S)):
            #print(S_list[j])
            sum_temp *= 10
            sum_temp += S_list[j]
            if sum_temp % 2019 == 0:
                num += 1
    print(num)

if __name__ == '__main__':
    main()

### FIXED CODE ###

S = input()
N = len(S)
mod = 2019
num = [0 for i in range(2019)]
num[0] = 1
t = 1
rem = 0

for i in reversed(S):
    rem += int(i) * t
    rem %= mod
    t *= 10
    t %= mod
    num[rem] += 1

print(int(sum([i * (i-1) / 2 for i in num])))
### BUGGY CODE ###

T = input()
ans = ""
for i in T:
    if i == "?":
        ans += "D"
    else:
        ans += i
print(ans)

### FIXED CODE ###

T = input()
ans = []
for i in T:
    if i == "?":
        ans.append("D")
    else:
        ans.append(i)
print("".join(ans))
### BUGGY CODE ###

n, m = [int(item) for item in input().split()]
s = [int(item) for item in input().split()]
t = [int(item) for item in input().split()]

dp = [[0] * (n+1) for _ in range(m+1)]
for i, tt in enumerate(t):
    val = 0 
    for j, ss in enumerate(s):
        # Update Partial
        if ss == tt:
            val += dp[i][j] + 1
        # Keep prev
        dp[i+1][j+1] += dp[i][j+1] + val
        dp[i+1][j+1] %= (10**9+7)
print((dp[m][n]+1) % (10**9+7))


### FIXED CODE ###

MOD = 10**9 + 7
n, m = [int(item) for item in input().split()]
s = [int(item) for item in input().split()]
t = [int(item) for item in input().split()]

dp = [[0] * (n+1) for _ in range(m+1)]
for i, tt in enumerate(t):
    val = 0 
    for j, ss in enumerate(s):
        # Update Partial
        if ss == tt:
            val += dp[i][j] + 1
            val %= MOD
        # Keep prev
        dp[i+1][j+1] += dp[i][j+1] + val
        dp[i+1][j+1] %= MOD 
print((dp[m][n]+1) % MOD)
### BUGGY CODE ###

#Replacing
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))
Q = int(input())
asum = A.sum()
for i in range(Q):
    B, C = list(map(int, input().split()))
    CC = A==B
    NC = CC==False
    A = A*(NC) + C*CC
    asum += np.count_nonzero(CC) *(C-B)
    print(asum)
    # print(A.sum())

### FIXED CODE ###

#Replacing
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))
Q = int(input())
u, count = np.unique(A,return_counts=True)
L = np.zeros(10**5,int)
for i in range(len(u)):
    L[u[i]-1] = count[i]
asum = A.sum()
for i in range(Q):
    B, C = list(map(int, input().split()))
    asum += L[B-1] * (C-B)
    L[C-1] += L[B-1]
    L[B-1] = 0
    print(asum)
### BUGGY CODE ###

a,b,n = [int(i) for i in input().split()]
 
ans = [0 for _ in range(b)]
 
for i in range(b):
    ans[i] = (a*i // b - a * (i // b))
    
ans = ans[:n+1] if n < b else ans
 
print(max(ans))

### FIXED CODE ###

a,b,n = [int(i) for i in input().split()]
 
x = min(n,b-1)
print(a*x // b)
### BUGGY CODE ###

from operator import itemgetter

n = int(input())
lr = [list(map(int, input().split())) + [i] for i in range(n)]

l_sort = sorted(lr, key=itemgetter(0), reverse=True)
r_sort = sorted(lr, key=itemgetter(1))
#print(l_sort, r_sort)

ans = 0
used = set()
now = 0
i = 0
j = 0

if abs(l_sort[0][0]) >= abs(r_sort[0][1]):
	while used != set(range(n)):
		while l_sort[i][2] in used:
			i += 1
		if not (l_sort[i][0] <= now and now <= l_sort[i][1]):
			ans += abs(now - l_sort[i][0])
			now = l_sort[i][0]
		used.add(l_sort[i][2])
		i += 1
		#print(now)
		if used == set(range(n)):
			break
		while r_sort[j][2] in used:
			j += 1
		if not (r_sort[j][0] <= now and now <= r_sort[j][1]):
			ans += abs(now - r_sort[j][1])
			now = r_sort[j][1]
		used.add(r_sort[j][2])
		j += 1
		#print(now)

	print(ans + abs(now))

else:
	while used != set(range(n)):
		while r_sort[j][2] in used:
			j += 1
		if not (r_sort[j][0] <= now and now <= r_sort[j][1]):
			ans += abs(now - r_sort[j][1])
			now = r_sort[j][1]
		used.add(r_sort[j][2])
		j += 1
		#print(now)
		if used == set(range(n)):
			break
		while l_sort[i][2] in used:
			i += 1
		if not (l_sort[i][0] <= now and now <= l_sort[i][1]):
			ans += abs(now - l_sort[i][0])
			now = l_sort[i][0]
		used.add(l_sort[i][2])
		i += 1
		#print(now)

	print(ans + abs(now))

### FIXED CODE ###

from operator import itemgetter

n = int(input())
lr = [list(map(int, input().split())) for i in range(n)] + [[0, 0]]

l_sort = sorted(lr, key=itemgetter(0), reverse=True)
r_sort = sorted(lr, key=itemgetter(1))

ans = 0
sigma = 0

for i in range(n // 2 + 1):
	sigma += 2 * (l_sort[i][0] - r_sort[i][1])
	if sigma > ans:
		ans = sigma

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

from itertools import combinations
import sys
N = int(input())

grid = sys.stdin.read().split('\n')


res = 0
for d in range(N):
  for x,y in combinations(range(N), 2):
    if grid[(x+d)%N][y] != grid[(y+d)%N][x]:
      break
  else:
    res += 1
print(res*N)

### FIXED CODE ###

from itertools import combinations
N = int(input())
grid = tuple(tuple(input()) for _ in range(N))

res = 0
for d in range(N):
  for x,y in combinations(range(N), 2):
    if grid[(x+d)%N][y] != grid[(y+d)%N][x]:
      break
  else:
    res += 1
print(res*N)
### BUGGY CODE ###

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

check = [False for i in range(len(S))]
for i in range(len(S)-1):
    if S[i:i+2] == "AC":
        check[i] = True

output = []
for i in range(Q):
    start, end = map(int, input().split(" "))
    output.append(check[start-1:end-1].count(True))

for i in output:
    print(i)


### FIXED CODE ###

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

check = [0 for i in range(N+1)]

tmp = 0
for i in range(N):
    check[i+1] = tmp
    if S[i:i+2] == "AC":
        tmp += 1

ans = [0 for i in range(Q)]

for i in range(Q):
    l, r = map(int, input().split())
    ans[i] = check[r] - check[l]

for i in range(Q):
    print(ans[i])
### BUGGY CODE ###

N,A,B = map(int, input().split())
cnt=0
cnt_blue=0
while cnt+A+B<N:
    cnt+=A+B
    cnt_blue+=A
    
print(cnt_blue+min(A,N-cnt))

### FIXED CODE ###

N,A,B = map(int, input().split())
cnt_blue=A*(N//(A+B))
cnt_all=(A+B)*(N//(A+B))    
print(cnt_blue+min(A,N-cnt_all))
### BUGGY CODE ###

import collections 
import math
import copy

def combinations_count(n, r):
    if(n<r):
        return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

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

cnt=0
c =collections.Counter(a) #要素の数

#全体の式の導出
#今回は要素ごとの組み合わせの数

for i in c.values() :
    cnt += combinations_count(i,2)

#各要素の影響を排除して出力
for i in a:
    print(cnt- combinations_count(c[i],2) +combinations_count(c[i]-1,2))


### FIXED CODE ###

import collections 
import math

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

cnt=0
c =collections.Counter(a) #要素の数

#全体の式の導出
#今回は要素ごとの組み合わせの数

for i in c.values() :
    cnt += i*(i-1)//2

#各要素の影響を排除して出力
for i in a:
    print(cnt- c[i]*(c[i]-1)//2 + (c[i]-1)*(c[i]-2)//2)
### BUGGY CODE ###

h, w, m = map(int, input().split())
#boms = [[0,0] for i in range(m)]
bomh =[0] * m
bomw =[0] * m
Hs = [0] * h
Ws = [0] * w
for i in range(m):
  _h, _w = map(int, input().split())
  #boms[i][0] = _h - 1
  #boms[i][1] = _w - 1
  bomh[i] = _h-1
  bomw[i] = _w-1
  Hs[_h - 1] += 1
  Ws[_w - 1] += 1
Hmax = max(Hs)
Hcount = Hs.count(Hmax)
Wmax = max(Ws)
Wcount = Ws.count(Wmax)
Hindexs = [0] * Hcount
Windexs = [0] * Wcount
hcount = 0
wcount = 0
for i in range(h):
  if Hs[i] == Hmax:
    Hindexs[hcount] = i
    hcount += 1
for i in range(w):
  if Ws[i] == Wmax:
    Windexs[wcount] = i
    wcount += 1
count = 0
for i in range(m):
  if (bomh[i] in Hindexs) and (bomw[i] in Windexs):
    count += 1
# for bom in boms:
#   if (bom[0] in Hindexs) and (bom[1] in Windexs):
if count == len(Hindexs) * len(Windexs):
  print(Hmax + Wmax - 1)
else:
  print(Hmax + Wmax)

### FIXED CODE ###

h, w, m = map(int, input().split())
boms = set()
Hs = [0] * h
Ws = [0] * w
for i in range(m):
  _h, _w = map(int, input().split())
  boms.add((_h - 1, _w - 1))
  Hs[_h - 1] += 1
  Ws[_w - 1] += 1
Hmax = max(Hs)
Hcount = Hs.count(Hmax)
Wmax = max(Ws)
Wcount = Ws.count(Wmax)
Hindexs = [0] * Hcount
Windexs = [0] * Wcount
hcount = 0
wcount = 0
for i in range(h):
  if Hs[i] == Hmax:
    Hindexs[hcount] = i
    hcount += 1
for i in range(w):
  if Ws[i] == Wmax:
    Windexs[wcount] = i
    wcount += 1
if m < len(Hindexs) * len(Windexs):
  print(Hmax + Wmax)
  exit()
for hindex in Hindexs:
  for windex in Windexs:
    if (hindex,windex) not in boms: 
      print(Hmax + Wmax)
      exit()
print(Hmax + Wmax -1)
### BUGGY CODE ###

n,a,b=map(int,input().split())
x,y=0,0
i=0
while(n>0):
    if(i%2==0):
        x+=min(n,a)
        n-=min(n,a)
    else:
        y+=min(b,n)
        n-=min(n,b)
    i+=1
print(x)


### FIXED CODE ###

n,a,b=map(int,input().split())
x=n//(a+b)
y=n%(a+b)
if(y>=a):
    print(x*a+a)
else:
    print(a*x+y)
### BUGGY CODE ###

n=input()
s=[]
for i in range(n):
	s+=[raw_input()]
result=0
for shift in range(n):
	ok=True
	for i in range(n):
		for j in range(n):
			if s[i][j]!=s[j][i]:
				ok=False
	if ok:
		result+=n
	for i in range(n):
		s[i]=s[i][1:]+s[i][0:1]
print result

### FIXED CODE ###

n=input()
s=[raw_input() for i in range(n)]
t=[]
for i in range(n):
	t+=[s[j][i] for j in xrange(n)]*2
s+=s
s=''.join(s)
#print s
t=''.join(t)
#print t
result=0
for shift in xrange(n):
	ok=True
	for i in xrange(n):
		if s[(i+shift)*n: (i+shift)*n+n]!=t[i*n*2+shift: i*n*2+shift+n]:
			ok=False
			break
	if ok:
		result+=n
print result
### BUGGY CODE ###

N = int(input())
As = list(map(int, input().split()))
r = 0
#for i in range(N):
#  for j in range(i+1, N):
#    r = (r+(As[i]^As[j]))%1000000007
for i in range(64):
  n = 0
  for A in As:
    if A&(1<<i) != 0:
      n += 1
  #print(i, n)
  d = n*(N-n)%1000000007
  d2 = (d<<i)%1000000007
  r = (r+d2)%1000000007
print(r)


### FIXED CODE ###

N = int(input())
As = list(map(int, input().split()))
r = 0
#for i in range(N):
#  for j in range(i+1, N):
#    r = (r+(As[i]^As[j]))%1000000007
for i in range(64):
  n = 0
  for A in As:
    if A&(1<<i) != 0:
      n += 1
  #print(i, n)
  d = n*(N-n)%1000000007
  d2 = (d<<i)%1000000007
  r = (r+d2)%1000000007
print(r)
### BUGGY CODE ###

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

c=[]
for _ in range(N):
  c.append([ x for x in map(int,input().split(' '))])

x=float('inf')

for seq in [[0,1,2],[0,2,1],[1,0,2],[1,2,0],[2,0,1],[2,1,0]]:
  counter=[[0]*C for _ in range(3)]
  for i in range(N):
    for j in range(N):
      b=i%3
      cur=c[i][j]
      counter[seq[(j+b)%3]][cur-1]+=1
      #s+=D[cur-1][seq[(j+b)%3]]
  for c0 in range(C):
    for c1 in range(C):
      if c1==c0:
        continue
      for c2 in range(C):
        if c2==c0 or c2==c1:
          continue
        s=0
        for i, cx in enumerate(counter[0]):
          s+=D[i][c0]*cx
        for i, cx in enumerate(counter[1]):
          s+=D[i][c1]*cx
        for i, cx in enumerate(counter[2]):
          s+=D[i][c2]*cx
        if s<x:
          x=s

print(x)



### FIXED CODE ###

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

c=[]
for _ in range(N):
  c.append([ x for x in map(int,input().split(' '))])

x=float('inf')

memo={}

counter=[[0]*C for _ in range(3)]
for i in range(N):
  for j in range(N):
    b=i%3
    cur=c[i][j]
    counter[(j+b)%3][cur-1]+=1
    #s+=D[cur-1][seq[(j+b)%3]]

for c0 in range(C):
  for c1 in range(C):
    if c1==c0:
      continue
    for c2 in range(C):
      if c2==c0 or c2==c1:
        continue
      s=0
      for i, cx in enumerate(counter[0]):
        s+=D[i][c0]*cx
      for i, cx in enumerate(counter[1]):
        s+=D[i][c1]*cx
      for i, cx in enumerate(counter[2]):
        s+=D[i][c2]*cx
      if s<x:
        x=s


print(x)
### BUGGY CODE ###

r=[0]*3
r[0],r[1],r[2],n=map(int, input().split())
ct=0
r=sorted(r)

for i in range(n//r[2]+1):
    for j in range(n//r[1]+1):
        if (n-(i*r[2]+j*r[1]))%r[0]==0 and 0<=(n-(i*r[2]+j*r[1]))<=n+1:
            ct+=1
print(ct)
    

### FIXED CODE ###

r=[0]*3
r[0],r[1],r[2],n=map(int, input().split())
ct=0
r=sorted(r)

for i in range(n//r[2]+1):
    for j in range(min(n//r[1]+1, n-r[2]*i+1)):
        if (n-(i*r[2]+j*r[1]))%r[0]==0 and 0<=(n-(i*r[2]+j*r[1]))<=n+1:
            ct+=1
print(ct)
### BUGGY CODE ###

H,W,M = map(int,input().split())
P = [0]*(H+1)
Q = [0]*(W+1)
A = set()
for k in range(M):
  a,b = map(int,input().split())
  P[a] += 1
  Q[b] += 1
  A.add((a,b))
Pindex = [i for i, v in enumerate(P) if v == max(P)]
Qindex = [i for i, v in enumerate(Q) if v == max(Q)]
Pmax = P[Pindex[0]]
Qmax = Q[Qindex[0]]
ans = Pmax+Qmax-1
key = 0
for x in Pindex:
  for y in Qindex:
    if (x,y) not in A:
      ans += 1
      key = 1
      break
  if key == 1:
    break
print(ans)


### FIXED CODE ###

H,W,M = map(int,input().split())
P = [0]*(H+1)
Q = [0]*(W+1)
A = set()
for k in range(M):
    a,b = map(int,input().split())
    P[a] += 1
    Q[b] += 1
    A.add((a,b))
Pmax = max(P)
Qmax = max(Q)
Pindex = []
Qindex = []
for i in range(H+1):
    if P[i] == Pmax:
        Pindex.append(i)
for i in range(W+1):
    if Q[i] == Qmax:
        Qindex.append(i)
ans = Pmax+Qmax-1
key = 0
for x in Pindex:
  for y in Qindex:
    if (x,y) not in A:
      ans += 1
      key = 1
      break
  if key == 1:
    break
print(ans)