from collections import deque

N = int(input())
P = list(map(int, input().split()))
grid = [[1]*N for _ in range(N)]
c = [[min(min(i, N-1-i), min(j, N-1-j)) for i in range(N)] for j in range(N)]
dx = [1, 0, -1, 0]
dy = [0, -1, 0, 1]

ans = 0
for pi in P:
  pi -= 1
  hi = pi//N
  wi = pi%N
  ans += c[hi][wi]
  grid[hi][wi] = 0
  stack = deque([[hi, wi]])
  while stack:
    nowy, nowx = stack.pop()
    for dxi, dyi in zip(dx, dy):
      nxy = nowy + dyi
      nxx = nowx + dxi
      if (nxy==-1) or (nxy==N):
        continue
      if (nxx==-1) or (nxx==N):
        continue
      if c[nxy][nxx] == 0:
        continue
      cnow = c[nowy][nowx]
      gnow = grid[nowy][nowx]
      if c[nxy][nxx] > cnow + gnow:
        c[nxy][nxx] = cnow + gnow
        stack.append([nxy, nxx])

print(ans)
