#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int MAXN = 1e5 + 5;

char s[MAXN];

int main(void)
{
	int n;
	scanf("%d%s",&n,s+1);
	
	if(s[1] != s[n]) return printf("1"), 0;
	for(int i=1; i<n; ++i)
		if(s[i] != s[1] && s[i+1] != s[1])
			return printf("2"), 0;
	printf("-1");
	return 0;
}
                                                     /*CP is fun!*/
#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define int long long
using namespace std;
const int MAXN=2e5+5;
const int MOD=1e9+7;
const int MAX=205;

ll powerm(ll a,ll b,ll m) { //m-> mod
    if(b==0) {
        return 1;
    }
    ll res=powerm(a,b/2,m);
    if(b%2) {
        return ((res*res)%m*(a%m))%m;
    }
    return (res*res)%m;
}

//INVERSE -> a^(m-2) ...m -> prime

int power(ll a,ll b) {
    if(b==0) {
        return 1;
    }
    ll res=power(a,b/2);
    if(b%2==0) {
        return res*res;
    }
    return res*res*a;
}

/*vector<int>fac(1e5);

void facm(ll m) { //factorial mod precompute
    fac[0]=1;
    for(int i=1;i<fac.size();i++) { //name an array fac
        fac[i]=((i)%m*fac[i-1])%m;
    }
    return;
}

ll choose(ll a,ll b,ll m) { //a choose b mod m
    if(a<b) {
        return 1;
    }
    ll ans=((fac[a]*(powerm(fac[a-b],m-2,m)))%m*powerm(fac[b],m-2,m))%m;
    return ans;
}*/



signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //cout<<setprecision(9)<<fixed;
    //freopen("diamond.in","r",stdin);
    //freopen("diamond.out","w",stdout);
    int n;
    cin>>n;
    string s;
    cin>>s;
    s.pb('z');
    int dp[n+1][26][2];
    for(int j=0;j<26;j++) {
        dp[n][j][0]=0;
        dp[n][j][1]=1e15;
    }
    for(int i=n-1;i>=0;i--) {
        for(int j=0;j<26;j++) {
            dp[i][j][0]=dp[i+1][j][1];
            if(s[i]!=j+'a') {
                dp[i][j][1]=min(1+dp[i+1][s[i+1]-'a'][0],dp[i+1][j][1]);
            } else {
                dp[i][j][1]=dp[i+1][j][1];
            }
        }
    }
    if(dp[0][s[0]-'a'][0]>=1e10) {
        cout<<-1;
    } else {
        cout<<dp[0][s[0]-'a'][0];
    }
    return 0;
}
