#include <bits/stdc++.h>
using namespace std;

#define rep(i,a,b) for(int i=(a),i##end=(b);i<=i##end;++i)
#define per(i,a,b) for(int i=(a),i##end=(b);i>=i##end;--i)
//mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
template<typename T>void chkmax(T&x,T y){if(x<y)x=y;}
template<typename T>void chkmin(T&x,T y){if(x>y)x=y;}
inline int read(){
  #define nc getchar()
  bool f=0;int x=0;char c=nc;
  while(c<48)f|=c=='-',c=nc;
  while(c>47)x=x*10+(c^48),c=nc;
  return f?-x:x;
  #undef nc
}

typedef double db;
typedef long long ll;
typedef vector<int>vi;
typedef pair<int,int>pii;
const int maxn=1e5+10;
int n,dp[maxn];char S[maxn];int tmp[26];

void solve(){
  scanf("%d%s",&n,S+1);
  memset(tmp,0x3f,sizeof tmp);
  dp[0]=0;
  tmp[S[1]-'a']=0;
  rep(i,1,n){
    dp[i]=n+1;
    rep(j,0,25)if('a'+j!=S[i]){
      chkmin(dp[i],tmp[j]+1);
    }
    if(i<n)chkmin(tmp[S[i+1]-'a'],dp[i]);
  }
  printf("%d\n",dp[n]>n?-1:dp[n]);
}

signed main(){
//  int T=read();
//  while(T--)solve();
  solve();
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main() 
{
    int n;
    string s;
    cin>>n>>s;
    
    if(s[0]!= s[n-1])
    cout<<1<<"\n";
    else
    {
        bool fg=0;
        for(int i=n-2;i>=0;i--)
        {
            if(s[0]!= s[i] && s[i+1] != s[n-1])
            fg =1;
        }
        if(fg)
        cout<<2<<"\n";
        else
        cout<<-1<<"\n";
    }

    
    
    return 0;
}