N = gets.to_i A = gets.split.map &:to_i width = (A.sum**0.5).floor*2-1 add = sub = 0 A.shift(width).map.with_index{|n, i| height = [width - i, i+1].min diff = height - n if diff > 0 add += diff else sub -= diff end } p (add - sub).abs + [add, sub].min