#include <bits/stdc++.h>
#include <assert.h>
using namespace std;
const long long MOD1 = 1000000007;
const long long MOD2 = 998244353;
#define logn long
#define lnog long
#define lgon long
#define itn int
typedef long long ll;
typedef pair<long double, long double> P;
const long long INF = 1e18;
int main(){
    long long N; cin >> N;
    string S; cin >> S;
    if (N == 2) {
        if (S[0] == S[1]) cout << -1 << endl;
        else cout << 1 << endl;
        return 0;
    }
    if (N == 3) {
        if (S[0] != S.back()) cout << 1 << endl;
        else cout << -1 << endl;
        return 0;
    }
    if (S[0] != S.back()) {
        cout << 1 << endl;
        return 0;
    }
    char ch = S[0];
    long long ans = 0;
    long long pos = 0;
    for (long long i = 1; i < N - 1; i++) {
        if (S[i] != ch && S[i+1] != ch) {
            cout << 2 << endl;
            return 0;
        }
    }
    cout << -1 << endl;
}
#include<bits/stdc++.h>
using namespace std;

int main () {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    //cin >> t;
    while (t--) {
        int n, inf = 1e9 + 7;
        string s;
        cin >> n >> s;
        vector<int> dp(26, inf);
        dp[s[n - 1] - 'a'] = 0;
        int ans = inf;
        for (int i = n - 2; i >= 0; i--) {
            vector<int> dp1 = dp;
            for (int j = 0; j < 26; j++) {
                if (s[i] - 'a' != j && dp1[j] != inf) {
                    if (i == 0) ans = min(ans, dp1[j] + 1); 
                    else {
                        dp[s[i - 1] - 'a'] = min(dp[s[i - 1] - 'a'], dp1[j] + 1);
                    }
                }
            }
        }
        if (ans == inf) ans = -1;
        cout << ans << '\n';
    }
}