N = int(input())
P = list(map(int, input().split()))

L = [[min([i, j, N - 1 - i, N - 1 - j]) for j in range(N)] for i in range(N)]
M = N * N
V = [0] * (N * N + 1)
for i in range(1, N * N + 1):
  w = (i - 1) % N
  h = (i - w - 1) // N
  t = 0
  if w != 0:
    t |= 1 << (h * N + w)
  if w != N - 1:
    t |= 1 << (h * N + w + 2)
  if h != 0:
    t |= 1 << ((h - 1) * N + w + 1)
  if h != N - 1:
    t |= 1 << ((h + 1) * N + w + 1)
  V[i] = t

ans = 0
for i in P:
  w = (i - 1) % N
  h = (i - w - 1) // N
  ans += L[h][w]
  for j in range(1, M + 1):
    if V[i] & (1 << j):
      ww = (j - 1) % N
      hh = (j - ww - 1) // N
      if L[hh][ww] > L[h][w]:
        L[hh][ww] = L[h][w]
      V[j] |= V[i]
  L[h][w] = 0

print(ans)
