import queue
class gridMan:
  def __init__(s, L):
    s.L = L
    s.H = len(s.L)
    s.W = len(s.L[0])
  def makeWall(s, x = -2):
    w = len(s.L[0]) + 2
    s.L = [[x] * w] + [[x] + i + [x] for i in s.L] + [[x] * w]
  def dist(s, S): 
    ans = min(S[0] - 1, S[1] - 1)
    ans = min(ans, N - S[0], N - S[1])
    T = [[-1] * (s.W + 2) for _ in range(s.H + 2)]
    q = queue.Queue()
    q.put([S[0], S[1]])
    T[S[0]][S[1]] = 0
    k = 0
    while not q.empty():
      h, w = q.get()
      k = T[h][w]
      for i, j in [[1, 0], [-1, 0], [0, 1], [0, -1]]:
        hh = h + i
        ww = w + j
        if s.L[hh][ww] == -2:
          ans = min(ans, k)
          if ans == 0:
            return ans
          continue
        kk = k + s.L[hh][ww]
        if kk > ans: continue
        if T[hh][ww] > kk or T[hh][ww] == -1:
          q.put([hh, ww])
          T[hh][ww] = kk
    return ans


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

L = [[1 for j in range(N)] for i in range(N)]


G = gridMan(L)
G.makeWall()

ans = 0
for i in P:
  w = (i - 1) % N
  h = (i - w - 1) // N
  S = [h + 1, w + 1]
  t = G.dist(S)
  if t == 0:
    G.L[h + 1][w + 1] = -2
  else:
    ans += t
    G.L[h + 1][w + 1] = 0
print(ans)
