using System; using System.Collections.Generic; using System.Linq; using CompLib.Util; public class Program { public void Solve() { var sc = new Scanner(); int n = sc.NextInt(); int[] a = sc.IntArray(); var g = new List(); int target = a[0]; int cnt = 1; for (int i = 1; i < n; i++) { if (target != a[i]) { g.Add(new Pair(target, cnt)); cnt = 0; target = a[i]; } cnt++; } g.Add(new Pair(target, cnt)); // もともと条件を満たす { var hs = new HashSet(); bool f = true; foreach (var p in g) { f &= hs.Add(p.A); } if (f) { Console.WriteLine(0); return; } } // 先頭と最後が同じ { var hs = new HashSet(); bool f = g[0].A == g[g.Count - 1].A; for (int i = 0; i < g.Count - 1; i++) { f &= hs.Add(g[i].A); } if (f) { Console.WriteLine(1); return; } } Console.WriteLine("-1"); } public static void Main(string[] args) => new Program().Solve(); } struct Pair { public int A, C; public Pair(int a, int c) { A = a; C = c; } } namespace CompLib.Util { using System; using System.Linq; class Scanner { private string[] _line; private int _index; private const char Separator = ' '; public Scanner() { _line = new string[0]; _index = 0; } public string Next() { while (_index >= _line.Length) { _line = Console.ReadLine().Split(Separator); _index = 0; } return _line[_index++]; } public int NextInt() => int.Parse(Next()); public long NextLong() => long.Parse(Next()); public double NextDouble() => double.Parse(Next()); public decimal NextDecimal() => decimal.Parse(Next()); public char NextChar() => Next()[0]; public char[] NextCharArray() => Next().ToCharArray(); public string[] Array() { _line = Console.ReadLine().Split(Separator); _index = _line.Length; return _line; } public int[] IntArray() => Array().Select(int.Parse).ToArray(); public long[] LongArray() => Array().Select(long.Parse).ToArray(); public double[] DoubleArray() => Array().Select(double.Parse).ToArray(); public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray(); } }