import sys
import queue
input = sys.stdin.readline
n = int(input())
p = list(map(int,input().split()))
m = [[min([n-1-i,i,j,n-1-j]) for j in range(n)]for i in range(n)]
b = [[True for _ in range(n)] for _ in range(n)]
c = 0
for pi in p:
  j = (pi-1)%n
  i = (pi-1)//n
  b[i][j] = False
  c += m[i][j]
  q = queue.Queue()
  q.put((i,j))
  while not q.empty():
    ni,nj = q.get()
    for ai,aj in [(0,1),(1,0),(-1,0),(0,-1)]:
      if all([0<=ni+ai,ni+ai<=n-1,0<=nj+aj,nj+aj<=n-1]):
        if b[ni][nj] == True:
          if m[ni+ai][nj+aj] > m[ni][nj]+1:
            m[ni+ai][nj+aj] = m[ni][nj] + 1
            q.put((ni+ai,nj+aj))
        else:
          if m[ni+ai][nj+aj] > m[ni][nj]:
            m[ni+ai][nj+aj] = m[ni][nj]
            q.put((ni+ai,nj+aj))       
print(c) 