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**2)]
c = 0
for pi in p:
  b[pi-1] = False
  c += m[pi-1]
  q = queue.Queue()
  q.put(pi-1)
  while not q.empty():
    ni = q.get()
    for ai in [-1,+1,-n,+n]:
      if 0<=ni+ai and ni+ai<=n**2-1:
        if b[ni] == True:
          if m[ni+ai] > m[ni]+1:
            m[ni+ai] = m[ni] + 1
            q.put(ni+ai)
        else:
          if m[ni+ai] > m[ni]:
            m[ni+ai] = m[ni]
            q.put(ni+ai)    
print(c) 