#include <fstream>
#include <string>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <map>
using namespace std;
#ifdef DEBUG
string name = "data";
#else
string name = "secv";
#endif
ifstream fin(name + ".in");
ofstream fout(name + ".out");
int main() {
int n;
fin >> n;
int a[5005];
pair<int, int> best[5005];
map<int, int> p;
set<int> used;
for (int i = 0; i < n; ++i) {
fin >> a[i];
used.insert(a[i]);
}
int last = -1;
int biggest = -1;
for (auto x: used) {
p[x] = last;
last = x;
biggest = max(biggest, x);
}
for (int i = 0; i < n; ++i) {
if (p[a[i]] == -1) {
best[i] = {i, 1};
continue;
}
for (int j = i - 1; j >= 0; --j) {
if (a[j] == p[a[i]] && best[j].second > 0) {
best[i] = {i, best[j].second + i - j};
break;
}
}
}
int res = 432414;
bool found = false;
for (int i = 0; i < n; ++i) {
if (a[i] == biggest && best[i].second > 0) {
res = min(res, best[i].second);
found = true;
}
}
if (found) {
fout << res;
} else {
fout << "-1";
}
return 0;
}