import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } sc.close(); Set set = new HashSet<>(); set.add(a[0]); for (int i = 1; i < n - 1; i++) { if (a[i] != a[i - 1] && set.contains(a[i])) { for (int j = i + 1; j < n; j++) { if (a[i] != a[j]) { System.out.println(-1); return; } } System.out.println(1); return; } set.add(a[i]); } if (a[0] == a[n - 1] && set.size() > 1) { System.out.println(1); } else { System.out.println(0); } } }