#include<bits/stdc++.h>
#define int long long
#define N 100010
#define re register 
using namespace std;
int n,m,p[N],q[N],mi,tmp; 
vector<int> a[N],b[N];
char s[N]; 
template <class T> inline void read(T &x)
{
	x=0;int g=1;char s=getchar();
	for (;s<'0'||s>'9';s=getchar()) if (s=='-') g=-1;
	for (;s>='0'&&s<='9';s=getchar()) x=(x<<1)+(x<<3)+(s^48);
	x*=g;
}
signed main()
{
	re int i,j,x,z,op;
	read(n); 
	scanf("%s",s+1);
	if (s[1]==s[n])
	{
		if (n<=3) {printf("-1");return 0;} 
		for (i=2;i<n-1;i++)
		if (s[i]!=s[1]&&s[i+1]!=s[1]) 
		{
			printf("2");
			return 0;
		}
		printf("-1"); 
	}
	else
		printf("1");
	return 0;
}
#include<bits/stdc++.h>
#define pb push_back
#define eb emplace_back
using namespace std;

//#define DEBUG
#ifdef DEBUG
template<typename ...Args>
int debug(const Args &...args){
	return fprintf(stderr,args...);
}
#else
#define debug(...) void()
#endif

typedef unsigned long long ull;
typedef unsigned uint;
typedef long long ll;
#define G getchar()
int read()
{
	int x=0; bool flg=false; char ch=G;
	for (;!isdigit(ch);ch=G) if (ch=='-') flg=true;
	for (;isdigit(ch);ch=G) x=(x<<3)+(x<<1)+(ch^48);
	return flg?-x:x;
}
#undef G
#define fi first
#define se second
/*
const int mod=;
inline int upd(const int &x){return x+(x>>31&mod);}
inline void add(int &x,const int &y){x=upd(x+y-mod);}
inline void iadd(int &x,const int &y){x=upd(x-y);}
int qpow(int x,int y){
	int res=1;
	for (;y;y>>=1,x=1LL*x*x%mod)
		if (y&1) res=1LL*res*x%mod;
	return res;
}
*/
//typedef pair<int,int> P;
#define rep(i,l,r) for (int i(l),_##i(r);i<=_##i;i++)
#define per(i,l,r) for (int i(r),_##i(l);i>=_##i;i--)
#define all(x) (x).begin(),(x).end()
#define forall(x,y) for (const int &y: e[x])

int n; char s[100010];
int dp[26];
void chk(int &x,int y){
	if (y<x) x=y;
}
int solve(){
	scanf("%d%s",&n,s+1);
	memset(dp,0x3f,sizeof dp);
	dp[s[n]-'a']=0;
	per(i,1,n){
		int t=1e9;
		rep(j,0,25) if (j+'a'!=s[i]) t=min(t,dp[j]);
		if (t==1e9) continue;
		if (i==1) return t+1;
		chk(dp[s[i-1]-'a'],t+1);
	}
	return -1;
}

int main()
{
	for (int T=1;T--;) printf("%d\n",solve());
	return 0;
}