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

s = list(input())
Q = int(input())
q = []
f = []
c = []

for _ in range(Q):
    tmp = list(map(str,input().split()))
    if len(tmp) == 1:
        q.append(int(tmp[0]))
        f.append(0)
        c.append('')
    else:
        q.append(int(tmp[0]))
        f.append(int(tmp[1]))
        c.append(list(tmp[2]))

s2 = list(reversed(s))
for i in range(Q):
    if q[i] == 1:
        tmp = s
        s = s2
        s2 = tmp
    else:
        if f[i] == 1:
            s2.append(c[i][0])
            c[i].extend(s)
            s = c[i]
        else:
            s.append(c[i][0])
            c[i].extend(s2)
            s2 = c[i]

print("".join(s))


### FIXED CODE ###

from collections import deque

s = list(input())
s2 = deque(list(reversed(s)))
s = deque(s)
Q = int(input())
q = []
f = []
c = []
count = 0

for _ in range(Q):
    tmp = list(map(str,input().split()))
    if len(tmp) == 1:
        count += 1
        q.append(int(tmp[0]))
        f.append(0)
        c.append('')
    else:
        q.append(int(tmp[0]))
        f.append(int(tmp[1]))
        c.append(tmp[2])


for i in range(Q):
    if q[i] == 1:
        tmp = s
        s = s2
        s2 = tmp
    else:
        if f[i] == 1:
            s.appendleft(c[i])
            s2.append(c[i])
        else:
            s.append(c[i])
            s2.appendleft(c[i])

for i in range(len(s)):
    print(s.popleft(),end="")
print("")
### BUGGY CODE ###

S = input()
Q = int(input())
num_rev = 0

for i in range(Q):
  inp = input()
  if len(inp) == 1:
    num_rev += 1
  else:
    T, F, C = inp.split()
    if ((num_rev % 2 == 0) and int(F) == 1) or ((num_rev % 2 == 1) and int(F) == 2):
      S = C + S
    else:
      S = S + C

if num_rev % 2 == 0:
  print(S)
else:
  print(S[::-1])


### FIXED CODE ###

S = input()
Q = int(input())
num_rev = 0
S_front = [""] * Q
S_back = [""] * Q
 
for i in range(Q):
  inp = input()
  if len(inp) == 1:
    num_rev += 1
  else:
    T, F, C = inp.split()
    if ((num_rev % 2 == 0) and int(F) == 1) or ((num_rev % 2 == 1) and int(F) == 2):
      S_front[i] = C
    else:
      S_back[i] = C

S = "".join(S_front)[::-1] + S + "".join(S_back)
if num_rev % 2 == 0:
  print(S)
else:
  print(S[::-1])
### BUGGY CODE ###

import numpy as np
n = int(input())
a = np.array([int(x) for x in input().split()])
count = 0
for i in a:
    a_i = i%a
    if np.count_nonzero(a_i == 0)==1:
        count+=1

print(count)

### FIXED CODE ###

import numpy as np
n = int(input())
a = np.sort(np.array(list(map(int, input().split()))))
max_num = a[-1]+1
dp = np.array([0]*(max_num))

if n == 1:
    print(1)
elif np.all(a==a[0]):
    print(0)
elif np.count_nonzero(a==1)  == 1:
    print(1)
else:
    for i in a:
        for j in np.arange(i, max_num, i):
            dp[j] += 1
    print(np.count_nonzero(dp[a] == 1))
### BUGGY CODE ###

h, w, m = map(int, input().split())
boms = []
Hs = [0] * h
Ws = [0] * w
for i in range(m):
  _h, _w = map(int, input().split())
  boms.append((_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
count = 0
for bom in boms:
  if (bom[0] in Hindexs) and (bom[1] in Windexs):
    count += 1
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 ###

from scipy.sparse.csgraph import dijkstra

n = int(input())
dists = []
for i in range(n):
    ds = map(int, input().split())
    for j, c in enumerate(ds):
        if i >= j:
            continue
        dists.append((c, i, j))
dists.sort()

links = [[0] * n for _ in range(n)]
ans = 0

for c, i, j in dists:
    graph = dijkstra(links, directed=False, indices=i)
    if graph[j] < c:
        break
    elif graph[j] > c:
        links[i][j] = c
        links[j][i] = c
        ans += c

graph = dijkstra(links, directed=False)
for c, i, j in dists:
    if graph[i][j] != c:
        print(-1)
        break
else:
    print(ans)


### FIXED CODE ###

def solve(n, tbl):
    ans = 0
    for i, row_i in enumerate(tbl):
        for j in range(i + 1, n):
            row_j = tbl[j]
            ij = row_i[j]
            mind = min((ik + kj for ik, kj in zip(row_i, row_j) if ik and kj), default=float('inf'))
            if ij > mind:
                return -1
            if ij < mind:
                ans += ij
    return ans


n = int(input())
tbl = [list(map(int, input().split())) for _ in range(n)]
print(solve(n, tbl))
### BUGGY CODE ###

n = int(input())
v = list(map(int,input().split()))
c = list(map(int,input().split()))
ans = -1e9
for i in range(2**n):
    conv = str(bin(i))[2:].zfill(n)
    val = 0
    for j in range(n):
        if conv[j] == "1":
            val += v[j] - c[j]
    ans = max(ans, val)
print(ans)


### FIXED CODE ###

n = int(input())
v = list(map(int,input().split()))
c = list(map(int,input().split()))
p = [v[i]-c[i] for i in range(n)]
ans = 0
for i in range(n):
    if p[i] > 0:
        ans += p[i]
print(ans)
### BUGGY CODE ###

from operator import mul
from functools import reduce

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under

n = int(input())
list_A = list(map(int, input().split()))
max_value = max(list_A)
ans = 0
a = 0
for i in list_A:
    c = cmb(max_value, i)
    ans = max(ans, c)
    if ans == c:
        a = i

print(max_value, a)

### FIXED CODE ###

n = int(input())
list_A = list(map(int, input().split()))
list_A.sort()
max_value = max(list_A)
a = max_value/2
b = pow(10, 10)
c = 0
for num in list_A:
    if abs(a-num) < b:
        b = abs(a-num)
        c = num

print(max_value, c)
### BUGGY CODE ###

MOD = 10**9+7
k = int(input())
s = input()
n = len(s)


def com(n, r):
    X = Y = 1
    if r < 0 or n < r:
        return 0
    if n-r < r:
        r = n-r
    for i in range(1, r+1):
        Y = Y*i % MOD
        X = X*(n-i+1) % MOD
    Y = pow(Y, MOD-2, MOD)
    return X*Y % MOD


ans = 0
for x in range(k+1):
    ans += pow(26, x, MOD)*com(n+k-x-1, n-1)*pow(25, k-x, MOD)
    ans %= MOD
print(ans)


### FIXED CODE ###

MOD = 10**9+7
k = int(input())
s = input()
n = len(s)


U = n+k
fact = [0]*(U+1)
fact[0] = 1
for i in range(1, U+1):
    fact[i] = fact[i-1]*i % MOD


invfact = [0]*(U+1)
invfact[U] = pow(fact[U], MOD-2, MOD)
for i in reversed(range(U)):
    invfact[i] = invfact[i+1]*(i+1) % MOD


def nCr(n, r):
    if r < 0 or n < r:
        return 0
    return fact[n]*invfact[r]*invfact[n-r]


ans = 0
for x in range(k+1):
    ans += pow(26, x, MOD)*nCr(n+k-x-1, n-1)*pow(25, k-x, MOD)
    ans %= MOD
print(ans)
### BUGGY CODE ###

n, k = list(map(int, input().split()))
h = list(map(int, input().split()))
f = [1 << 31] * n 

f[0] = 0
for i in range(0, n):
    lim = max(i - k, 0)
    for j in range(lim, i):
        f[i] = min(f[i], abs(h[j] - h[i]) + f[j])

print(f[n - 1])

### FIXED CODE ###

n, k = list(map(int, input().split()))
h = list(map(int, input().split()))
f = [1 << 31] * n 

f[0] = 0
for i in range(0, n):
    lim = max(i - k, 0)
    for j in range(lim, i):
        f[i] = min(f[i], abs(h[j] - h[i]) + f[j])

print(f[n - 1])
### BUGGY CODE ###

'''
B - RGB Boxes
'''

r_weight, g_weight, b_weight, N = map(int, input().split())

ans = 0
for r in range(N+1):
    if r*r_weight > N:
        break
    for g in range(N-r*r_weight+1):
        if r*r_weight + g*g_weight > N:
            break
        b = (N - r*r_weight - g*g_weight) // b_weight
        if r*r_weight + g*g_weight + b*b_weight == N and b >= 0:
            ans += 1

print(ans)


### FIXED CODE ###

'''
B - RGB Boxes
'''

r_weight, g_weight, b_weight, N = map(int, input().split())

ans = 0
for r in range(N+1):
    for g in range(N-r*r_weight+1):
        rem = N - r*r_weight - g*g_weight
        if rem % b_weight == 0 and rem >= 0:
            ans += 1

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

N = int(input())
A = list(map(int, input().split()))
sum = 0
# temp1 = 0
# temp2 = 0
MOD = 1000000007
for i in range(0, N-1):
    for j in range(i+1, N):
        #print(i, j)
        temp1 = 0
        temp2 = 0
        temp3 = 0
        temp1 = A[i] % MOD
        temp2 = A[j] % MOD
        temp3 = temp1 * temp2
        sum = sum + temp3
sum = sum % MOD
print(sum)

### FIXED CODE ###

MOD = 1000000007
N = int(input())
A = list(map(int, input().split()))
ans = 0
B = [0]*(N-1)
sumA = sum(A)
for i in range(1, N):
    sumA -= A[i-1]
    B[i-1] = sumA
for i in range(0, N-1):
    ans += A[i] * B[i]
    ans %= MOD
print(ans)
### BUGGY CODE ###

import sys
# input = sys.stdin.buffer.readline


N = int(input())
A = [list(map(int, input().split()))[::-1] for _ in range(N)]
day = 0
cnt = N
Last = [True] * N
while cnt:
    flag = True
    Done = [False] * N
    for i, a in enumerate(A):
        if not Last[i]:
            continue
        if len(a) == 0:
            continue
        if Done[i]:
            continue
        x = a[-1] - 1
        if A[x] and (not Done[x]) and (A[x][-1] - 1 == i):
            A[i].pop()
            A[x].pop()
            Done[i] = True
            Done[x] = True
            flag = False
            if not A[i]:
                cnt -= 1
            if not A[x]:
                cnt -= 1
    Last = Done
    if flag:
        break
    day += 1
if cnt:
    print(-1)
else:
    print(day)

### FIXED CODE ###

import sys
# input = sys.stdin.buffer.readline


N = int(input())
A = [list(map(int, input().split()))[::-1] for _ in range(N)]
day = 0
cnt = N
Last = set([i for i in range(N)])
while cnt:
    flag = True
    Done = [False] * N
    L = set()
    for i in Last:
        if len(A[i]) == 0:
            continue
        if Done[i]:
            continue
        x = A[i][-1] - 1
        if A[x] and (not Done[x]) and (A[x][-1] - 1 == i):
            A[i].pop()
            A[x].pop()
            Done[i] = True
            Done[x] = True
            L.add(i)
            L.add(x)
            flag = False
            if not A[i]:
                cnt -= 1
            if not A[x]:
                cnt -= 1
    Last = L
    if flag:
        break
    day += 1
if cnt:
    print(-1)
else:
    print(day)
### BUGGY CODE ###

N = int(input())
s = ['' for i in range(N)]
for i in range(N):
  s[i]=input()

lsts=list(set(s))
lrl=list(range(len(lsts)))
n = [0 for i in lrl]
for i in lrl:
  n[i]=s.count(lsts[i])
  
w=[lsts[i] for i in lrl if n[i]==max(n)]
w.sort()
for wd in w:
  print(wd)

### FIXED CODE ###

N = int(input())
dc={}
for i in range(N):
  s=input()
  if s in dc:
    dc[s]+=1
  else:
    dc[s]=1
m=max(dc.values())
w = [kv[0] for kv in dc.items() if kv[1] == m]
w.sort()
for p in w:
  print(p)
### BUGGY CODE ###

def main():
    N = int(input())
    A = list(map(int,input().split()))
    MOD = 10**9 +7

    S = 0
    for i in range(len(A)-1):
        tmp = 0
        for j in range(i+1,len(A)):
            tmp+=A[j]
        S+= (tmp%MOD * A[i])%MOD


    print(S%MOD)

main()


### FIXED CODE ###

def main():
    N = int(input())
    A = list(map(int,input().split()))
    MOD = 10**9 +7

    S = 0
    for i in range(len(A)-1):
        tmp = 0
        for j in range(i+1,len(A)):
            tmp+=A[j]
        S+= (tmp%MOD * A[i])%MOD


    print(S%MOD)



from itertools import accumulate

m = 10**9 + 7

N,*A = map(int,open(0).read().split())

result = 0

b = [0]*N
b[0] = A[0]
for i in range(1,N):
    b[i]= b[i-1] + A[i]




for i in range (N-1):
    result += A[i]*(b[N-1]-b[i])
    result %= m
print(result)
### BUGGY CODE ###

n = int(input())
adjacency_list = [list(map(int, input().split())) for i in range(n)]
egde = []
for u in range(n):
    for v in range(u):
        egde.append([adjacency_list[u][v], u, v])
egde.sort()

ans = 0
for d, u, v in egde:
    for w in range(n):
        if w == u or w == v:
            continue
        if d > adjacency_list[u][w] + adjacency_list[w][v]:
            print(-1)
            exit()
        elif d == adjacency_list[u][w] + adjacency_list[w][v]:
            break
    else:
        ans += d

print(ans)

### FIXED CODE ###

import sys
import numpy as np
input = sys.stdin.readline
n = int(input())
G = np.array([list(map(int, input().split())) for _ in range(n)], dtype=float)
use = np.ones((n,n),dtype=bool)
impossible = np.zeros((n,n),dtype=bool)
for w in range(n):
    D = sum(np.meshgrid(G[w], G[w]))
    D[w] = np.inf
    D[:,w] = np.inf
    use &= ~(G == D)
    impossible |= G > D
if impossible.any():
    print(-1)
else:
    print(int(np.sum(G[use]) / 2))
### BUGGY CODE ###


n=int(input())
s=input()
r=[]
g=[]
b=[]
for i in range(n):
    if s[i]=="R":
        r.append(i+1)
    elif s[i]=="G":
        g.append(i+1)
    else:
        b.append(i+1)

sum=0
if len(r)*len(g)*len(b)==0:
    print(0)
else:
    for re in r:
        for gr in g:
            cnt=0
            re1=re
            re1,gr=min(re,gr),max(re,gr)
            #print(re,gr,b)
            if (re1+gr)%2==0:
                if (re1+gr)//2 in b:
                    cnt-=1
            if 2*gr-re1 in b:
                cnt-=1
            if 2*re1-gr in b:
                cnt-=1
            sum+=cnt+len(b)
            #print(cnt)
    print(sum)


### FIXED CODE ###

def ri(): return int(input())
def rli(): return list(map(int, input().split()))
def rls(): return list(map(str, input().split()))
def pl(a): print(" ".join(list(map(str, a))))
def ma():return map(int,input().split())
"""
k=ri()

n,k=ma()
for i in range(n):
    for

n=int(input())
s=input()
r=[0]*n
b=[0]*n
g=[0]*n

for i in range(n):
    if s[i]=="R":
"""

"""
from itertools import accumulate as acc
import operator as op
import copy as cp
import math; import numpy
from bisect import bisect_left as bisl
from bisect import bisect_right as bisr
###
"""



"""
import math
k=int(input())

cnt=0
for a in range(1,k+1):
    for b in range(1,k+1):
        ct=math.gcd(a,b)
        for c in range(1,k+1):
            cnt+=math.gcd(ct,c)

print(cnt)
"""
"""
n,k=map(int,input().split())
mod=10**9+7

import math
dj0=[[i for i in range(1,k+1)] for j in range(k)]
dj1=[i for i in range(1,k+1)]
dj2=[0]*k
for i in dj1:
    for j in range(1,k):
        dj0[i-1][j-1]=math.gcd(i,j)
        dj2[math.gcd(i,j)]+=1
"""
n=ri()
s=input()
ans = s.count("R")*s.count("G")*s.count("B")
for i in range(n):
    for j in range(i+1,n):
        k=2*j-i
        if k>=n:
            break
        ans-=s[k]!=s[i]!=s[j]!=s[k]

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


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

def gcd(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        return a
    c = a % b
    return gcd(b, c)
 
def lcm(x, y):
    return (x * y) // gcd(x, y)

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

success = False
ans = 0

_lcm = lcm(N, M)
for k in range(1, N*M//_lcm + 1):
    ans = k * _lcm
    s = [(i * ans)//N + 1 for i in range(len(S))]
    t = [(i * ans)//M + 1 for i in range(len(T))]

    pool = s + t

    double_st = [x for x in set(pool) if pool.count(x) > 1]
    isImpossible = False

    for st in double_st:
        s_i = my_index(s, st, -1)
        t_i = my_index(t, st, -1)
        if s_i == -1 or t_i == -1 or s_i >= len(S) or t_i >= len(T):
            continue
        if S[s_i] != T[t_i]:
            isImpossible = True
            
    if not isImpossible:
        success = True
        break

if not success:
    print(-1)
else:
    print(ans)

### FIXED CODE ###


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

def gcd(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        return a
    c = a % b
    return gcd(b, c)
 
def lcm(x, y):
    return (x * y) // gcd(x, y)

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

success = False
fans = 0

_lcm = lcm(len(S), len(T))

def isImpble(k, _lcm):
    global success
    global S
    global T
    global fans
    
    ans = k * _lcm
    double_st = []
    
    for i in range(len(S)):
        if (len(T)*i)%len(S) == 0 and (len(T) * i)//len(S) <len(T):
            double_st += [(i * ans)//len(S) + 1]
    isImpossible = False

    for st in double_st:
        s_i = ((st-1) * len(S))//ans
        t_i = ((st-1) * len(T))//ans
        if S[s_i] != T[t_i]:
            isImpossible = True
            break

            
    if isImpossible:
        return True
    else:
        success = True
        fans = k*_lcm
        return False
        

imp = isImpble((len(S) * len(T))//_lcm, _lcm)

if not imp:
    for k in range(1, (len(S) * len(T))//_lcm ):
        imp = isImpble(k, _lcm)
        if success:
            break
    

if not success:
    print(-1)
else:
    print(fans)
### BUGGY CODE ###

def mygets():
    import sys
    return list(map(int, sys.stdin.readline().split()))

n, x = mygets()
a = mygets()

b1 = a.copy()
b2 = a.copy()

ans = sum(a)

for i in range(1, n):
    for j, v in enumerate(a):
        b2[j] = min(v, b1[(j + n - 1) % n])
    ans = min(ans, sum(b2) + i * x)
    b1, b2 = b2, b1

print(ans)

### FIXED CODE ###

def mygets():
    return list(map(int, input().strip().split(' ')))

n, x = mygets()
a = mygets()

b1 = a

ans = sum(a)

def f(k, u):
    j = k - 1
    if j < 0:
        j = n - 1
    w = b1[j]
    if u < w:
        return u
    return w

for i in range(1, n):
    b2 = [f(k, u) for k, u in enumerate(a)]
    ans = min(ans, sum(b2) + i * x)
    b1 = b2

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

import sys
input = sys.stdin.readline

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


#Segment tree（区間の最大値を求めるバージョン）
for i in range(30):#要素数以上の2のベキを見つける
    if N<=2**i:
        seg_el=2**i#Segment treeの台の要素数
        break

SEG=[0]*(2*seg_el-1)#Segment treeを一次元リストとして作る

def update(n,x,seg_el):#A[n]=xに更新（反映）
    i=n+seg_el-1
    while i>=0:
        SEG[i]=max(x,SEG[i])
        i=(i-1)//2

def getvalues(a,b,k,l,r):#区間[a,b)での,SEG[k],つまり区間[l,r)に関する最大値を調べる
    if r<=a or b<=l:#区間[a,b)が対象区間の外にあれば,infを出力
        return -float("inf")
    if a<=l and r<=b:#区間[a,b)が対象区間の中にあればSEG[k]を出力
        return SEG[k]
    vl=getvalues(a,b,k*2+1,l,(l+r)//2)#それ以外のときは,SEG[k*2+1]とSEG[k*2+2]で場合分け（木の子ノード二つを見る）
    vr=getvalues(a,b,k*2+2,(l+r)//2,r)
    return max(vl,vr)

for i in range(N):
    h,a=H[i],A[i]
    x=getvalues(0,h,0,0,seg_el)
    update(h-1,x+a,seg_el)

print(getvalues(0,N,0,0,seg_el))


### FIXED CODE ###

import sys
input = sys.stdin.readline

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


#Segment tree（区間の最大値を求めるバージョン）
for i in range(30):#要素数以上の2のベキを見つける
    if N<=2**i:
        seg_el=2**i#Segment treeの台の要素数
        break

SEG=[0]*(2*seg_el-1)#Segment treeを一次元リストとして作る

def update(n,x,seg_el):#A[n]=xに更新（反映）
    i=n+seg_el-1
    while i>=0:
        SEG[i]=max(x,SEG[i])
        i=(i-1)//2

def getvalues(a,b,k,l,r):#区間[a,b)での,SEG[k],つまり区間[l,r)に関する最大値を調べる
    if r<=a or b<=l:#区間[a,b)が対象区間の外にあれば,infを出力
        return -float("inf")
    if a<=l and r<=b:#区間[a,b)が対象区間の中にあればSEG[k]を出力
        return SEG[k]
    vl=getvalues(a,b,k*2+1,l,(l+r)//2)#それ以外のときは,SEG[k*2+1]とSEG[k*2+2]で場合分け（木の子ノード二つを見る）
    vr=getvalues(a,b,k*2+2,(l+r)//2,r)
    return max(vl,vr)

for i in range(N):
    h,a=H[i],A[i]
    x=getvalues(0,h,0,0,seg_el)
    update(h-1,x+a,seg_el)

print(getvalues(0,N,0,0,seg_el))
### BUGGY CODE ###

n = int(input())
s = [input() for _ in range(n)]
result = 0
for i in range(n):
    a = 1
    for x in range(n):
        if any(s[(x+i)%n][y] != s[(y+i)%n][x] for y in range(x, n)):
            a = 0
            break
    result += a
print(result*n)

### FIXED CODE ###

n = int(input())
s = [input() for _ in range(n)]
result = 0
for i in range(n):
    h = True
    for x in range(n):
        if all(s[(x+i)%n][y]==s[(y+i)%n][x] for y in range(x, n)):
            continue
        else:
            h = False
            break
    if h:
        result += 1
print(result*n)
### BUGGY CODE ###

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


def pow_k(x, n):
    """
    O(log n)
    """
    if n == 0:
        return 1

    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x
        x *= x
        n //= 2

    return K * x


total = pow_k(2, n) - 1

now = 1
div = 1
ans = 0
for i in range(max(a, b) + 1):
    now *= (n - i)
    div *= i + 1
    if i == a-1 or i == b-1:
        total -= now // div
print(total % (10 ** 9 + 7))


### FIXED CODE ###

n, a, b = map(int, input().split())
MOD = 10 ** 9 + 7
# Pythonのpowは繰り返し二乗法で実装されている
ans = pow(2, n, MOD) - 1
product = 1
div = 1
for i in range(1, b + 1):
    product = product * (n - i + 1) % MOD
    div = div * i % MOD
    if i == a or i == b:
        # 逆元を求める
        ans -= product * pow(div, MOD - 2, MOD)
print(ans % MOD)
### BUGGY CODE ###

import itertools
import os
import sys

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

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


H, W = list(map(int, sys.stdin.buffer.readline().split()))
A = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(H)]
B = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(H)]

grid = [[0] * W for _ in range(H)]
for h, w in itertools.product(range(H), range(W)):
    grid[h][w] = A[h][w] - B[h][w]

# 符号をどっちにするか選べる
dp = [[set() for _ in range(W)] for _ in range(H)]
dp[0][0] = {grid[0][0], -grid[0][0]}
for h, w in itertools.product(range(H), range(W)):
    g = grid[h][w]
    s = dp[h][w]
    limit = (H - h + 1) * (W - w + 1) * 80
    if h - 1 >= 0:
        for c in dp[h - 1][w]:
            if abs(c + g) <= limit:
                s.add(abs(c + g))
            if abs(c - g) <= limit:
                s.add(abs(c - g))
    if w - 1 >= 0:
        for c in dp[h][w - 1]:
            if abs(c + g) <= limit:
                s.add(abs(c + g))
            if abs(c - g) <= limit:
                s.add(abs(c - g))

ans = INF
for s in dp[-1][-1]:
    ans = min(ans, abs(s))

print(ans)


### FIXED CODE ###

import itertools
import os
import sys

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

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


H, W = list(map(int, sys.stdin.buffer.readline().split()))
A = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(H)]
B = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(H)]

grid = [[0] * W for _ in range(H)]
for h, w in itertools.product(range(H), range(W)):
    grid[h][w] = A[h][w] - B[h][w]

# 符号をどっちにするか選べる
dp = [[set() for _ in range(W)] for _ in range(H)]
dp[0][0] = {grid[0][0] % 800, -(grid[0][0]) % 800}
for h, w in itertools.product(range(H), range(W)):
    g = grid[h][w]
    s = dp[h][w]
    if h - 1 >= 0:
        for c in dp[h - 1][w]:
            s.add((c + g) % 800)
            s.add((c - g) % 800)
    if w - 1 >= 0:
        for c in dp[h][w - 1]:
            s.add((c + g) % 800)
            s.add((c - g) % 800)
# print(grid)
# print(dp)
ans = INF
for s in dp[-1][-1]:
    ans = min(ans, s)
    ans = min(ans, (-s) % 800)
print(ans)