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

def main():
    n=int(input())
    s=[input() for _ in [0]*n]
    cnt=0
    for a in range(n):
        flag=0
        for i in range(n):
            ia_n=(i+a)%n
            for j in range(i,n-a):
                if s[i][j+a]!=s[j][ia_n]:
                    flag=1
                    break
            else:
                for j in range(n-a,n):
                    if s[i][j+a-n]!=s[j][ia_n]:
                        flag=1
                        break
            if flag==1:
                break
        else:
            cnt+=1
    print(n*cnt)
main()

### FIXED CODE ###

def main():
    n=int(input())
    nn=2*n-1
    s=[input() for _ in [0]*n]
    s=[i+i[:-1] for i in s]+[i+i[:-1] for i in s][:-1]
    ans=[[0]*n for _ in [0]*n]
    for i in range(1,nn):
        m=min(i+1,n)
        for j in range(nn-1):
            for k in range(max(i-j-n,0)+1,min(m,nn-j,i-j+n)):
                ii=i-k
                if s[i][j]!=s[ii][j+k]:
                    l=min(n-k+1,ii,j)
                    width=l-max(-1,ii-n,j-n)
                    ans[ii-l][j-l]=max(ans[ii-l][j-l],width)
    for i in range(1,n):
        for j in range(1,n):
            ans[i][j]=max(ans[i][j],ans[i-1][j-1]-1)
    print(sum([sum([i==0 for i in a]) for a in ans]))
main()
### BUGGY CODE ###

import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int,input().split()))

cand = [(0,N)]

ans = 0
while cand:
    temp = []
    for l,r in cand:
        m = min(A[l:r])
        i = A[l:r].index(m) + l
        ans += m * (i-l+1) * (r-i)
        if l < i:
            temp.append([l,i])
        if i+1 < r:
            temp.append([i+1,r])
    cand = temp

print(ans)




### FIXED CODE ###

import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int,input().split()))

class SegmentTree:
    def __init__(self,data,op,default):
        N = len(data)
        self.N = N
        self.op = op
        self.default = default
        self.l = 2**((N-1).bit_length())
        self.data = [default]*self.l + data + [default]*(self.l-N)
        for i in range(self.l-1,0,-1):
            self.data[i] = op(self.data[2*i], self.data[2*i+1])

    def get_data(self):
        return self.data[self.l:self.l + self.N]

    def set(self,i,val):
        i += self.l
        self.data[i] = val
        i = i//2
        while i > 0:
            self.data[i] = self.op(self.data[2*i], self.data[2*i+1])
            i = i//2

    def get(self,i,j):
        i += self.l
        j += self.l
        s = self.default 
        while j-i > 0:
            if i & 1:
                s = self.op(s,self.data[i])
                i += 1
            if j & 1:
                s = self.op(s,self.data[j-1])
                j -= 1
            i = i//2
            j = j//2
        return s

AI = [(a,i) for i,a in enumerate(A)]

inf = 10**10
seg = SegmentTree(AI,min,(inf,inf))

cand = [(0,N)]

ans = 0
while cand:
    temp = []
    for l,r in cand:
        m, i = seg.get(l,r)
        ans += m * (i-l+1) * (r-i)
        if l < i:
            temp.append([l,i])
        if i+1 < r:
            temp.append([i+1,r])
    cand = temp

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

N, Q = map(int,input().split())
l = [list(map(int,input().split())) for _ in range(N-1)]
m = [list(map(int,input().split())) for _ in range(Q)]
ll = [0]*N
ans = [0]*N
bb = [[i] for i in range(1,N+1)]
for i in l:
    bb[i[0]-1].append(i[1])
for i in m:
    ll[i[0]-1] += int(i[1])

# print("bb=",bb)
# print("ll=",ll)
for i in range(N):
    for k in range(len(bb[i])):
        if k == 0 :
            ans[i] += ll[i]
        else:
            ll[bb[i][k]-1] += ll[i]
print(*ans)

### FIXED CODE ###

n, q = map(int, input().split())
ki = [0] * (n + 1)
for i in range(n - 1):
    a, b = map(int, input().split())
    ki[b] = a
# print("ki=",ki)

c = [0] * (n + 1)
for i in range(q):
    p, x = map(int, input().split())
    c[p] += x
# print("c=",c)

for i in range(1, n + 1):
    c[i] += c[ki[i]]
# print(c)

print(" ".join(map(str, c[1:])))
### BUGGY CODE ###

A,B,K = map(int, input().split())
for i in range (K):
  if A >= 1:
    A -= 1
  elif B >= 1:
    B -= 1
print(A,B)

### FIXED CODE ###

A,B,K = map(int, input().split())
print(max(0,A-K),max(0,B-max(0,K-A)))
### BUGGY CODE ###

# [時間、変数、処理の種類]をイベントに追加。処理の種類は1：工事開始、2：工事終了、3：判定
from heapq import *
n, q = map(int, input().split())
events = []
ans = [-1] * q
koji = []
set_koji=set()
for _ in range(n):
    s, t, x = map(int, input().split())
    events += [[s - x - 0.3, x, 1]]
    events += [[t - x - 0.7, x, 2]]
for i in range(q):
    d = int(input())
    events += [[d, i, 3]]
events.sort()
for j, h, s in events:
    if s == 1:
        heappush(koji,h)
        set_koji.add(h)
    elif s == 2:
        set_koji.remove(h)
    else:
        while koji:
            if koji[0] in set_koji:
                break
            heappop(koji)
        if koji:
            ans[h] = koji[0]
print(*ans, sep="\n")


### FIXED CODE ###

import sys
from bisect import *

input = sys.stdin.readline

def f(n, q):
    xst = []
    for i in range(n):
        s, t, x = map(int, input().split())
        xst.append([x, s - x, t - x])
    xst.sort()

    ds = []
    for i in range(q):
        d = int(input())
        ds.append(d)

    ans = [-1] * q
    jump = [-1] * q

    for x, s, t in xst:
        i = bisect_left(ds, s)
        fi = bisect_left(ds, t)
        while i < fi:
            if jump[i] != -1:
                i = jump[i]
            else:
                ans[i] = x
                jump[i] = fi
                i += 1

    print(*ans, sep="\n")

n, q = map(int, input().split())
f(n, q)
### BUGGY CODE ###

N,M=map(int,input().split())
H=list(map(int,input().split()))
A=[];B=[]
retu=[[0]*N for _ in range(N)]
flg=[1]*N


for m in range(M):
  array=list(map(int,input().split()))
  A.append(array[0])
  B.append(array[1])

for m in range(M):
  if H[A[m]-1]<H[B[m]-1]:
    flg[A[m]-1]=0
  elif H[A[m]-1]>H[B[m]-1]:
    flg[B[m]-1]=0
  else:
    flg[A[m]-1]=flg[B[m]-1]=0
  
print(flg.count(1))
 
  

### FIXED CODE ###

N,M=map(int,input().split())
H=list(map(int,input().split()))
flg=[1]*N


for m in range(M):
  a,b =map(int,input().split())
  if H[a-1]<H[b-1]:
    flg[a-1]=0
  elif H[a-1]>H[b-1]:
    flg[b-1]=0
  else:
    flg[a-1]=flg[b-1]=0
  
print(flg.count(1))
### BUGGY CODE ###

N = int(input())
A = list(map(int,input().split()))
ans = []
for i in range(1,N+1):
    ans.append(str(A.index(i)+1))
print(' '.join(ans))

### FIXED CODE ###

N = int(input())
A = list(map(int,input().split()))
ans = [0] * N
for i,val in enumerate(A):
    ans[val-1] = i+1
print(*ans)
### BUGGY CODE ###

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

num = n-k

ans = 1
before = 0

for i in range(num+1):
    ans *= a[i]

for i in range(1,num+1):
    before = ans
    ans = ans/ a[i-1] * a[i+k-1]
    if before >= ans:
        print("No")
    else:
        print("Yes")


### FIXED CODE ###

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

num = n-k

for i in range(1,num+1):
    if a[i-1] >= a[i+k-1]:
        print("No")
    else:
        print("Yes")
### BUGGY CODE ###

n,q=map(int,input().split())
lst=[[] for i in range(n)]
ans=[0]*n

for i in range(n-1):
      a,b=map(int,input().split())
      lst[a-1].append(b-1)
      lst[b-1].append(a-1)

for i in range(q):
    s,t=map(int,input().split())
    ans[s-1]+=t

Q=[0]

flag=[True]+[False]*(n-1)


while Q:
  v=Q.pop(0)
  flag[v]=True
  cnt=ans[v]
  for i in lst[v]:
    if flag[i]:
      continue
    else:
      flag[i]=True
      ans[i]+=cnt
      Q.append(i)


print(' '.join([str(i) for i in ans]))

### FIXED CODE ###

n,q=map(int,input().split())
lst=[[] for i in range(n)]
ans=[0]*n

for i in range(n-1):
      a,b=map(int,input().split())
      lst[a-1].append(b-1)
      lst[b-1].append(a-1)

for i in range(q):
    s,t=map(int,input().split())
    ans[s-1]+=t

Q=[0]

flag=[True]+[False]*(n-1)


while Q:
  v=Q.pop()
  flag[v]=True
  for i in lst[v]:
    if flag[i]:
      continue
    flag[i]=True
    ans[i]+=ans[v]
    Q.append(i)


print(' '.join([str(i) for i in ans]))
### BUGGY CODE ###

import numpy as np
def serach(target, array):
    l, r = -1, len(array)
    while r - l > 1:
        m = (l+r)//2
        if array[m] < target:
            l = m
        else:
            r = m
    return l, r

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

BC = [list(map(int, input().split())) for _ in range(M)]
BC.sort(key=lambda x:x[1], reverse=True)
A.sort()
for b, c in BC:
    l, r = serach(c, A)
    if r <= 0:
        break
    b = min(b, r)
    A = np.hstack([A[b:], [c] * b])

print(sum(A))


### FIXED CODE ###

N, M = map(int, input().split())
A = [[1, int(i)] for i in input().split()]
for i in range(M):
    b, c = map(int, input().split())
    A.append([b, c])

total = 0
D = []
A = sorted(A, key=lambda x:x[1], reverse=True)
for b, c in A:
    if len(D) >= N:
        break
    D.extend([c]*b)
print(sum(D[:N]))
### BUGGY CODE ###

N,K=map(int,input().split())
p=list(map(int,input().split()))
array=[]

for n in range(N-(K-1)):
  a = sum(p[n:n+K])
  array.append(a)

ans=(max(array)+K)/2
print(ans)
  

### FIXED CODE ###

N,K=map(int,input().split())
p=list(map(int,input().split()))
array=[]

a=sum(p[0:K])
array.append(a)

for n in range(N-K):
  a=a-p[n]+p[n+K]
  array.append(a)

ans=(max(array)+K)/2
print(ans)
### BUGGY CODE ###

#!/usr/bin/env python3
import sys
sys.setrecursionlimit(300000)


def solve(N: int, W: int, w: "List[int]", v: "List[int]"):
    dp = [0] * (W + 1)
    for i in range(N):
        for j in range(W, -1, -1):
            if j + w[i] <= W:
                dp[j + w[i]] = max(dp[j + w[i]], dp[j] + v[i])
    ret = max(dp)
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    w = [int()] * (N)  # type: "List[int]" 
    v = [int()] * (N)  # type: "List[int]" 
    for i in range(N):
        w[i] = int(next(tokens))
        v[i] = int(next(tokens))
    solve(N, W, w, v)

if __name__ == '__main__':
    main()


### FIXED CODE ###

#!/usr/bin/env python3
import sys
sys.setrecursionlimit(300000)


def solve(N: int, W: int, w: "List[int]", v: "List[int]"):
    dp = [0] * (W + 1)
    for i in range(N):
        for j in range(W, -1, -1):
            if j + w[i] <= W:
                dp[j + w[i]] = max(dp[j + w[i]], dp[j] + v[i])
    ret = max(dp)
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    w = [int()] * (N)  # type: "List[int]" 
    v = [int()] * (N)  # type: "List[int]" 
    for i in range(N):
        w[i] = int(next(tokens))
        v[i] = int(next(tokens))
    solve(N, W, w, v)

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

import sys
import math  # noqa
import bisect  # noqa
import queue  # noqa


def input():
    return sys.stdin.readline().rstrip()


if __name__ == '__main__':
    S = int(input())

    if len(str(S)) == 19:
        print(0, 0, 1000000000, 0, 0, 1000000000)
        exit(0)

    search = min(1000000000, math.ceil(math.sqrt(S)))
    for i in range(search, 0, -1):
        if S % i == 0:
            print(0, 0, i, 0, 0, S // i)
            exit(0)


### FIXED CODE ###

import sys
import math  # noqa
import bisect  # noqa
import queue  # noqa


def input():
    return sys.stdin.readline().rstrip()


if __name__ == '__main__':
    S = int(input())

    x1, y1 = 0, 0
    x2, y2 = 1000000000, 1
    x3 = (x2 - S % x2) % x2
    y3 = (S + x3) // x2
    print('{} {} {} {} {} {}'.format(x1, y1, x2, y2, x3, y3))
### BUGGY CODE ###

import sys
import numpy as np

def retmin(a, b):
    if a > b:
        return b
    return a

input = sys.stdin.readline

# N 足場の数、K カエルができる行動
n, k = map(int, input().split())

# H 各足場の高さ
# h = list(map(int, input().split()))
h = [int(i) for i in input().split()]

# 初期化
# dp = np.full(100100, 1000000000)
dp = [999999999999 for i in range(100100)]
dp[0] = 0

for i in range(n):
    for j in range(k):
        if (i + j < n):
            c = abs(h[i+j]-h[i-1]) + dp[i-1]
            if (dp[i+j] > c):
                dp[i+j]  = c
print(int(dp[n-1]))

### FIXED CODE ###

import sys
import numpy as np

input = sys.stdin.readline

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

h = np.array([int(x) for x in input().split()])

# 初期化
dp = np.zeros(n, dtype=int)

for i in range(1, n):
    start = max(0, i-k)
    dp[i] = min(dp[start:i] + np.abs(h[i] - h[start:i]))
print(dp[n-1])
### BUGGY CODE ###

import fractions
from functools import reduce
 
def aaa(*numbers):
    return reduce(fractions.gcd, numbers)
 
num = int(input())
list = []
for i in range(1,num+1):
    for j in range(1,num+1):
        for k in range(1,num+1):
            list.append(aaa(i,j,k))
print(sum(list))

### FIXED CODE ###

import math
from functools import reduce
 
def aaa(*numbers):
    return reduce(math.gcd, numbers)
 
num = int(input())
list = []
for i in range(1,num+1):
    for j in range(1,num+1):
        for k in range(1,num+1):
            list.append(aaa(i,j,k))
print(sum(list))
### BUGGY CODE ###

def multiply2(A):
    res = 1
    for item in A:
        res *= item
    if res > 10**18:
        return '-1'
    else:
        return res

if __name__ == "__main__":
    N = int(input())
    A = []
    A = list(map(int, input().split()))
    print(multiply2(A))

### FIXED CODE ###

def multiply2(A):
    res = 1
    if 0 in A:
        return 0
    for item in A:
        res *= item
        if res > 10**18:
            return -1
    return res

if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    print(multiply2(A))
### BUGGY CODE ###

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

ans = [0 for i in range(1000)]
l = '0123456789'
for i in range(1,N):
    b = S[i]
    for a in l:
        if a in S[:i]:
            for c in l:
                if c in S[i+1:]:
                    s = a + b + c
                    ans[int(s)] = 1
print(sum(ans))

### FIXED CODE ###

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

def judge(A,B,C):
    s = S
    a = s.find(A)
    if a < 0:
        return False

    s = s[a+1:]
    b = s.find(B)
    if b < 0:
        return False

    s = s[b+1:]
    c = s.find(C)
    if c < 0:
        return False
    return True

l = '0123456789'
print(sum([int(bool(judge(a,b,c))) for a in l for b in l for c in l]))
### BUGGY CODE ###

import itertools


h, w = map(int, input().split())
c = [list(map(ord, input())) for i in range(h)]
c = list(itertools.chain.from_iterable(c))
INF = 10 ** 9


ans = 0
dq = [0] * ((h + 1) ** 2)
for wi in range(w - 1):
    for diff in range(h + 1):
        cnt = 0
        for i in range(h):
            if i + diff >= h:
                break
            cnt += (c[i * w + wi] == c[(i + diff) * w + wi + 1])
        dq[(0 + diff) * (h + 1) + 0] = cnt
        for i in range(h):
            if i + 1 + diff >= h:
                break
            cnt -= (c[(h - i - 1 - diff) * w + wi] == c[(h - i - 1) * w + wi + 1])
            dq[(i + 1 + diff) * (h + 1) + i + 1] = cnt

        cnt = 0
        for i in range(h):
            if i + diff >= h:
                break
            cnt += (c[(i + diff) * w + wi] == c[i * w + wi + 1])
        dq[0 + diff] = cnt
        for i in range(h):
            if i + 1 + diff >= h:
                break
            cnt -= (c[(h - i - 1 - diff) * w + wi + 1] == c[(h - i - 1) * w + wi])
            dq[(i + 1) * (h + 1) + i + 1 + diff] = cnt
            
    dp = [INF] * ((h + 1) ** 2)
    dp[0] = 0
    for i in range(h + 1):
        for j in range(h + 1):
            if i + 1 < h + 1:
                dp[(i + 1) *(h + 1) + j] = min(dp[i * (h + 1) + j] + dq[i * (h + 1) + j], dp[(i + 1) *(h + 1) + j])
            if j + 1 < h + 1:
                dp[i * (h + 1) + j + 1] = min(dp[i * (h + 1) + j] + dq[i * (h + 1) + j], dp[i * (h + 1) + j + 1])
    ans += dp[-1]

print(ans)
    

### FIXED CODE ###

def solve(k):
    dp = [[0] * (h + 1) for i in range(h + 1)]
    for i in range(h):
        for diff in range(h - i):
            dp[i + 1][i + 1 + diff] = dp[i][i + diff] + (s[i + diff][k] == s[i][k + 1])
            dp[i + 1 + diff][i + 1] = dp[i + diff][i] + (s[i][k] == s[i + diff][k + 1])

    dq = [[INF] * (h + 1) for i in range(h + 1)]
    dq[0][0] = 0
    for i in range(h):
        dq[i + 1][0] = dq[i][0] + dp[h - i][h]
        dq[0][i + 1] = dq[0][i] + dp[h][h - i]  
    for i in range(h):
        for j in range(h):
            dq[i + 1][j + 1] = min(dq[i + 1][j] + dp[h - (i + 1)][h - j],
                                   dq[i][j + 1] + dp[h - i][h - (j + 1)])

    return dq[h][h]


h, w = map(int, input().split())
s = [input() for i in range(h)]
INF = 10 ** 5
OFFSET = 500

ans = 0
for i in range(w - 1):
    ans += solve(i)
    
print(ans)
### BUGGY CODE ###

N = int(input())
S = input()
St = []
for i in range(N):
  St += [ord(S[i])-97]
  
St.reverse()
ans = 0
#初期化
dp=[[0 for i in range(N+1)] for j in range(N+1)]

#DP:dp[i][j] Sのi文字目以降とj文字目以降の最大マッチング
for i in range(N):
  for j in range(i):
    if (St[i] == St[j]): 
      dp[i+1][j+1] = max(dp[i+1][j+1], dp[i][j] + 1)
      ans = max(ans, min(dp[i+1][j+1], i-j))
    if i-j <= dp[i+1][j+1]:
      break
      
print(ans)

### FIXED CODE ###

N = int(input())
S = input()
from collections import deque

def is_ok(l):
    hs = set()
    q = deque()
    for i in range(N-l+1):
        h = S[i:i+l]
        if h in hs:
            return True
        q.append(h)
        if len(q) >= l:
            hs.add(q.popleft())
    return False

ok = 0
ng = N//2+1
while ng-ok > 1:
    m = (ng+ok)//2
    if is_ok(m):
        ok = m
    else:
        ng = m
print(ok)
### BUGGY CODE ###

#
import sys
import math
 
# ひとつ入力
n = int(input())
 
# initial state
a = [int(i) for i in input().split()] 
# last state
b = [int(i) for i in input().split()] 
 
answer = 0
answer_p = -1
while answer >= 0:
    answer_p = answer
    max_diff=0
    max_diff_i=-1
    for i in range(n):
        diff = b[i] - a[i]
        if max_diff < diff:
            max_diff = diff
            max_diff_i = i
        if b[i] < 1:
            print(-1)
            exit()
    if max_diff < 0:
        print(-1)
        exit()
    elif max_diff==0:
        for i in range(n):
            if b[i] != a[i]:
                print(-1)
                exit()
        print(answer)
        exit()
    else:
        res = (b[max_diff_i]-a[max_diff_i]) // (b[(max_diff_i+1)%n]+b[(max_diff_i-1)%n])
        answer += res
        b[max_diff_i] -= res * (b[(max_diff_i+1)%n]+b[(max_diff_i-1)%n])
    if answer == answer_p:
        print(-1)
        exit()
#    print(a,b,max_diff,max_diff_i)


### FIXED CODE ###

#
import sys
import math

# ひとつ入力
n = int(input())

# initial state
a = [int(i) for i in input().split()] 
# last state
b = [int(i) for i in input().split()] 

answer = 0
answer_p = -1
while answer >= 0:
    answer_p = answer
    for i in range(n):
        tmp = b[(i-1)%n] + b[(i+1)%n]
        if 0 < tmp < b[i] and a[i] < b[i]:
            res = (b[i]-a[i]) // (b[(i+1)%n]+b[(i-1)%n])
            answer += res
            b[i] -= res * (b[(i+1)%n]+b[(i-1)%n])
        
    if answer == answer_p:
        break
#    print(a,b,answer)

for i in range(n):
    if a[i] != b[i]:
        print(-1)
        exit()
print(answer)
### BUGGY CODE ###

def main():
    s = input()
    t = input()
    if not set(t).issubset(set(s)):
        return -1
    m = 0
    st = s
    for i in t:
        if i in st:
            j = st.index(i)
            m += j + 1
            st = st[j+1:]
        else:
            m += len(st)
            st = s
            j = st.index(i)
            st = st[j+1:]
            m += j + 1
    return m
print(main())


### FIXED CODE ###

def main():
    s = input()
    t = input()
    if not set(t).issubset(set(s)):
        return -1
    m = 0
    st = s
    for i in t:
        if i in st:
            j = st.index(i)
            m += j + 1
            st = st[j+1:]
        else:
            m += len(st)
            st = s
            j = st.index(i)
            st = st[j+1:]
            m += j + 1
    return m
print(main())
### BUGGY CODE ###

#
import sys
import math
import numpy as np
import itertools

# いくつか入力
n,q = (int(i) for i in input().split())  
# 文字列をリストに
s = list(input())

# n行複数列の数をリストに
td = [[i for i in input().split()] for i in range(q)] 

#print(s,td)
answer = n
for i in range(n):
    temp = i
    for j in range(q):
        if td[j][0] == s[temp]:
            if td[j][1]== 'R':
                temp += 1
            else:
                temp -= 1
            if temp <0 or temp >= n:
                answer -= 1
#                print("l",i)
                break
#    print("a",temp)

print(answer)




### FIXED CODE ###

#
import sys
import math
import numpy as np
import itertools

# いくつか入力
n,q = (int(i) for i in input().split())  
# 文字列をリストに
s = list(input())

# n行複数列の数をリストに
td = [[i for i in input().split()] for i in range(q)] 


hidari = 0
migi   = n-1

for i in range(q-1,-1,-1):
    if td[i][0] == s[hidari] and td[i][1] == 'L':
        hidari += 1
        if hidari == n:
            print("0")
            exit()
    elif hidari >0:
        if td[i][0] == s[hidari-1] and td[i][1] == 'R' :
            hidari -= 1

for i in range(q-1,-1,-1):
    if td[i][0] == s[migi] and td[i][1] == 'R':
        migi -= 1
        if migi == -1:
            print("0")
            exit()
    elif migi <n-1:
        if td[i][0] == s[migi+1] and td[i][1] == 'L' :
            migi += 1

if migi < hidari:
    print("0")
else:
    print(migi-hidari+1)

#print(migi,hidari)
### BUGGY CODE ###

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

dp = [0] + [min(i, j, N - 1 - i, N - 1 - j) for i in range(N) for j in range(N)]
memo = [1] * (N * N + 1)

def move(fr, to):
    if dp[to] > dp[fr] + memo[fr]:
        dp[to] = dp[fr] + memo[fr]
        S.append(to)

ans = 0
for p in P:
    ans += dp[p]
    memo[p] = 0

    S = [p]
    while S:
        v = S.pop()

        q, r = divmod(v - 1, N)
        if r != 0:
            move(v, v - 1)
        if r != N - 1:
            move(v, v + 1)
        if q < N - 1:
            move(v, v + N)
        if q >= 1:
            move(v, v - N)

print(ans)

### FIXED CODE ###

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

dp = [0] + [min(i, j, N - 1 - i, N - 1 - j) for i in range(N) for j in range(N)]
memo = [1] * (N * N + 1)

def move(fr, to):
    if dp[to] > dp[fr] + memo[fr]:
        dp[to] = dp[fr] + memo[fr]
        S.append(to)

ans = 0
for p in P:
    ans += dp[p]
    memo[p] = 0

    S = [p]
    while S:
        v = S.pop()

        q, r = divmod(v - 1, N)
        if r != 0:
            move(v, v - 1)
        if r != N - 1:
            move(v, v + 1)
        if q < N - 1:
            move(v, v + N)
        if q >= 1:
            move(v, v - N)

print(ans)