### constant

MAX_C = 10000

### main

while true
  n = gets.strip.to_i
  break if n == 0

  bl = gets.strip.split(' ').map{|s| s.to_i}
  bsum = bl.inject(0){|s, b| s + b }
  #p bl
  #p bsum

  goal = []
  k = 1
  sum = 0
  while sum < bsum
    sum += k
    goal << k
    k += 1
  end
  #p sum

  count = 0
  while bl != goal && count <= MAX_C
    l = bl.length
    bl = bl.map{|b| b - 1}.select{|b| b > 0}
    bl << l
    count += 1
  end

  puts (count > MAX_C ? -1 : count)
end