from collections import deque

def main():
  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+1)] for j in range(N+1)]
  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
        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)

if __name__ == "__main__":
  main()