import sys
input = sys.stdin.readline
from collections import deque
n = int(input())
a = list(map(int,input().split()))
ls = [0 for i in range(n*n)]
for i in range(n):
  for j in range(n):
    ls[i*n+j] = min(i,j,n-1-i,n-1-j)
ans = 0
st = set()
for i in a:
  x,y = divmod(i-1,n)
  ans += ls[i-1]
  st.add(i-1)
  q = deque([i-1])
  while q:
    j = q.popleft()
    px,py = divmod(j,n)
    for nx,ny in (px-1,py),(px+1,py),(px,py+1),(px,py-1):
      if 0 <= nx < n and 0 <= ny < n:
        k = nx*n+ny
        if j in st:
          if ls[j] < ls[k]:
            ls[k] = ls[j]
            q.append(k)
        elif ls[j]+1 < ls[j]:
          ls[k] = ls[j]+1
          q.append(k)
print(ans)