import java.util.Scanner; 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(); int[] c = new int[n + 1]; c[a[0]]++; for (int i = 1; i < n - 1; i++) { if (a[i] != a[i - 1] && c[a[i]] > 0) { for (int j = i + 1; j < n; j++) { if (a[i] != a[j]) { System.out.println(-1); return; } } System.out.println(1); return; } c[a[i]]++; } if (a[0] == a[n - 1]) { System.out.println(1); } else { System.out.println(0); } } }