N = gets.to_i
Ps = gets.split.map(&:to_i)

ds = (0 ... N*N).map{|i| [i%N, i/N, N - 1 - i%N, N - 1 - i/N].min}
as = Array.new(N*N, false)
ans = 0
Ps.map{_1 - 1}.each do |i|
  ans += ds[i]
  as[i] = true
  s = [i]
  while !s.empty?
    j = s.pop
    d = as[j] ? ds[j] : ds[j] + 1
    if j - N >= 0 && ds[j - N] > d
      ds[j - N] = d
      s << j - N
    end
    if j + N < N*N && ds[j + N] > d
      ds[j + N] = d
      s << j + N
    end
    if j%N != 0 && ds[j - 1] > d
      ds[j - 1] = d
      s << j - 1
    end
    if j%N != N - 1 && ds[j + 1] > d
      ds[j + 1] = d
      s << j + 1
    end
  end
end
puts ans