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


n = int(input())
def gcd(a,b):
    if b == 0:
        return a
    else:
        return gcd(b,a%b)
def gcd_3(i,j,k):
    return gcd(gcd(i,j),k)
ans = 0
for i in range(1,n+1):
    for j in range(1,n+1):
        for k in range(1,n+1):
            ans += gcd_3(i,j,k)
print(ans)

### FIXED CODE ###

n = int(input())
import math
l = []
for i in range(1,n +1):
    for j in range(1,n+1):
        l.append(math.gcd(i,j))
ans = 0
for i in range(1, n+1):
    for j in l:
        ans += math.gcd(j,i)
print(ans)
### BUGGY CODE ###

import sys

N = int(sys.stdin.readline())
A = sys.stdin.readline().split()
for i in range(N):
        A[i] = int(A[i])

ans = [0]*N
for i in range(1, N+1):
        for j in range(N):
                if(A[j] == i):
                        ans[i-1] = j+1
print(" ".join(map(str, ans)))

### FIXED CODE ###

import sys

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

ans = [None]*N
for i, order in enumerate(A, 1):
        ans[order-1] = i
print(" ".join(map(str, ans)))
### BUGGY CODE ###

N=int(input())
t=dict()
ans=list()
for i in range(N):
    tmp=input()
    if tmp in t:
        t[tmp]+=1
    else:
        t[tmp]=1
ans=[kv[0] for kv in t.items() if kv[1] == max(t.values())]
ans.sort()
for i in ans:
    print(i)

### FIXED CODE ###

N=int(input())
t=dict()
ans=list()
for i in range(N):
    tmp=input()
    if tmp in t:
        t[tmp]+=1
    else:
        t[tmp]=1
max_v=max(t.values())
ans=[kv[0] for kv in t.items() if kv[1] == max_v]
ans.sort()
for i in ans:
    print(i)
### BUGGY CODE ###

s = input()
q = int(input())

for i in range(q):
    query = input()
    if query[0] == '1':
        s = s[::-1]
    else:
        if query[2] == '1':
            s = query[4]+s
        else:
            s = s+query[4]
print(s)

### FIXED CODE ###

s = input()
q = int(input())

t = 0
l = ''
r = ''
for i in range(q):
    query = input()
    if query == '1':
        if t == 1:
            t = 0
        else:
            t = 1
    else:
        if query[2] == '1':
            if t == 0:
                l = query[4:]+l
            else:
                r = r+query[4]
        else:
            if t == 0:
                r = r+query[4]
            else:
                l = query[4] + l
s = l+s+r
if t == 1:
    print(s[::-1])
else:
    print(s)
### BUGGY CODE ###

n=int(input())
s=list(input())
ans=n

for i in range(n):
  l=s[0:i]
  r=s[i+1:n]
  v=l.count("W")+r.count("E")
  if ans>v:
    ans=v
print(ans)


### FIXED CODE ###

n=int(input())
s=list(input())
ans=n

#累積和を求める
wcum=[];wcount=0
ecum=[];ecount=0
for i in range(n):
  if s[i]=="W":
    wcount+=1
  else:
    ecount+=1
  ecum.append(ecount)
  wcum.append(wcount)
ans=ecum[n-1]-ecum[0]
for i in range(1,n):
  v=wcum[i-1]+(ecum[n-1]-ecum[i])
  if ans>v:
    ans=v
print(ans)
### BUGGY CODE ###

def main():
    import fractions
    import sys
    input = sys.stdin.readline
    N = int(input())
    a = tuple(map(int,input().split()))
    MOD = 10**9+7
    g1 = 1
    for i,t in enumerate(a[:N//2]):
        g1 = divmod(g1*t,fractions.gcd(g1,t))[0]

    g2 = 1
    for i,t in enumerate(a[N//2:]):
        g2 = divmod(g2*t,fractions.gcd(g2,t))[0]
    res = 0
    g = fractions.gcd(g1,g2)

    r1 = (g2//g)%MOD
    for t in a[:N//2]:
        res += r1*divmod(g1,t)[0]%MOD
    r2 = (g1//g)%MOD
    for t in a[N//2:]:
        res += r2*divmod(g2,t)[0]%MOD
    print(res%MOD)
if __name__ == '__main__':
    main()


### FIXED CODE ###

import fractions
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int,input().split()))
g = 1
res = 0
MOD = 10**9+7
for elem in a:
    tmp = g*elem//fractions.gcd(g,elem)
    res *= tmp//g
    g = tmp
    res += g//elem
print(res%MOD)
### BUGGY CODE ###

words = list(input())
num = len(words)
children = [0 for s in range(num)]

for i in range(num):
  if words[i] == "R":
    a = 0
    while True:
      if words[a + i + 1] == "R":
        a += 1
      else:
        if a % 2 == 0:
          children[i + a] += 1
          break
        else:
          children[i + a + 1] += 1
          break      
  else:
    a = 0
    while True:
      if words[i - a - 1] == "L":
        a += 1
      else:
        if a % 2 == 0:
          children[i - a] += 1
          break
        else:
          children[i - a - 1] += 1
          break      
          
answer = str(children[0])
for i in range(num - 1):
  answer += " " + str(children[i+1])
print(answer)

### FIXED CODE ###

words = list(input())
num = len(words)
children = [0 for s in range(num)]
p = 0
for i in range(num - 1):
  p += 1
  if i == num - 2:
    if words[i] == "R":
      if p % 2 == 1:
        children[i] += int(p/2) + 1
        children[i+1] += int(p/2) + 1
        p = 0
      else:
        children[i] += int(p/2)
        children[i+1] += int(p/2) + 1
        p = 0
    else:
      p += 1
      if p % 2 == 1:
        children[i-p+2] += int(p/2) + 1
        children[i-p + 1] += int(p/2)
        p = 0
      else:
        children[i+2-p] += int(p/2)
        children[i-p+1] += int(p/2)
        p = 0
  elif words[i] == words[i + 1]:
    pass
  else:
    if words[i] == "R":
      if p % 2 == 1:
        children[i] += int(p/2) + 1
        children[i+1] += int(p/2)
        p = 0
      else:
        children[i] += int(p/2)
        children[i+1] += int(p/2)
        p = 0
    else:
      if p % 2 == 1:
        children[i-p+1] += int(p/2) + 1
        children[i-p] += int(p/2)
        p = 0
      else:
        children[i+1-p] += int(p/2)
        children[i-p] += int(p/2)
        p = 0
             
answer = str(children[0])
for i in range(num - 1):
  answer += " " + str(children[i+1])
print(answer)
### BUGGY CODE ###

# coding:utf-8

import math
import re

def main(params):
    n,m = params[0]
    a = params[1]
    tmpA = []
    for i in a:
        i = int(i)
        tmpA.append(i)
    a = tmpA
    a.sort()
    a.reverse()
    r = []
    for g in params[2:]:
        b = int(g.split()[0])
        c = int(g.split()[1])
        t = [c] * b
        r.extend(t)
    r.sort()
    r.reverse()
    cnt = 0
    total = 0L
    while True:
        if len(a) == 0:
            total += r[0]
            r.pop(0)
        elif len(r) == 0:
            total += a[0]
            a.pop(0)
        elif a[0] < r[0]:
            total += r[0]
            r.pop(0)
        else:
            total += a[0]
            a.pop(0)
        cnt += 1
        if cnt == n:
            break
    print total
#    a = params[1].split()
#    b = list(params[2])

if __name__ == "__main__":
    n,m = raw_input().split()
    n = int(n)
    m = int(m)
    a = raw_input().split()
    cnt = m # TODO: edit here
    params = [[n,m],a]
    for i in range(cnt):
        param = raw_input()
        params.append(param)
    main(params)



### FIXED CODE ###

# coding:utf-8

import math
import re

def main(params):
    n,m = params[0]
    a = params[1]
    tmpA = []
    for i in a:
        i = int(i)
        tmpA.append(i)
    a = tmpA
    a.sort()
    a.reverse()
    r = {}
    for g in params[2:]:
        b = int(g.split()[0])
        c = int(g.split()[1])
        if c not in r:
            r[c] = b
        else:
            r[c] += b
#        t = [c] * b
#        r.extend(t)
#    r.sort()
#    r.reverse()
    rV = r.keys()
    rV.sort()
    rV.reverse()
    cnt = 0
    total = 0L
    while True:
        if len(a) == 0:
            total += rV[0]
            r[rV[0]] -= 1
            if r[rV[0]] == 0:
                rV.pop(0)
        elif len(rV) == 0:
            total += a[0]
            a.pop(0)
        elif a[0] < rV[0]:
            total += rV[0]
            r[rV[0]] -= 1
            if r[rV[0]] == 0:
                rV.pop(0)
        else:
            total += a[0]
            a.pop(0)
        cnt += 1
        if cnt == n:
            break
    print total
#    a = params[1].split()
#    b = list(params[2])

if __name__ == "__main__":
    n,m = raw_input().split()
    n = int(n)
    m = int(m)
    a = raw_input().split()
    cnt = m # TODO: edit here
    params = [[n,m],a]
    for i in range(cnt):
        param = raw_input()
        params.append(param)
    main(params)
### BUGGY CODE ###

A, B, N = map(int, input().split())
ans_dict = {}
for x in range(N + 1):
    if (A * x) // B >= (A * (x // B)):
        ans_dict.setdefault((((A * x) // B) - (A * (x // B))), x)
    else:
        break


print(max(ans_dict))

### FIXED CODE ###

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

if N >= B:
    x = B - 1
    ans = ((A * x) // B) - (A * (x // B))
else:
    x = N
    ans = ((A * x) // B) - (A * (x // B))


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

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

c = 0
for i in range(N):
    for j in range(i + 1, N):
        if A[i] == A[j]:
            print("NO")
            c += 1
            break
if c == 0:
    print("YES")

### FIXED CODE ###

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

b = set(A)
B = list(map(int, b))

if len(A) == len(B):
    print("YES")
else:
    print("NO")
### BUGGY CODE ###

n, k = map(int, input().split())
h = list(map(int, input().split()))
dp = [pow(10, 9)] * n
dp[0] = 0
for i in range(n):
    for j in range(i+1, i+k+1):
        if j == n:
            break
        dp[j] = min(dp[j], dp[i] + abs(h[j] - h[i]))
print(dp[-1])

### FIXED CODE ###

n, k = map(int, input().split())
h = list(map(int, input().split()))
dp = [pow(10, 9)] * n
dp[0] = 0
for i in range(n):
    for j in range(i+1, i+k+1):
        if j == n:
            break
        dp[j] = min(dp[j], dp[i] + abs(h[j] - h[i]))
print(dp[-1])
### BUGGY CODE ###

#!/usr/bin/env python
# -*- coding: utf-8 -*-

N = int(input())
A_list = list(map(int, input().split()))
ans = 0

for i in range(N):
    if i != N -1:
        for j in range(i+1, N):
            ans += A_list[i] * A_list[j]
while ans >= 10 ** 17 + 700000000:
    ans -= 10 ** 17 + 700000000
while ans >= 10 ** 15 + 7000000:
    ans -= 10 ** 15 + 7000000
while ans >= 10 ** 13 + 70000:
    ans -= 10 ** 13 + 70000
while ans >= 10 ** 11 + 700:
    ans -= 10 ** 11 + 700
while ans >= 10 ** 9 + 7:
    ans -= 10 ** 9 + 7
print(ans)


### FIXED CODE ###

#!/usr/bin/env python
# -*- coding: utf-8 -*-

N = int(input())
A_list = list(map(int, input().split()))
ans = 0
SUM = sum(A_list)
for i in range(N):
    SUM -= A_list[i]
    if i != N -1:
        ans += A_list[i] * SUM
while ans >= 10 ** 21 + 7000000000000:
    ans -= 10 ** 21 + 7000000000000
while ans >= 10 ** 19 + 70000000000:
    ans -= 10 ** 19 + 70000000000
while ans >= 10 ** 17 + 700000000:
    ans -= 10 ** 17 + 700000000
while ans >= 10 ** 15 + 7000000:
    ans -= 10 ** 15 + 7000000
while ans >= 10 ** 13 + 70000:
    ans -= 10 ** 13 + 70000
while ans >= 10 ** 11 + 700:
    ans -= 10 ** 11 + 700
while ans >= 10 ** 9 + 7:
    ans -= 10 ** 9 + 7
print(ans)
### BUGGY CODE ###

n=int,input()
a=list(map(int,input().split()))
m=max(a)
ans=0
for i in range(m+2):
    pre=0
    for j in range(len(a)):
        if i-1<=a[j]<=i+1:
            pre+=1
    ans=max(ans,pre)
    
print(ans)
    

### FIXED CODE ###

from collections import Counter
n=int(input())
a=list(map(int,input().split()))
for i in range(n):
    a.append(a[i]-1)
    a.append(a[i]+1)
c=Counter(a).most_common()
print(c[0][1])
### BUGGY CODE ###

import bisect
import sys
input = sys.stdin.readline
N = int(input())
A = [int(input()) for i in range(N)]
B = [A[0]]
tmp = [0]*N
ans = 1
for i in range(1, N):
    id = bisect.bisect_left(B, A[i])
    if id == 0:
        bisect.insort_left(B, A[i])
    else:
        B[id-1] = A[i]
print(len(B))


### FIXED CODE ###

import bisect
import sys
import collections
input = sys.stdin.readline
N = int(input())
A = [int(input()) for i in range(N)]
B = [A[0]]
B = collections.deque(B)
tmp = [0]*N
ans = 1
for i in range(1, N):
    id = bisect.bisect_left(B, A[i])
    if id == 0:
        B.appendleft(A[i])
    else:
        B[id-1] = A[i]
print(len(B))
### BUGGY CODE ###

n,m,k = map(int,input().split())
A = [int(p) for p in input().split()]
B = [int(p) for p in input().split()]
a,b = [0],[0]

a_apnd = a.append
b_apnd = b.append
for i in range(n):
  a_apnd(a[i]+A[i])
for i in range(m):
  b_apnd(b[i]+B[i])

count = 0
for i in range(n+1):
  res = k-a[i]
  if res<0:
    break
  for t in range(m,-1,-1):
    if b[t] <= res:
      break
  count = max(count,i+t)

print(count)

### FIXED CODE ###

n,m,k = map(int,input().split())
A = [int(p) for p in input().split()]
B = [int(p) for p in input().split()]
a,b = [0],[0]

a_apnd = a.append
b_apnd = b.append
for i in range(n):
  a_apnd(a[i]+A[i])
for i in range(m):
  b_apnd(b[i]+B[i])

count = 0
t = m
for i in range(n+1):
  if k<a[i]:
    break
  while b[t] > k-a[i]:
    t -= 1
  count = max(count,i+t)

print(count)
### BUGGY CODE ###

from bisect import bisect
from operator import itemgetter

n, a, b = map(int, input().split())
ppp = map(int, input().split())
qqq = [i for i, p in sorted(enumerate(ppp, start=1), key=itemgetter(1))]
INF = 10 ** 15

dp = [(0, 0)]
for i in qqq:
    ndp = []
    s = bisect(dp, (i,))
    tmp_min = INF
    for j, cost in dp[:s]:
        cost += b
        ndp.append((j, cost))
        tmp_min = cost
    stay_cost = dp[s - 1][1]
    if tmp_min > stay_cost:
        ndp.append((i, stay_cost))
        tmp_min = stay_cost
    for j, cost in dp[s:]:
        cost += a
        if tmp_min > cost:
            ndp.append((j, cost))
            tmp_min = cost
    dp = ndp

print(dp[-1][1])


### FIXED CODE ###

from bisect import bisect
from operator import itemgetter

n, a, b = map(int, input().split())
ppp = map(int, input().split())
qqq = [i for i, p in sorted(enumerate(ppp, start=1), key=itemgetter(1))]
INF = 10 ** 15

dp = [(0, 0)]
for i in qqq:
    ndp = []
    s = bisect(dp, (i,))
    tmp_min = INF
    for j, cost in dp[:s]:
        cost += b
        ndp.append((j, cost))
        tmp_min = cost
    stay_cost = dp[s - 1][1]
    if tmp_min > stay_cost:
        ndp.append((i, stay_cost))
        tmp_min = stay_cost
    for j, cost in dp[s:]:
        cost += a
        if tmp_min > cost:
            ndp.append((j, cost))
            tmp_min = cost
    dp = ndp

print(dp[-1][1])
### BUGGY CODE ###

n=int(input())
cc=0
answer=0

while cc<=n:
  #length=len(str(cc))
  n3=str(cc).count("3")
  n5=str(cc).count("5")
  n7=str(cc).count("7")
  
  if len(str(cc))==n3+n5+n7 and min(n3,n5,n7)!=0:
    #print(cc,n3,n5,n7,answer)
    answer=answer+1
    
  cc=cc+1
  
print(answer)





### FIXED CODE ###

#解答より
N=int(input())

def dfs(s):
  if int(s)>N:
    return 0
  if all (s.count(c)>0 for c in "753"):
    ret=1
  else:
    ret=0
    
  for c in "753":
    ret=ret+dfs(s+c)
  
  return ret

print (dfs("0"))
### BUGGY CODE ###

import sys
from collections import deque
sys.setrecursionlimit(10**9)
input = lambda: sys.stdin.readline().rstrip()
inpl = lambda: list(map(int,input().split()))
N, M = inpl()
uv = [ set() for _ in range(N)]
vu = [ set() for _ in range(N)]
for _ in range(M):
    A, B = inpl()
    uv[A-1].add(B-1)
    vu[B-1].add(A-1)

for s in range(N):
    states_pool = deque()
    states_pool.append((s,set({}),set(range(N))-set({s})))
    while states_pool:
        u, chain, remain = states_pool.pop()
        if s in uv[u] and len(chain):
            print(len(chain)+1)
            for c in chain:
                print(c+1)
            print(u+1)
            exit()
        else:
            new_remain = remain.copy()
            if u != s: new_remain -= vu[u]
            candidates = new_remain & uv[u]
            new_remain -= uv[u]
            new_chain = chain.copy()
            new_chain.add(u)
            for c in candidates:
                states_pool.append((c, new_chain, new_remain))
    for i in range(s+1,N):
        uv[i].discard(s)
print(-1)

### FIXED CODE ###

import sys
input = lambda: sys.stdin.readline().rstrip()
inpl = lambda: list(map(int,input().split()))
N, M = inpl()
uv = [ set() for _ in range(N)]
vu = [ set() for _ in range(N)]
for _ in range(M):
    A, B = inpl()
    uv[A-1].add(B-1)
    vu[B-1].add(A-1)

EMPTY, LOOP, TREE = 0, 1, 2
ENTER, EXIT = 0, 1
status = [EMPTY] * N
for s in range(N):
    states_pool = [(s,[],ENTER)]
    while states_pool:
        u, chain, flag = states_pool.pop()
        if flag == EXIT or status[u] == TREE:
            status[u] = TREE
            continue
        elif status[u] == LOOP:
            break
        else: # ENTER, EMPTY
            status[u] = LOOP
            states_pool.append((u,chain,EXIT))
            new_chain = chain.copy()
            new_chain.append(u)
            for c in uv[u]:
                states_pool.append((c, new_chain, ENTER))
    else:
        continue
    break
else:
    print(-1)
    exit()

n = 0
while chain[n] != u:
    n += 1
chain = chain[n:]
L = len(chain)
nextnode = [None]*N
for i in range(L-1):
    nextnode[chain[i]] = chain[i+1]
nextnode[chain[L-1]] = chain[0]
pos = [None]*N
for p in range(len(chain)):
    pos[chain[p]] = p
i = 0
while i < L:
    h = 1
    c = chain[(i+1)%L]
    for j in uv[chain[i % L]]:
        p = pos[j]
        if p is None:
            continue
        else:
            if (p-i)%L > h:
                h = (p-i)%L
                c = j
    nextnode[chain[i % L]] = c
    i += 1
    while chain[i % L] != c:
        pos[chain[i % L]] = None
        i += 1

loop = [c]
k = c
while nextnode[k] != c:
    k = nextnode[k]
    loop.append(k)
print(len(loop))
for l in loop:
    print(l+1)
### BUGGY CODE ###

def solve():

  S=input()
  N=len(S)
  childs=[1]*N
  result=[0]*N

  for i in range(N):
    if S[i]=="R": 
      j = i
      count = 0
      for j in range(j+1,N):
        count = count + 1
        if S[j] == "L":
          if count & 1 == 0:
            result[j] = result[j] + 1
          else:
            result[j-1] = result[j-1] + 1
          break
    else:
      j = i
      count = 0
      for j in range(j-1,-1,-1):
        count = count + 1
        if S[j] == "R":
          if count & 1 == 0:
            result[j] = result[j] + 1
          else:
            result[j+1] = result[j+1] + 1
          break
  return " ".join(map(str, result))
          
print(solve())

### FIXED CODE ###

s=input()
ans=[0]*len(s)
import itertools
i=0
for key,g in itertools.groupby(s):
  num=len(list(g))
  if key=="R":
    i+=num
    ans[i-1]+=(num//2+(num%2>0))
    ans[i]+=num//2
  elif key=="L":
    ans[i-1]+=num//2
    ans[i]+=(num//2+(num%2>0))
    i+=num
  if i>=len(ans):
    break
print(*ans)
### BUGGY CODE ###

from itertools import combinations
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)

    return divisors


#nの素数判定
def is_prime(n):
    if n == 1:
        return False
    for i in range(2,int(n**0.5)+1):
        if n % i == 0:
            return False
    return True

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

A, B = map(int, input().split())

di_A = make_divisors(A)
di_B = make_divisors(B)

di_C = list(set(di_A) & set(di_B))
if len(di_C)==1:
    print(1)
    exit()

ans = 0
array = []
array_not = []
edges = [[False] * len(di_C) for _ in range(len(di_C))]
for i in range(len(di_C)):
    for j in range(i+1, len(di_C)):
        if gcd(di_C[i], di_C[j])==1:
            edges[i][j]=True

for num in range(len(di_C), 0, -1):
    fraction = combinations(range(len(di_C)), num)
    for relation in fraction:
        judge = []
        for i, j in combinations(relation, 2):
            judge.append(edges[i][j])
        if all(judge):
            ans = num
            print(ans)
            # 最大派閥議員数が分かればよいので終了
            exit()
print(ans)



### FIXED CODE ###



def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr



A, B = map(int, input().split())

fac_A = factorization(A)
fac_B = factorization(B)

prime_A = [f[0] for f in fac_A]
prime_B = [f[0] for f in fac_B]

if 1 in prime_A: prime_A.remove(1)
if 1 in prime_B: prime_B.remove(1)
common = set(prime_A) & set(prime_B)
print(len(common)+1)
### BUGGY CODE ###

# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math


N,M = map(int,input().split())
a = []
for i in range(M):
    x,y = map(int,input().split())
    a.append([x,y])

ans = False
routes = {}
for i in range(N):
    routes[i+1] = []
for ai in range(len(a)):
    x,y = a[ai]
    if 1 in a[ai]:
        routes[1] = routes[1]+a[ai]
    if N in a[ai]:
        routes[N] = routes[N]+a[ai]

# print(routes)
if 1 in routes[N] or N in routes[1]:
    print("POSSIBLE")
    exit()

s_reach = list(set(routes[1]))
N_reach = list(set(routes[N]))
Ms_reach = list(set(N_reach+s_reach))

if len(s_reach)+len(N_reach) != len(Ms_reach):
    print("POSSIBLE")
    exit()

print("IMPOSSIBLE")


### FIXED CODE ###

# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math


N,M = map(int,input().split())
a = []
for i in range(M):
    x,y = map(int,input().split())
    a.append([x,y])

ans = False
routes_s = []
routes_g = []

for ai in range(len(a)):
    if 1 in a[ai]:
        inx = a[ai].index(1)
        routes_s.append(a[ai][inx-1])
    if N in a[ai]:
        inx = a[ai].index(N)
        routes_g.append(a[ai][inx-1])

# print(routes)
if 1 in routes_g or N in routes_s:
    print("POSSIBLE")
    exit()

s_reach = list(set(routes_s))
g_reach = list(set(routes_g))
sg_reach = list(set(g_reach+s_reach))

if len(s_reach)+len(g_reach) != len(sg_reach):
    print("POSSIBLE")
    exit()

print("IMPOSSIBLE")
### BUGGY CODE ###

#Replacing
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))
Q = int(input())

for i in range(Q):
    B, C = list(map(int, input().split()))
    A = A*( (A==B)==False ) + C*(A==B)
    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 ###

import sys
input = sys.stdin.readline
from collections import deque
n = int(input())
Ps = list(map(int, input().split()))
Ps = [p-1 for p in Ps]
fs = [0] * (n**2)

## フロアフィールドを作る
for x in range(n**2):
    i,j = divmod(x,n)
    x = min(i,n-1-i)
    y = min(j,n-1-j)
    fs[i*n+j] = min(x,y)

ans = 0
mvs = [1, -1, n, -n]
occupied = [1] * (n**2)
for p in Ps:
    ans += fs[p]
    occupied[p] = 0
    q = deque([])
    q.append(p)
    while len(q) > 0: #pを起点にBFS
        p_ = q.popleft()
        dist = fs[p_]
        for mv in mvs:
            if 0 <= p_+mv < n**2:
                p_next = p_ + mv
                # ある位置h,wが
                # occupiedなら、隣接マス+1で到達
                # vacantなら、隣接マス+0で到達
                if dist+occupied[p_] < fs[p_next]:
                    q.append(p_next)
                    fs[p_next] = dist + occupied[p_]

print(ans)
    
    

### FIXED CODE ###

from collections import deque
n = int(input())
Ps = list(map(int, input().split()))
Ps = [p-1 for p in Ps]
fs = [0] * (n**2)

## フロアフィールドを作る
for x in range(n**2):
    i,j = divmod(x,n)
    x = min(i,n-1-i)
    y = min(j,n-1-j)
    fs[i*n+j] = min(x,y)

ans = 0
mvs = [1, -1, n, -n]
occupied = [1] * (n**2)
for p in Ps:
    ans += fs[p]
    occupied[p] = 0
    q = deque([])
    q.append(p)
    while len(q) > 0: #pを起点にBFS
        p_ = q.popleft()
        dist = fs[p_]
        for mv in mvs:
            if 0 <= p_+mv < n**2:
                p_next = p_ + mv
                # ある位置h,wが
                # occupiedなら、隣接マス+1で到達
                # vacantなら、隣接マス+0で到達
                if dist+occupied[p_] < fs[p_next]:
                    q.append(p_next)
                    fs[p_next] = dist + occupied[p_]

print(ans)