N = gets.to_i A = gets.split.map(&:to_i) if N == 1 puts 0 exit end rotate_cnt = 0 while A[0] == A[-1] rotate_cnt += 1 a = A.shift A.push(a) end cnt = 0 (N - 1).times do |i| cnt += 1 if A[i] != A[i + 1] end if cnt == A.uniq.size - 1 if rotate_cnt == 0 puts 0 else puts 1 end else puts -1 end