n=int(input())
p=list(map(int,input().split()))
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]
  while todo:
    v=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]>sheet[v]+space[v]:
          sheet[nv]=sheet[v]+space[v]
          todo.append(nv)
print(ans)

