#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ln "\n"
using namespace std;
//#define dbg(x) cout<<#x<<" = "<<x<<" "
#define rep(i,s,n) for(ll i = (s);i<(n);++i)
#define pc(x) __builtin_popcount(x)
void DBG() {
	cerr << "]" << endl;
}
template<class H, class... T> void DBG(H h, T... t) {
    //cerr << '[' << endl;
	cerr << to_string(h);
	if(sizeof...(t))
		cerr << ", ";
	DBG(t...);
}
#ifdef _DEBUG
#define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) cerr << "[ ]" << endl
#endif

#define pi  pair<int,int>
//const int mod=998244353;

void solve(){  
    ll n;
    cin>>n;
    string s;
    cin>>s;
    if(n==1)
    {
        cout<<1<<ln;
        return;
    }
    ll ans=-1;
    if(s[0]!=s[n-1]) cout<<1<<ln;
    else 
    {
        char s1=s[0];
        for(int i=1;i<n;i++)
        {
            if(s[i]!=s1 && s[i+1]!=s1)
            {
                ans=2;
                break;
            }
        }
        cout<<ans<<ln;
    }
}
 
 
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);  
    //sieve();  
    int test=1;
   // cin>>test;   
    while(test--)
        solve();
    return 0;
}




#include<bits/stdc++.h>
#define mxn 100005
#define inf 1e9
using namespace std;
int n, mem[mxn][26];
char str[mxn];
int dp(int pos, int ch)
{
    if(pos==n)return inf;
    if(pos==n-1 && (str[n-1]-'a')!=ch)return 1;
    int &ret= mem[pos][ch];
    if(ret!=-1)return ret;

    ret= dp(pos+1, ch);
    if((str[pos]-'a')!=ch)ret= min(ret, 1+dp(pos+2, str[pos+1]-'a'));
    return ret;
}
int main()
{
    scanf("%d %s", &n, str);
    memset(mem, -1, sizeof mem);
    int res= dp(1, str[0]-'a');
    if(res==inf)res= -1;
    printf("%d\n", res);

    return 0;
}
