#!/usr/bin/ruby
tri=(0..9999).map{|e|e*(e+1)/2}
while (n=gets.to_i)>0
	a=gets.split.map(&:to_i)
	s=a.reduce(:+)
	idx=tri.index(s)
	if idx
		ans=[*1..idx]
		i=0
		while a!=ans
			k=a.length
			a.map!{|e|e-=1}
			a<<k
			a.delete_if{|e|e==0}
			i+=1
			break if i>10000
		end
		p i>10000 ? -1 : i
	else
		p -1
	end
end