import numpy as np
from collections import deque
 
n = int(input())
od = [int(num)-1 for num in input().split()]

current = np.array([[ 1 for i in range(n)] for j in range(n)]) 
mkip = np.array([[ -1 for i in range(n)] for j in range(n)])

for i in range(n):
  for j in range(n):
    cand = [i, j, n-1-i, n-1-j]
    minval = min(cand)
    mkip[i][j] = minval

def valid(x, y):
  return 0 <= x and x < n and 0 <= y and y < n

def update(current, mkip, x, y):
  current[x][y] = 0
  q = deque([])
  q.append((x, y))
  while len(q) > 0:
    x, y = q.popleft()
    if valid(x+1, y) and mkip[x+1][y] > mkip[x][y] + current[x][y]:
        mkip[x+1][y] = mkip[x][y] + current[x][y]
        q.append((x+1, y))
        
    if valid(x, y+1) and mkip[x][y+1] > mkip[x][y] + current[x][y]:
        mkip[x][y+1] = mkip[x][y] + current[x][y]
        q.append((x, y+1))
        
    if valid(x-1, y) and mkip[x-1][y] > mkip[x][y] + current[x][y]:
        mkip[x-1][y] = mkip[x][y] + current[x][y]
        q.append((x-1, y))

    if valid(x, y-1) and mkip[x][y-1] > mkip[x][y] + current[x][y]:
        mkip[x][y-1] = mkip[x][y] + current[x][y]
        q.append((x, y-1))
        
  return current, mkip
        
count = 0
for i in range(len(od)):
  x, y = int(od[i] / n), od[i] % n
  count += mkip[x][y]
  current, mkip = update(current, mkip, x, y)
  
  
print(count)