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

def dijkstra(s,n,cost,l):
    d = [float("inf")] * n
    used = [False] * n
    d[s] = 0
    
    while True:
        v = -1
        for i in range(n):
            if (not used[i]) and (v == -1):
                v = i
            elif (not used[i]) and d[i] < d[v]:
                v = i
        if v == -1:
            break
        used[v] = True
        dist_v = d[v]
        for j in range(n):
            weight = cost[v][j]
            alt = dist_v + weight
            if (dist_v - 1) // l != (alt - 1) // l and dist_v != 0:
                alt = ((dist_v - 1) // l + 1) * l + weight         
            d[j] = min(d[j],alt)
    return d

n,m,l = map(int, input().split())
path = [[float("inf") for i in range(n)] for i in range(n)]
ans_list = []
for i in range(m):
  a,b,cost = map(int, input().split())
  a -= 1
  b -= 1
  if cost <= l:
    path[a][b] = cost
    path[b][a] = cost
for s in range(n):
  ans_list.append(dijkstra(s,n,path,l))
q = int(input())

for i in range(q):
  s,t = map(int, input().split())
  ans = ans_list[s-1][t-1]
  if ans == float("inf"):
    ans = -1
  else:
    ans = (ans - 1) // l
  print(ans)


### FIXED CODE ###

import sys
input = sys.stdin.readline

def dijkstra(s,n,cost,l):
    d = [float("inf")] * n
    used = [False] * n
    d[s] = 0
    
    while True:
        v = -1
        for i in range(n):
            if (not used[i]) and (v == -1):
                v = i
            elif (not used[i]) and d[i] < d[v]:
                v = i
        if v == -1:
            break
        used[v] = True
        dist_v = d[v]
        for j, weight in enumerate(cost[v]):
            alt = dist_v + weight
            r = (dist_v - 1) // l
            if r != ((alt - 1) // l) and dist_v != 0:
                alt = (r + 1) * l + weight         
            d[j] = min(d[j],alt)
    return d

n,m,l = map(int, input().split())
path = [[float("inf") for i in range(n)] for i in range(n)]
ans_list = []
for i in range(m):
  a,b,cost = map(int, input().split())
  a -= 1
  b -= 1
  if cost <= l:
    path[a][b] = cost
    path[b][a] = cost
for s in range(n):
  ans_list.append(dijkstra(s,n,path,l))
q = int(input())

for i in range(q):
  s,t = map(int, input().split())
  ans = ans_list[s-1][t-1]
  if ans == float("inf"):
    ans = -1
  else:
    ans = (ans - 1) // l
  print(ans)
### BUGGY CODE ###

import numpy as np
import sys
input = sys.stdin.readline

def draft(A, K, N):
    A = np.array(A)
    K = min(K, 20)
    for _ in range(1, K+1):# 2 * 10**5
        tmp = np.zeros(N, dtype=np.int64)
        for i in range(N):# 2 * 10**5
            L = i - A[i]
            R = i + A[i] + 1
            if L < 0:
                tmp[0] += 1
            else:
                tmp[L] += 1
            if R < N:
                tmp[R] -= 1
        A = np.cumsum(tmp)
    for i in range(N):
        print(int(A[i]), end=" ")

def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    draft(A, K, N)

if __name__ == "__main__":
    main()

### FIXED CODE ###

def draft(A, K, N):
    for _ in range(1, K+1):# 2 * 10**5
        tmp = [0]*(N+1)
        for i in range(N):# 2 * 10**5
            L = max(0, i - A[i])
            R = min(N, i + A[i]+1)
            tmp[L] += 1
            tmp[R] -= 1
        f = True
        for i in range(N):
            tmp[i+1] += tmp[i]
            A[i] = tmp[i]
            if A[i] != N:
                f = False
        if f:
            break
    for i in range(N):
        print(A[i], end=" ")

def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    draft(A, K, N)

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

import sys
input = sys.stdin.readline

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

dp = [[None for _ in range(N)] for _ in range(N)]
for i in range(N):
    dp[0][i] = A[i]

ans = sum(dp[0])
for d in range(1, N):
    for i in range(N):
        dp[d][i] = min(dp[d-1][i], A[(i+d)%N])
    ans = min(ans, sum(dp[d])+X*d)

print(ans)

### FIXED CODE ###

import sys
input = sys.stdin.readline

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

dp = [[None for _ in range(N)] for _ in range(N)]
for i in range(N):
    dp[0][i] = A[i]

ans = sum(dp[0])
for d in range(1, N):
    for i in range(N):
        dp[d][i] = min(dp[d-1][i], A[(i+d)%N])
    ans = min(ans, sum(dp[d])+X*d)

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

import sys
import itertools
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

N = I()
# combs = list(itertools.combinations_with_replacement(range(1,int(N**0.5)), 3))
combs = list(itertools.product(range(1,int(N**0.5)), repeat=3))

sum = []
for comb in combs:
    temp = comb[0]**2 + comb[1]**2 + comb[2]**2+ comb[0]*comb[1]+ comb[1]*comb[2] + comb[2]*comb[0]
    sum.append(temp)

for i in range(1,N+1):
    print(sum.count(i))

### FIXED CODE ###

import sys
import itertools
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

N = I()

# combs = list(itertools.product(range(1,int(N**0.5)), repeat=3))
combs = list(itertools.combinations_with_replacement(range(1,int(N**0.5)), 3))
sum = [0] * (N+1)

for comb in combs:
    temp = comb[0]**2 + comb[1]**2 + comb[2]**2+ comb[0]*comb[1]+ comb[1]*comb[2] + comb[2]*comb[0]
    if temp <= N:
        if comb[0] != comb[1] and comb[1] != comb[2] and comb[2] != comb[0]:
            sum[temp] += 6
            # sum.append(temp)
            # sum.append(temp)
            # sum.append(temp)
            # sum.append(temp)
            # sum.append(temp)
            # sum.append(temp)
        elif comb[0] == comb[1] == comb[2]:
            sum[temp] += 1
            # sum.append(temp)
        else:
            sum[temp] += 3
            # sum.append(temp)
            # sum.append(temp)
            # sum.append(temp)


for i,s in enumerate(sum):
    if i == 0:
        continue
    print(s)
# for i in range(1,N+1):
#     print(sum.count(i))

# elif (comb[0] == comb[1] and comb[1] != comb[2] and comb[2] != comb[0]) or (comb[0] != comb[1] and comb[1] == comb[2] and comb[2] != comb[0]) or (comb[0] != comb[1] and comb[1] != comb[2] and comb[2] == comb[0]):
### BUGGY CODE ###

def inpl(): return list(map(int, input().split()))
n = int(input())
a = inpl()
#sa = sorted(a)
maximum = max(a)

while True:
    c = 0
    if maximum//2-c in a:
        print(maximum, maximum//2)
        break
    elif maximum-maximum//2+c in a:
        print(maximum, maximum-maximum//2+c)
        break
    else:
        c += 1
    

### FIXED CODE ###

def inpl(): return list(map(int, input().split()))
n = int(input())
a = inpl()
maximum = max(a)
diff = maximum/2
a.remove(maximum)
min_num = [abs(i - diff) for i in a]
min_num = a[min_num.index(min(min_num))]
print(maximum, min_num)
### BUGGY CODE ###

n = int(input())

print(sum(range(1, n)))

### FIXED CODE ###

n = int(input())

print(n*(n-1) // 2)
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
q = int(input())
bc = [list(map(int, input().split())) for i in range(q)]

for b, c in bc:
    sumA = 0
    for i in range(0, n):
        if a[i] == b:
            a[i] = c
        sumA += a[i]
    print(sumA)
            

### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
q = int(input())
bc = [list(map(int, input().split())) for i in range(q)]

sumA = 0
num = [0]*100100
for aa in a:
    num[aa] += 1
    sumA += aa

for b, c in bc:
    sumA = sumA + ((c - b) * num[b])
    num[c] += num[b]
    num[b] = 0
    print(sumA)
### BUGGY CODE ###

from functools import reduce
import sys
input = sys.stdin.readline

N = int(input())
AS = map(int, input().split())

ans = reduce(lambda a,b: a*b, AS)
if ans > pow(10, 18):
    print(-1)
else:
    print(ans)


### FIXED CODE ###


import sys
from functools import reduce

input = sys.stdin.readline

N = int(input())
A = input().rstrip().split()

def main():
    result = 1
    if '0' in A:
        return 0

    for a in A:
        a = int(a)
        result *= a

        if result > pow(10, 18):
            return -1

    return result

ans = main()
print(ans)
### BUGGY CODE ###

import bisect

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

def count(t):
    c = 0
    for x in a:
        if x == 0:
            c += len(a) - 1 if t >= 0 else 0
        elif x > 0:
            c += bisect.bisect_right(a, (t // x)) - (x * x <= t)
        else:
            c += len(a) - bisect.bisect_left(a, -(t // -x)) - (x * x <= t)
    return c

lo = -1000000000000000001
hi = 1000000000000000001
while hi - lo > 1:
    mid = (lo + hi) // 2
    if count(mid) < k * 2:
        lo = mid
    else:
        hi = mid
print(hi)


### FIXED CODE ###

import bisect

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

def count(t):
    c = 0
    for x in a:
        if x == 0:
            c += len(a) - 1 if t >= 0 else 0
        elif x > 0:
            c += bisect.bisect_left(a, (t // x) + 1) - (x * x <= t)
        else:
            c += len(a) - bisect.bisect_left(a, -(t // -x)) - (x * x <= t)
    return c

lo = -1000000000000000001
hi = 1000000000000000001
while hi - lo > 1:
    mid = (lo + hi) // 2
    if count(mid) < k * 2:
        lo = mid
    else:
        hi = mid
print(hi)
### BUGGY CODE ###

import functools;M=10**9+7;F=functools.lru_cache(2**16)(lambda X:pow(X,M-2,M));H,W,A,B=map(int,input().split());Z=C=1
for I in range(H-1):Z=C=C*(W+H-B-2-I)*F(I+1)%M
for I in range(1,H-A):C=C*(B-1+I)*F(I)*(H-I)*F(W+H-B-1-I)%M;Z=(Z+C)%M
print(Z)

### FIXED CODE ###

F=lambda X:pow(X,M-2,M);M=10**9+7;H,W,A,B=map(int,input().split());Z=C=1
for I in range(min(W-1-B,H-1)):Z=C=C*(W+H-B-2-I)*F(I+1)%M
for I in range(1,H-A):C=C*(B-1+I)*F(I)*(H-I)*F(W+H-B-1-I)%M;Z=(Z+C)%M
print(Z)
### BUGGY CODE ###

# input 現在座標ｘ, 移動回数k, 移動距離d
x, k, d = map(int, input().split())

# 現在座標が正ならば引き算、負なら足し算をする
for i in range(k):
    if x > 0:
        x = x - d;
    elif x <= 0:
        x = x + d;

# xが負の数の場合正の数に編集する
if x < 0:
    x = x * (-1);

print(x);


### FIXED CODE ###

x,k,d=map(int,input().split())
x=abs(x)
if x>k*d:
    print(x-k*d)
else:
    k-=x//d
    x-=(d*(x//d))
    ans=[x,abs(d-x)]
    print(ans[k%2])
### BUGGY CODE ###

import numpy as np
from scipy.sparse.linalg import dsolve
N=int(input())
a=list(map(int, input().split()))
for i in range(N):
  a[i]=a[i]*2
X=[[0 for j in range(N)] for i in range(N)]
for i in range(N-1):
    X[i][i]=1
    X[i][i+1]=1
X[N-1][0]=1
X[N-1][N-1]=1
a=np.array(a)
X=np.array(X)
b= dsolve.spsolve(X, a, use_umfpack=True)
for i in range(N):
  print(int(b[i]), end=" ")
    

### FIXED CODE ###

N=int(input())
a=list(map(int, input().split()))
for i in range(N):
    a[i]=a[i]*2
b=[0 for i in range(N)]
sum1=0
for i in range(N-1):
    if i%2==0:
        sum1+=a[i]
    else:
        sum1+=a[i]*(-1)
ans=(sum1+a[N-1])/2
b[0]=ans
for i in range(N-1):
    b[i+1]=a[i]-b[i]
for i in range(N):
    print(int(b[i]), end=" ")
### BUGGY CODE ###

import numpy as np
H,W = map(int,input().split())
S = [list(input()) for _ in range(H)]
L = np.zeros((H,W),dtype=np.int)
R = np.zeros((H,W),dtype=np.int)
U = np.zeros((H,W),dtype=np.int)
D = np.zeros((H,W),dtype=np.int)
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            L[i][j] = 0
        else:
            L[i][j] = L[i][j-1]+1
        if S[-i-1][-j-1] == "#":
            R[-i-1][-j-1] = 0
        else:
            R[-i-1][-j-1] = R[-i-1][-j]+1

for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            None
        else:
            U[i][j] = U[i-1][j]+1
        if S[-i-1][-j-1] == "#":
            None
        else:
            D[-i-1][-j-1] = D[-i][-j-1]+1
print(np.max(L+R+U+D)-3)

### FIXED CODE ###

import numpy as np
H,W = map(int,input().split())
S = np.array([list(input()) for _ in range(H)])
S = np.where(S=='.',1,0)


L = np.zeros((H,W),dtype=np.int)
R = np.zeros((H,W),dtype=np.int)
U = np.zeros((H,W),dtype=np.int)
D = np.zeros((H,W),dtype=np.int)

for i in range(W):
    L[:,i] = (L[:,i-1]+1)*S[:,i]
    R[:,-i-1] = (R[:,-i]+1)*S[:,-i-1]
for j in range(H):
    U[j,:] = (U[j-1,:]+1)*S[j,:]
    D[-j-1,:] = (D[-j,:]+1)*S[-j-1,:]

print(np.max(L+R+U+D)-3)
### BUGGY CODE ###

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

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

A = sorted(A)

dicts = {}

for i in range(len(A)):
    for num in make_divisors(A[i]):
        if num in dicts:
            dicts[num]+=1
        else:
            dicts[num]=1
    
    if i>=2:
        del_list=[]
        for k,v in dicts.items():
            if v<i:
                del_list.append(k)
        for d in del_list:
            del dicts[d]

print(max(dicts.keys()))

### FIXED CODE ###

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

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

A = sorted(A)
from collections import Counter
check = Counter(A)
for c,k in check.items():
    if k>=N-1:
        A=[c]



def del_dicts(dicts,i):
    del_list=[]
    for k,v in dicts.items():
        if v<i:
            del_list.append(k)
    for d in del_list:
        del dicts[d]
    return dicts

dicts = {}

for i in range(len(A)):
    if i<=2:
        for num in make_divisors(A[i]):
            if num in dicts:
                dicts[num]+=1
            else:
                dicts[num]=1
    
    else:
        del_list=[]
        
        for k in dicts.keys():
            if A[i]%k == 0:
                dicts[k]+=1
    dicts = del_dicts(dicts,i)
print(max(dicts.keys()))
### BUGGY CODE ###

import sys
input = sys.stdin.readline


if __name__ == "__main__":
    n = int(input())
    s_array = [sorted(input()[:-1]) for _ in range(n)]

    count = 0
    for i in range(n):
        for j in range(i+1, n):
            if(s_array[i] == s_array[j]):
                count += 1

    print(count)


### FIXED CODE ###

import sys
input = sys.stdin.readline


if __name__ == "__main__":
    n = int(input())
    s_array = [''.join(sorted(input()[:-1])) for _ in range(n)]

    s_array.sort()
    ans = 0
    count = 0
    word = s_array[0]
    for i in range(1, n):
        if(word == s_array[i]):
            count += 1
        else:
            ans += (count*(count+1))//2
            count = 0
        word = s_array[i]

    ans += (count*(count+1))//2
    print(ans)
### BUGGY CODE ###

s=input().rstrip()
ans=[0 for i in range(2019)]
ans[0]=1
for i in range(len(s)):
    tmp=int(s[i:])%2019
    ans[tmp]+=1
count=0
for i in ans:
    count+=int(i*(i-1)/2)
print(count)

### FIXED CODE ###

s=input().rstrip()
ans=[0 for i in range(2019)]
tmp=0
a=1
rs=reversed(s)
for i in rs:
    ans[tmp%2019]+=1
    tmp=(tmp+int(i)*a)%2019
    a*=10
    a%=2019
ans[tmp%2019]+=1
count=0
for i in ans:
    count+=int(i*(i-1)/2)
print(count)
### BUGGY CODE ###


n,m = list(map(int,input().split()))
h = list(map(int,input().split()))
d = [[0] * n for _ in range(n)]

for i in range(m):
  a,b = list(map(int,input().split()))
  a -= 1
  b -= 1
  d[a][b] = 1
  d[b][a] = 1 

mh = [0] * n
ans = set()

for i in range(n):
  t = h[i]
  mh[i] = 1
  for j in range(n):
    if d[i][j] == 1 and i != j:
      if t <= h[j]:
        mh[i] = 0
  if mh[i] == 1: ans.add(i)

print(len(ans))


### FIXED CODE ###


n,m = list(map(int,input().split()))
h = list(map(int,input().split()))
d = [1] * n
for i in range(m):
  a,b = list(map(int,input().split()))
  a -= 1
  b -= 1
  if h[a] > h[b]:
    d[b] = 0
  elif h[b] > h[a]:
    d[a] = 0
  else:
    d[a] = 0
    d[b] = 0

print(sum(d))
### BUGGY CODE ###

n = int(input())
a = (list(map(int, input().split())))
q = int(input())
bc = [list(map(int, input().split())) for _ in range(q)]
sum = 0
for i in range(q):
    for j in range(n):
        if a[j] == bc[i][0]:
            a[j] = bc[i][1]
    for k in range(n):
        sum += a[k]
    print(sum)
    sum = 0


### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
q = int(input())
bc = [list(map(int, input().split())) for _ in range(q)]
s = 0
for g in range(len(a)):
    s += a[g]
e = [0] * 100001
for i in a:
    e[i] += 1
for j in range(q):
    s += (bc[j][1] - bc[j][0]) * e[bc[j][0]]
    e[bc[j][1]] += e[bc[j][0]]
    e[bc[j][0]] = 0
    print(s)
### BUGGY CODE ###

def gcd(a,b):
    while b != 0:
        a, b = b, a%b
#        print(a,b)
    return(a)

def main():    
    N,X = map(int,input().split())
    x = list(map(int,input().split()))
    for i in range(N):
        x[i] = abs(x[i]-X)
    while len(x) > 1:
#        print(x[0],x[1])
        y = gcd(x[0],x[1])
        x.pop(0)
        x.pop(0)
        x.insert(0,y)
#        print(x)
    print(x[0])

if __name__ == "__main__":
    main()

### FIXED CODE ###

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

def main():    
    N,X = map(int,input().split())
    x = list(map(int,input().split()))
    for i in range(N):
        x[i] = abs(x[i]-X)
    y = x[0]
    for i in range(N):
        if x[i]%y == 0:
            continue
        y = gcd(y,x[i])
    print(y)

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

n = int(input())
A = list(map(int, input().split()))
mod = 10 ** 9 + 7

bit_count = [0] * 60
ans = 0
for a in A:
    for bit in range(60):
        if (a >> bit) & 1:
            count = bit_count[bit]
            ans += (2 ** bit) * (n-1-count*2)
            bit_count[bit] += 1
            ans %= mod
        else:
            continue

print(ans)

### FIXED CODE ###

n = int(input())
A = list(map(int, input().split()))
mod = 10 ** 9 + 7

bit_count = [0] * 60
ans = 0
for a in A:
    for bit in range(60):
        if (a >> bit) & 1:
            count = bit_count[bit]
            ans += (2 ** bit) * (n-1-count*2)
            bit_count[bit] += 1
            ans %= mod
        else:
            continue

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

import sys
from itertools import groupby

readline = sys.stdin.buffer.readline
readline()
b = list(map(int, readline().split()))

b.sort(reverse=True)
# print(b)

x = len(b)
c = [(k, len(tuple(L))) for k, L in groupby(b)]
# print(c)
for i, v in enumerate(c):
  if v[1] > 1:
    x -= v[1]
    continue
  for vv in c[-1:i:-1]:
    # print(v[0], vv[0])
    if v[0] / 2 < vv[0]:
      break
    if not v[0] % vv[0]:
      x -= 1
      break
print(x)

### FIXED CODE ###

import sys
from itertools import groupby

readline = sys.stdin.buffer.readline
readline()
b = list(map(int, readline().split()))

b.sort()
# print(b)

x = len(b)
c = [(k, len(tuple(L))) for k, L in groupby(b)]
# print(c)
m = c[-1][0]
# print(m)
s = [True] * (m + 1)
# print(len(s))

a = 0
for i, le in c:
  if s[i]:
    if le <= 1:
      a += 1
    for v in range(i, m + 1, i):
      s[v] = False
# print(s)
print(a)
### BUGGY CODE ###

n, k = map(int, input().split())
p = [int(x) for x in input().split()]
highest = 0

for i in range(n-k+1):
    a = sum(p[i:i+k])
    if a > highest:
        highest = a

print((highest + k)/2)

### FIXED CODE ###

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

a= sum(p[0:k])
ans = a

for i in range(n-k):
    a -= p[i]
    a += p[i+k]
    ans = max(ans,a)

print((ans+k)/2)
### BUGGY CODE ###

n,k=map(int,input().split())
arr=list(map(int,input().split()))
temp=[0]*n
temp[n-2]=abs(arr[n-2]-arr[n-1])
if n>=3:
    i=n-3
    while i>=0:
        idx=i
        count=0
        temp1=[]
        for j in range(idx+1,n):
            temp1.append(abs(arr[j]-arr[idx])+temp[j])
            count+=1
            if count==k:
                break
        temp[idx]=min(temp1)
        i-=1
print(temp[0])

### FIXED CODE ###

n,k=map(int,input().split())
arr=list(map(int,input().split()))
temp=[0]*n
temp[n-2]=abs(arr[n-2]-arr[n-1])
if n>=3:
    i=n-3
    while i>=0:
        idx=i
        count=0
        temp1=[]
        for j in range(idx+1,n):
            temp1.append(abs(arr[j]-arr[idx])+temp[j])
            count+=1
            if count==k:
                break
        temp[idx]=min(temp1)
        i-=1
print(temp[0])
### BUGGY CODE ###

N=input()
S=[ list(raw_input()) for i in range(N)]

A=0
ans=0
for B in range(N):
	T={}

	for i in range(N):
		for j in range(N):
			T[(i,j)]=S[(i+A)%N][(j+B)%N]

	flag=0
	for i in range(N):
		if flag==-1: break
		for j in range(N):
			if T[ (i,j) ]==T[ (j,i) ]:
				pass
			else:
				flag=-1
				break
	else:
		ans+=1*N


print ans


### FIXED CODE ###

N=input()
S=[ list(raw_input()) for i in range(N)]

A=0
ans=0
for B in range(N):
	flag=0
	for i in range(N):
		if flag==-1: break
		for j in range(N):
			x1=(N+i-A)%N
			y1=(N+j-B)%N

			x2=(N+j-A)%N
			y2=(N+i-B)%N

			#print i,j,x1,y1,x2,y2
			if S[x1][y1]==S[x2][y2]:
				pass
			else:
				flag=-1
				#break
	else:
		ans+=1*N


print ans
### BUGGY CODE ###

from collections import deque
H, W, K = map(int, input().split())
sx, sy, gx, gy = map(int, input().split())
sx -= 1
sy -= 1
gx -= 1
gy -= 1
grid = [input() for _ in range(H)]


def inside(r, c):
    return 0 <= r < H and 0 <= c < W and grid[r][c] != '@'


dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
INF = 10**9
dist = [[INF]*W for _ in range(H)]
que = deque()
que.append([sx, sy])
dist[sx][sy] = 0
while que:
    [x, y] = que.popleft()
    for k in range(4):
        for d in range(1, K+1):
            nx = x + d*dx[k]
            ny = y + d*dy[k]
            if not inside(nx, ny) or dist[nx][ny] <= dist[x][y]:
                break
#             if not inside(nx, ny):
#                 break
#             if dist[nx][ny] <= dist[x][y]:
#                 break
            if dist[nx][ny] == INF:
                dist[nx][ny] = dist[x][y] + 1
                que.append([nx, ny])
if dist[gx][gy] == INF:
    print(-1)
else:
    print(dist[gx][gy])


### FIXED CODE ###

from collections import deque
H, W, K = map(int, input().split())
sx, sy, gx, gy = map(int, input().split())
sx -= 1
sy -= 1
gx -= 1
gy -= 1
grid = [input() for _ in range(H)]


def inside(r, c):
    return 0 <= r < H and 0 <= c < W and grid[r][c] != '@'


dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
INF = 10**9
dist = [[INF]*W for _ in range(H)]
que = deque()
que.append([sx, sy])
dist[sx][sy] = 0
while que:
    [x, y] = que.popleft()
    for k in range(4):
        for d in range(1, K+1):
            nx = x + d*dx[k]
            ny = y + d*dy[k]
            if not inside(nx, ny) or dist[nx][ny] <= dist[x][y]:
                break
#             if not inside(nx, ny):
#                 break
#             if dist[nx][ny] <= dist[x][y]:
#                 break
            if dist[nx][ny] == INF:
                dist[nx][ny] = dist[x][y] + 1
                que.append([nx, ny])
if dist[gx][gy] == INF:
    print(-1)
else:
    print(dist[gx][gy])