from collections import deque

N = int(input())
P = list(map(int, input().split()))
grid = [[1]*N for _ in range(N)]
c = [[min(i, N-1-i, 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) or (nxx==-1) or (nxx==N):
        continue
      if c[nxy][nxx] == 0:
        continue
      cnow = c[nowy][nowx]
      if grid[nowy][nowx] == 0:
        if c[nxy][nxx] > cnow:
          c[nxy][nxx] = cnow
          stack.append([nxy, nxx])
      else:
        if c[nxy][nxx] > cnow+1:
          c[nxy][nxx] = cnow+1
          stack.append([nxy, nxx])

print(ans)
