#include <bits/stdc++.h>

int main() {
	using namespace std;
	ios_base::sync_with_stdio(false), cin.tie(nullptr);

	const int INF = 1e9;

	int N; cin >> N;
	string S; cin >> S;
	std::array<int, 26> dp; dp.fill(INF);
	dp[S[0] - 'a'] = 0;
	for (int i = 0; i < int(S.size()); i++) {
		int best = INF;
		for (int v = 0; v < 26; v++) {
			if (v == S[i] - 'a') continue;
			best = min(best, dp[v] + 1);
		}
		if (i+1 < int(S.size())) {
			dp[S[i+1] - 'a'] = min(dp[S[i+1] - 'a'], best);
		} else {
			if (best > N) best = -1;
			cout << best << '\n';
		}
	}

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
using ll = long long int;
using ld = long double;
#define pow(n,m) powl(n,m)
#define sqrt(n) sqrtl(n)
const ll MAX = 5000000000000000000;
const ll MOD = 0;
random_device rd;
mt19937 mt(rd());
int main(){
    ll N;
    string S;
    cin >> N >> S;
    if(S[0] != S[N - 1]){
        cout << 1 << endl;
    }
    else{
        for(ll i = 0;i < N - 1;i++){
            if(S[0] != S[i] && S[0] != S[i + 1]){
                cout << 2 << endl;
                return 0;
            }
        }
        cout << -1 << endl;
    }
}
