//Sub2;-;
#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define all(x) x.begin(), x.end()
using namespace std;
const int maxn = 1e6 + 5;
int A[maxn];
map<int, vector<int>> mp;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n;
int ma = INT_MIN, mi = INT_MAX;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> A[i];
ma = max(ma, A[i]);
mi = min(mi, A[i]);
}
int res = INT_MAX;
if (ma == mi) {
for (int i = 1; i <= n; ++i) {
if (A[i] == ma) {
mp[ma].push_back(i);
}
}
for (int i = 1; i < mp[ma].size(); ++i) {
res = min(res, mp[ma][i] - mp[ma][i - 1]);
}
}
else {
for (int i = 1; i <= n; ++i) {
if (A[i] == ma) {
mp[ma].push_back(i);
}
if (A[i] == mi) {
mp[mi].push_back(i);
}
}
for (int i = 0; i < mp[ma].size(); ++i) {
int it = lower_bound(mp[mi].begin(), mp[mi].end(), mp[ma][i]) - mp[mi].begin();
if (it != mp[mi].size()) {
res = min(res, abs(mp[mi][it] - mp[ma][i] + 1));
}
}
for (int i = 0; i < mp[mi].size(); ++i) {
int it = lower_bound(mp[ma].begin(), mp[ma].end(), mp[mi][i]) - mp[ma].begin();
if (it != mp[ma].size()) {
res = min(res, abs(mp[ma][it] - mp[mi][i] + 1));
}
}
}
cout << res;
}
Ly9TdWIyOy07CiNpbmNsdWRlPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgdWxsIHVuc2lnbmVkIGxvbmcgbG9uZwojZGVmaW5lIGxsIGxvbmcgbG9uZwojZGVmaW5lIGFsbCh4KSB4LmJlZ2luKCksIHguZW5kKCkKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKY29uc3QgaW50IG1heG4gPSAxZTYgKyA1OwppbnQgQVttYXhuXTsKbWFwPGludCwgdmVjdG9yPGludD4+IG1wOwppbnQgbWFpbigpIHsKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oMCk7IGNpbi50aWUoMCk7IGNvdXQudGllKDApOwogICAgaW50IG47CiAgICBpbnQgbWEgPSBJTlRfTUlOLCBtaSA9IElOVF9NQVg7CiAgICBjaW4gPj4gbjsKICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IG47ICsraSkgewogICAgICAgIGNpbiA+PiBBW2ldOwogICAgICAgIG1hID0gbWF4KG1hLCBBW2ldKTsKICAgICAgICBtaSA9IG1pbihtaSwgQVtpXSk7CiAgICB9CiAgICBpbnQgcmVzID0gSU5UX01BWDsKICAgIGlmIChtYSA9PSBtaSkgewogICAgICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IG47ICsraSkgewogICAgICAgICAgICBpZiAoQVtpXSA9PSBtYSkgewogICAgICAgICAgICAgICAgbXBbbWFdLnB1c2hfYmFjayhpKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBmb3IgKGludCBpID0gMTsgaSA8IG1wW21hXS5zaXplKCk7ICsraSkgewogICAgICAgICAgICByZXMgPSBtaW4ocmVzLCBtcFttYV1baV0gLSBtcFttYV1baSAtIDFdKTsKICAgICAgICB9CiAgICB9CiAgICBlbHNlIHsKICAgICAgICBmb3IgKGludCBpID0gMTsgaSA8PSBuOyArK2kpIHsKICAgICAgICAgICAgaWYgKEFbaV0gPT0gbWEpIHsKICAgICAgICAgICAgICAgIG1wW21hXS5wdXNoX2JhY2soaSk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKEFbaV0gPT0gbWkpIHsKICAgICAgICAgICAgICAgIG1wW21pXS5wdXNoX2JhY2soaSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBtcFttYV0uc2l6ZSgpOyArK2kpIHsKICAgICAgICAgICAgaW50IGl0ID0gbG93ZXJfYm91bmQobXBbbWldLmJlZ2luKCksIG1wW21pXS5lbmQoKSwgbXBbbWFdW2ldKSAtIG1wW21pXS5iZWdpbigpOwogICAgICAgICAgICBpZiAoaXQgIT0gbXBbbWldLnNpemUoKSkgewogICAgICAgICAgICAgICAgcmVzID0gbWluKHJlcywgYWJzKG1wW21pXVtpdF0gLSBtcFttYV1baV0gKyAxKSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBtcFttaV0uc2l6ZSgpOyArK2kpIHsKICAgICAgICAgICAgaW50IGl0ID0gbG93ZXJfYm91bmQobXBbbWFdLmJlZ2luKCksIG1wW21hXS5lbmQoKSwgbXBbbWldW2ldKSAtIG1wW21hXS5iZWdpbigpOwogICAgICAgICAgICBpZiAoaXQgIT0gbXBbbWFdLnNpemUoKSkgewogICAgICAgICAgICAgICAgcmVzID0gbWluKHJlcywgYWJzKG1wW21hXVtpdF0gLSBtcFttaV1baV0gKyAxKSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICBjb3V0IDw8IHJlczsKCn0KCg==