#include <bits/stdc++.h>
using namespace std;
#define int long long
int N;
char S[100'100];
int dp[100'100];
int mn[222];
signed main()
{
    cin.tie(0)->sync_with_stdio(false);
    memset(dp, 0x3F, sizeof dp);
    memset(mn, 0x3F, sizeof mn);
    dp[0] = 0;
    cin>>N;
    cin >>(S + 1);
    for (int i = 1; i <= N; ++i)
    {
        for (char c = 'a'; c <= 'z';++c)
        {
            if (c == S[i]) continue;
            dp[i] = min(dp[i], mn[c] + 1);
        }
        mn[S[i]] = min(mn[S[i]], dp[i - 1]);
    }
    if (dp[N] >= 1e9) cout << "-1\n";
    else cout << dp[N] << '\n';
    return 0;
}
#include <bits/stdc++.h>
const int N = 1e5 + 5;
int n; char str[N];
int main() {
	scanf("%d%s", &n, str + 1);
	if (str[1] != str[n]) puts("1");
	else {
		for (int i = 1; i < n; i++)
			if (str[i] != str[1] && str[i + 1] != str[n]) { puts("2"); return 0; }
		puts("-1");
	}
	return 0;
}