n = gets.to_i a = gets.split.map &:to_i s = a.uniq.size if s == n puts 0 elsif a.first == a.last b = a[a.index { |i| i != a.first }..a.rindex { |i| i != a.last }] puts b.uniq.size == b.size ? 1 : -1 else puts -1 end