#include int ri() { int n; scanf("%d", &n); return n; } int main() { int n = ri(); int a[n]; for (auto &i : a) i = ri() - 1; std::vector compressed; for (int i = 0; i < n; i++) if (!i || a[i - 1] != a[i]) compressed.push_back(a[i]); bool possible = true; int cnt[n]; for (auto &i : cnt) i = -1; std::vector twos; for (int i = 0; i < (int) compressed.size(); i++) { if (cnt[compressed[i]] == -2) possible = false; else if (cnt[compressed[i]] == -1) cnt[compressed[i]] = i; else twos.push_back(i), twos.push_back(cnt[compressed[i]]), cnt[compressed[i]] = -2; } for (auto i : twos) if (i && i + 1 < (int) compressed.size()) possible = false; printf("%s", possible ? twos.size() ? "1" : "0" : "-1"); return 0; }