require 'set'; require 'prime'; require 'pp'
INF=Float::INFINITY; MOD=10**9+7
@n = gets.chomp.to_i
a = gets.chomp.split.map(&:to_i)

@sitting = Array.new(@n){Array.new(@n, true)}
@grid = Array.new(@n){Array.new(@n)}
@n.times do |i|
  @n.times do |j|
    @grid[i][j] = [i, j, @n - i - 1, @n - j - 1].min
  end
end


@dy = [0,0,1,-1]
@dx = [1,-1,0,0]
def denpa(y, x)
  q = [[y, x]]
  until q.empty?
    y, x = q.pop
    cur = @grid[y][x]
    cur += 1 if @sitting[y][x]
    4.times do |i|
      ny, nx = y + @dy[i], x + @dx[i]
      if (0 <= ny && ny < @n) && (0 <= nx && nx < @n)
        if cur < @grid[ny][nx]
          @grid[ny][nx] = cur
          q.push [ny, nx]
        end
      end
    end
  end
end

ans = 0
a.each do |num|
  y, x = (num - 1).divmod(@n)
  ans += @grid[y][x] if @grid[y][x] > 0
  # @grid[y][x] -= 1
  @sitting[y][x] = false
  denpa(y, x)
  # @grid.each{|l| p l} if [16,10,9,5,11,12].include? num
  # p '--'
end

puts ans
