N=gets.to_i
X=(0...N*N).map{|i|[i%N,i/N,N-i%N-1,N-i/N-1].min}
Y=[0]*N*N
ans=0
gets.split.map{|e|x=e.to_i-1
ans+=X[x]
Y[x]=1
q=[x]
while q[0]do
  x=q.shift
  c=X[x]+1-Y[x]
  if x>=N&&X[x-N]>c then
    X[x-N]=c
    q<<x-N
  end
  if x+N<N*N&&X[x+N]>c then
    X[x+N]=c
    q<<x+N
  end
  if x%N>0&&X[x-1]>c then
    X[x-1]=c
    q<<x-1
  end
  if x%N+1<N&&X[x+1]>c then
    X[x+1]=c
    q<<x+1
  end
end}
p ans
