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

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

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

  ans = 0
  for pi in p:
    y,x = divmod(pi-1,n)
    ans += m[y*n+x]
    #ans += m[y][x]
    exist[y*n+x] = 0
    #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)*n+(qx+dx)] > m[qy*n+qx]+exist[qy*n+qx]:
          m[(qy+dy)*n+(qx+dx)] = m[qy*n+qx]+exist[qy*n+qx]
          q.append((qy+dy,qx+dx))
        #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()
