// G. They Are Everywhere
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<vll> vvll;
typedef vector<pll> vpll;
typedef vector<string> vs;
typedef unordered_map<ll, ll> umll;
template<class T>
using pq = priority_queue<T, vector<T>, greater<T>>;
#define io \
ios_base::sync_with_stdio(false); \
cin.tie(nullptr);
void solve() {
int n;
cin >> n;
string s;
cin >> s;
set<char> st;
for (int i = 0; i < n; ++i) {
st.insert(s[i]);
}
int dist = st.size();
int l = 0, r = 0, count = 0, ans = INT_MAX;
map<char, int> mp;
while (l < n) {
if (count == dist) {
ans = min(ans, r-l);
mp[s[l]]--;
if (mp[s[l]] == 0) {
count--;
}
l++;
} else {
if (r >= n) {
break;
}
mp[s[r]]++;
if (mp[s[r]] == 1) {
count++;
}
r++;
}
}
cout << ans;
}
int main() {
io;
ll tests = 1;
// cin >> tests;
while (tests--) {
solve();
}
return 0;
}
Ly8gIEcuIFRoZXkgQXJlIEV2ZXJ5d2hlcmUKCgojcHJhZ21hIEdDQyB0YXJnZXQoImF2eDIiKQojcHJhZ21hIEdDQyBvcHRpbWl6YXRpb24oIk8zIikKI3ByYWdtYSBHQ0Mgb3B0aW1pemF0aW9uKCJ1bnJvbGwtbG9vcHMiKQoKI2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKdHlwZWRlZiBsb25nIGRvdWJsZSBsZDsKdHlwZWRlZiBwYWlyPGxsLCBsbD4gcGxsOwp0eXBlZGVmIHZlY3Rvcjxib29sPiB2YjsKdHlwZWRlZiB2ZWN0b3I8aW50PiB2aTsKdHlwZWRlZiB2ZWN0b3I8bGw+IHZ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