def main():
  #n = int(input())
  #p = tuple(map(int,input().split()))
  import sys
  n,*p = map(int,sys.stdin.read().split())

  m = [[0 for _ in range(n)] for _ in range(n)]
  for i in range(n):
    for j in range(n):
      m[i][j] = min(i,n-1-i,j,n-1-j)
  #print(m)

  exist = [[1 for _ in range(n)] for _ in range(n)]

  ans = 0
  for pi in p:
    pi -= 1
    y = pi//n
    x = pi%n
    ans += m[y][x]
    exist[y][x] = 0
    q = [(y,x)]
    while(len(q)>0):
      qy,qx = q.pop()
      for dy,dx in [[1,0],[-1,0],[0,1],[0,-1]]:
        if qy+dy < 0 or qy+dy >= n or qx+dx < 0 or qx+dx >= n:
          continue
        if m[qy+dy][qx+dx] > m[qy][qx]+exist[qy][qx]:
          m[qy+dy][qx+dx] = m[qy][qx]+exist[qy][qx]
          q.append((qy+dy,qx+dx))
  print(ans)

main()