100
92 53 86 41 77 68 80 54 17 96 89 53 64 55 2 80 28 58 77 43 70 91 71 71 78 3 25 2 15 47 60 70 95 19 47 58 57 47 91 2 23 80 86 98 98 98 57 98 98 25 98 98 57 98 98 98 98 98 25 98 98 98 98 98 57 98 98 25 98 98 57 98 98 57 98 98 25 98 98 34 98 98 34 98 98 25 98 98 24 98 98 25 89 34 76 71 91 92 22 13
 20
9 2 12 10 2 9 13 14 11 16 9 1 9 13 5 11 12 19 20 1
 100
77 95 91 95 77 54 91 77 77 42 63 48 97 85 54 63 42 70 42 97 75 77 93 64 56 88 91 85 85 64 64 88 54 70 6 3 20 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 18 48 85 75 97 20 54 64 20 85 56 77 75 42
 20
15 12 19 6 5 11 2 5 18 3 17 15 17 7 9 18 19 17 18 15
 100
92 42 58 62 24 59 62 100 92 62 54 100 62 82 91 62 100 100 62 100 100 62 92 92 92 62 92 92 62 92 62 92 88 100 100 36 62 92 16 92 62 100 62 92 35 62 100 92 92 62 93 100 100 62 76 100 62 62 92 62 92 50 100 8 7 62 100 18 92 62 92 92 100 62 32 92 92 62 37 76 100 39 62 92 62 41 62 62 19 57 100 37 92 99 100 47 62 92 62 62
 100
44 80 26 88 24 37 4 96 23 25 5 5 7 41 54 35 25 57 88 91 20 78 98 64 57 60 86 91 67 63 32 100 91 34 26 41 34 98 5 80 3 57 57 25 42 98 25 88 5 5 24 67 98 34 47 84 62 31 71 91 98 57 35 57 24 34 13 79 2 73 38 57 73 5 98 100 4 23 42 7 25 34 18 91 25 26 53 32 57 25 91 8 4 16 23 91 34 53 42 98
 100
6 20 18 88 48 85 77 70 93 90 91 54 97 75 3 56 63 95 64 42 76 19 52 45 83 96 7 60 71 58 34 2 98 66 69 43 67 46 89 13 81 1 59 8 10 26 14 86 99 4 41 32 92 5 84 65 62 50 36 74 16 73 35 27 9 38 72 23 15 17 30 33 11 53 24 82 79 57 49 21 68 22 39 47 25 61 51 31 6 37 78 94 44 100 29 87 12 55 80 40
 20
13 16 6 13 19 7 18 5 17 8 11 13 19 11 19 18 18 13 14 1
 20
2 3 5 4 3 2 1 2 2 2 2 3 2 5 2 1 1 4 5 3
 100
53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 28 28 28 28 28 28 28 28 28 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 57 57 57 57 57 57 57 57 57 57 57 57 57 96 57 94 94 94 94 94 94 94 94 94 94 94 22 22 22 22 22 22 22 22 22 22 22 22 22 22
 100
94 77 52 53 56 88 23 46 33 28 11 96 68 84 4 91 57 20 98 75 89 83 22 67 80 16 54 41 27 34 62 69 5 50 32 42 45 55 36 59 60 18 37 63 90 1 64 7 81 29 39 93 82 48 43 61 17 66 8 79 2 26 6 44 31 86 40 73 3 65 12 78 74 25 87 95 24 92 13 58 85 76 100 70 38 71 10 19 97 14 47 72 99 35 9 21 51 49 30 15

47
 14
 41
 20
 79
 82
 99
 19
 9
 96
 100

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using db = double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;
template <class T>
using V = vector<T>;
template <class T, size_t SZ>
using AR = array<T, SZ>;
template <class T>
using PR = pair<T, T>;
const int MOD = 1e9 + 7;
const int MX = 2e5 + 5;
const ll INF = 1e18;
const ld PI = acos((ld)-1);
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;
constexpr int pct(int x) { return __builtin_popcount(x); }
constexpr int bits(int x) { return 31 - __builtin_clz(x); }
ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); }
ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }
template <class T>
bool ckmin(T& a, const T& b) {
  return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
  return a < b ? a = b, 1 : 0;
}
template <class T, class U>
T fstTrue(T lo, T hi, U first) {
  hi++;
  assert(lo <= hi);
  while (lo < hi) {
    T mid = lo + (hi - lo) / 2;
    first(mid) ? hi = mid : lo = mid + 1;
  }
  return lo;
}
template <class T, class U>
T lstTrue(T lo, T hi, U first) {
  lo--;
  assert(lo <= hi);
  while (lo < hi) {
    T mid = lo + (hi - lo + 1) / 2;
    first(mid) ? lo = mid : hi = mid - 1;
  }
  return lo;
}
template <class T>
void remDup(vector<T>& v) {
  sort(begin(v), end(v));
  v.erase(unique(begin(v), end(v)), end(v));
}
template <class T, class U>
void erase(T& t, const U& u) {
  auto it = t.find(u);
  assert(it != end(t));
  t.erase(it);
}
template <class T>
void re(complex<T>& c);
template <class T, class U>
void re(pair<T, U>& p);
template <class T>
void re(vector<T>& v);
template <class T, size_t SZ>
void re(AR<T, SZ>& a);
template <class T>
void re(T& x) {
  cin >> x;
}
void re(db& d) {
  str t;
  re(t);
  d = stod(t);
}
void re(ld& d) {
  str t;
  re(t);
  d = stold(t);
}
template <class T, class... U>
void re(T& t, U&... u) {
  re(t);
  re(u...);
}
template <class T>
void re(complex<T>& c) {
  T a, b;
  re(a, b);
  c = {a, b};
}
template <class T, class U>
void re(pair<T, U>& p) {
  re(p.first, p.second);
}
template <class T>
void re(vector<T>& x) {
  for (auto& a : x) re(a);
}
template <class T, size_t SZ>
void re(AR<T, SZ>& x) {
  for (auto& a : x) re(a);
}
template <class T>
void rv(int& n, vector<T>& x) {
  re(n);
  x.resize(n);
  for (auto& a : x) re(a);
}
str to_string(char c) { return str(1, c); }
str to_string(const char* second) { return (str)second; }
str to_string(str second) { return second; }
str to_string(bool b) { return to_string((int)b); }
template <class T>
str to_string(complex<T> c) {
  stringstream ss;
  ss << c;
  return ss.str();
}
str to_string(vector<bool> v) {
  str res = "{";
  for (int i = (0); i < ((int)(v).size()); ++i) res += char('0' + v[i]);
  res += "}";
  return res;
}
template <size_t SZ>
str to_string(bitset<SZ> b) {
  str res = "";
  for (int i = (0); i < (SZ); ++i) res += char('0' + b[i]);
  return res;
}
template <class T, class U>
str to_string(pair<T, U> p);
template <class T>
str to_string(T v) {
  bool fst = 1;
  str res = "";
  for (const auto& x : v) {
    if (!fst) res += " ";
    fst = 0;
    res += to_string(x);
  }
  return res;
}
template <class T, class U>
str to_string(pair<T, U> p) {
  return to_string(p.first) + " " + to_string(p.second);
}
template <class T>
void pr(T x) {
  cout << to_string(x);
}
template <class T, class... U>
void pr(const T& t, const U&... u) {
  pr(t);
  pr(u...);
}
void ps() { pr("\n"); }
template <class T, class... U>
void ps(const T& t, const U&... u) {
  pr(t);
  if (sizeof...(u)) pr(" ");
  ps(u...);
}
void DBG() { cerr << "]" << endl; }
template <class T, class... U>
void DBG(const T& t, const U&... u) {
  cerr << to_string(t);
  if (sizeof...(u)) cerr << ", ";
  DBG(u...);
}
void setIn(str second) { freopen(second.c_str(), "r", stdin); }
void setOut(str second) { freopen(second.c_str(), "w", stdout); }
void unsyncIO() { cin.tie(0)->sync_with_stdio(0); }
void setIO(str second = "") {
  unsyncIO();
  if ((int)(second).size()) {
    setIn(second + ".in"), setOut(second + ".out");
  }
}
int go(vector<int> x, int y) {
  if (y == 0 || (int)x.size() == 0) {
    return 0;
  }
  vector<int> l;
  vector<int> r;
  for (int i = 0; i < x.size(); i++) {
    if ((x[i] & (1 << y)) != 0) {
      l.push_back(x[i]);
    } else {
      r.push_back(x[i]);
    }
  }
  int ret = max((int)l.size() - 1, 0) + go(r, y - 1);
  ret = min(ret, max((int)r.size() - 1, 0) + go(l, y - 1));
  return ret;
}
int main() {
  setIO();
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  int ans = 0;
  vector<int> cnt(n + 1);
  vector<int> freq(n + 1);
  int maxi = 0;
  freq[0] = n;
  int rig = -1;
  for (int i = 0; i < n; i++) {
    freq[cnt[a[i]]]--;
    freq[++cnt[a[i]]]++;
    maxi = max(cnt[a[i]], maxi);
    if (freq[maxi] > 1) {
      rig = i;
    }
  }
  maxi = 0;
  for (int i = 0; i < n; i++) {
    if (cnt[i] > cnt[maxi]) {
      maxi = i;
    }
  }
  if (cnt[maxi] == n) {
    cout << -1 << endl;
    return 0;
  }
  int lo = rig + 1;
  int hi = n;
  vector<int> second(n);
  vector<int> l_cnt(n);
  vector<int> r_cnt(n);
  vector<int> cur(n);
  vector<int> best_prefix(n);
  priority_queue<pi> cur_set;
  while (lo < hi) {
    int mid = (lo + hi + 1) / 2;
    while (cur_set.size() > 0) {
      cur_set.pop();
    }
    int add = 0;
    for (int i = 0; i < n; i++) {
      second[i] = 0;
      l_cnt[i] = 0;
      r_cnt[i] = 0;
      cur[i] = 0;
      best_prefix[i] = 0;
    }
    for (int i = 0; i < mid; i++) {
      r_cnt[a[i]]++;
      if (a[i] == maxi) {
        add++;
      } else {
        second[a[i]]--;
      }
    }
    for (int i = 0; i < n; i++) {
      if (i != maxi) {
        cur[i] = l_cnt[i] - r_cnt[i];
        cur_set.push(make_pair(-cur[i], i));
      }
    }
    bool ok = false;
    for (int i = 0; i < n; i++) {
      second[i] += add;
      if (second[i] == 0) {
        ok = true;
      }
    }
    int aux = -r_cnt[maxi];
    for (int i = 0; i + mid < n; i++) {
      int r = i + mid;
      if (a[r] == maxi) {
        aux--;
        r_cnt[maxi]++;
      } else {
        r_cnt[a[r]]++;
        if (best_prefix[a[r]] >= r_cnt[maxi] - r_cnt[a[r]]) {
          ok = true;
        }
        cur[a[r]] = l_cnt[a[r]] - r_cnt[a[r]];
        cur_set.push(make_pair(-cur[a[r]], a[r]));
      }
      if (a[i] == maxi) {
        aux++;
        l_cnt[maxi]++;
      } else {
        best_prefix[a[i]] = max(best_prefix[a[i]], l_cnt[maxi] - l_cnt[a[i]]);
        l_cnt[a[i]]++;
        cur[a[i]] = l_cnt[a[i]] - r_cnt[a[i]];
        cur_set.push(make_pair(-cur[a[i]], a[i]));
      }
      while (cur_set.size() > 0) {
        auto now = cur_set.top();
        if (cur_set.top().first != -cur[cur_set.top().second]) {
          cur_set.pop();
        } else {
          break;
        }
      }
      int val = -cur_set.top().first;
      ok = (ok || (aux >= val));
    }
    while (cur_set.size() > 0) {
      auto now = cur_set.top();
      if (cur_set.top().first != -cur[cur_set.top().second]) {
        cur_set.pop();
      } else {
        break;
      }
    }
    int val = -cur_set.top().first;
    ok = (ok || (aux >= val));
    if (ok) {
      lo = mid;
    } else {
      hi = mid - 1;
    }
  }
  cout << lo << endl;
}
