
def main(n,p):
  sheet=[min(i%n,i//n,n-i%n-1,n-i//n-1) for i in range(n**2)]
  space=[1]*(n**2)
  ans=0
  for x in p:
    x-=1
    ans+=sheet[x]
    space[x]=0
    todo=[(x,sheet[x])]
    while todo:
      v,c=todo.pop()
      x,y=divmod(v,n)
      for dx,dy in ((0,1),(0,-1),(1,0),(-1,0)):
        if 0<=x+dx<n and 0<=y+dy<n:
          nv=(x+dx)*n+(y+dy)
          if sheet[nv]>c:
            sheet[nv]=c
            todo.append((nv,c+space[nv]))
  print(ans)

n=int(input())
p=list(map(int,input().split()))
main(n,p)