n=gets.to_i
ps=gets.split.map &:to_i

w=[[],[],[],[]]
f=4.times.map{ [true]*(n*n) }
c=[1]*(n*n)
n.times{|i|
  f[0][i]=false
  f[1][i*n]=false
  f[2][(n-1)*n+i]=false
  f[3][(i+1)*n-1]=false
  u,d=i,n-1-i
  n.times{|j|
    k=[j,n-1-j].min
    if u<=d
      w[0][i*n+j]=w[1][j*n+i]=[k+1,u].min
    else
      w[0][i*n+j]=w[1][j*n+i]=[k+1,d+2].min
    end
    if d<=u
      w[2][i*n+j]=w[3][j*n+i]=[k+1,d].min
    else
      w[2][i*n+j]=w[3][j*n+i]=[k+1,u+2].min
    end
  }
}
m=(0...n*n).map{|j| (0..3).map{|i| w[i][j] }.min }
ans=0
dir=[-n,-1,n,1]
ps.each{|e|
  e-=1
  ans+=m[e]
  c[e]=0
  q=[e]
  while i=q.pop
    t=m[i]+c[i]
    4.times{|d|
      next unless f[d][i]
      j=i+dir[d]
      w[d^2][j]=t
      if t<m[j]
        m[j]=t
        q.push(j)
      end
    }
  end
}
puts ans