def dfs(c,v,sheet,space,n):
  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)+(y+dy)*n
      if sheet[nv]>c:
        sheet[nv]=c
        dfs(c+space[nv],nv,sheet,space,n)

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)
  #for i in range(n):
  #  print(sheet[i*n:(i+1)*n])
  ans=0
  for x in p:
    x-=1
    ans+=sheet[x]
    space[x]=0
    dfs(sheet[x],x,sheet,space,n)
  print(ans)

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