#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
 
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define V vector
#define debug(v) {for(auto i: v) cout << i << " "; cout << endl;}
#define vcin(v) rep(i, v.size()) cin >> v[i]
#define wcin(v, w) rep(i, v.size()) cin >>v[i] >>w[i]
ll MOD=1000000007;

int main(){
    ll n;
    cin >>n;
    string s;
    cin >>s;

    if(s[0]!=s[n-1]){
        cout <<1;
        return 0;
    }
    char c=s[0];

    for(ll i=1;i<n-1;i++){
        if(s[i]!=c&&s[i+1]!=c){
            cout <<2;
            return 0;
        }
    }

    cout <<-1;
}
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

typedef   vector<ll>  vi;
typedef   vector<pair<ll, ll> > vii;
typedef   pair<ll,ll> pii; 
typedef   multiset<pair<ll, ll> > msii;
typedef   multiset<ll> msi;
typedef   set<pair<ll, ll> > sii;
typedef   set<ll> si;
typedef   map<ll, ll> mii;

#define boost 	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define for1(x, a, b)  for(x=a; x<b; x++)
#define for2(x, a, b)  for(x=a, x>=b; x--)
//#define endl 	"\n"
#define all(x)  x.begin(), x.end()
#define p       pair
#define mp      make_pair
#define read    freopen("input.txt","r",stdin)
#define write   freopen("output.txt","w",stdout)
#define pb      push_back
#define ff      first
#define ss      second
#define bb      begin
#define arr_ub(arr, n, x) upper_bound(arr, arr+n, x)-arr
#define arr_lb(arr, n, x) lower_bound(arr, arr+n, x)-arr
#define u_p(v, x) upper_bound(v.begin(), v.end(), x)-v.begin()
#define l_b(v, x) lower_bound(v.begin(), v.end(), x)-v.begin()

const ll sz=1e6+123;
#define INF 1000000000000000007
#define MOD 1000000007
#define stringLen 18446744073709551620
#define pi 3.1415926536

inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); }
inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline ll modInverse(ll a) { return modPow(a, MOD-2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

int n;
string s;

void dfs(){

}


int main(){
    cin >> n;
    cin >> s;

    if(s[0]!=s[n-1]){
        cout << 1;
        return 0; 
    }

    vector<int> st;
    for(int i=0; i<n-1; i++){
        if(s[i]==s[0]){
            if(s[i+1]!=s[i] ){
                st.pb(s[i]);
            }
        }else{
            st.pb(s[i]);
        }
    }
    st.pb(s[n-1]);

    bool ok = true;
    int nt = st.size();

    if(nt==1){
        cout<<-1;
        return 0;
    }

    if(nt%2 ==1){
        for(int i=2; i<nt; i+=2){
            if(st[i]!=st[0]){
                ok = false;
                break;
            }
        }
        if(ok){
            cout << -1;
            return 0;
        }
    }


    cout << 2;

    return 0;

    // vector<vector<bool>> Fs(n, vector<bool>(n,false));
    // int first = 0;
    // int init_length=1;
    // int last = first+init_length;
    // // while(init_length<n-2){
    //     int ans = 0;
    //     while(last<n){
    //         if (s[first]!= s[last] && !Fs[last][init_length]){
    //             ans++;
    //             Fs[last][init_length] = true;
    //             first = last+1;
    //             if(s[first]!=s[n-1]){
    //                 cout<<ans+1;
    //                 return 0;
    //             }
    //             last = first+1;
    //         }else{
    //             last++;
    //         }
    //     }
    //     if(first==n){
    //         cout << ans;
    //         return 0;
    //     }

        // init_length++;
        // first = 0;
        // last=first+init_length;
    //}
    // cout << -1;

    // return 0;
}