#include <bits/stdc++.h>

#define ll long long
#define pushb push_back
#define popb pop_back
#define min_ min_element
#define max_ max_element
#define mp make_pair
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define sz(x) (int)(x).size()
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define vli vector<long long>
#define vvi vector<vector<int>>
#define pi pair<int,int>
#define vpi vector<pair<int,int>>
#define vs vector<string>
#define mem(name,val) memset(name,val,sizeof(name))
#define precise fixed<<setprecision(9)

using namespace std;

#define fast ios::sync_with_stdio(false);cin.tie(0);

const ll int INF= 1e18;
const ll int MOD= 1000000007;
const int MX= 100005;
const long double pie= 2*acos(0.0);
vector<ll int> dp(MX,0);

//  check the limits Chutiye
//  read the question correctly ( ll int vs int )

int main() 
{
    fast
    //for fast i/o for cp remember "\n" does for next line but it do not flush out the stream
    int test_c=1;
    //cin>>test_c;
    while(test_c--)
    {
        int i,j;
        int n;
        cin>>n;
        string s;
        cin>>s;
        if(s[0]!=s[n-1])
        {
            cout<<1;
            continue;
        }
        int flag=0;
        char a=s[0];
        for(i=2;i<n;i++)
        {
            if(s[i]!=a && s[i-1]!=a)
            {
                flag++;
                break;
            }
        }
        if(flag)
            cout<<2;
        else
            cout<<-1;
        
        
        
    }
	return 0;
}

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

#define rep( i, a, b ) for( int i = (a) ; i <= (b) ; i ++ )
#define per( i, a, b ) for( int i = (a) ; i >= (b) ; i -- )

typedef long long LL;

template<typename _T>
void read( _T &x )
{
    x = 0; char s = getchar(); int f = 1;
    while( s < '0' || '9' < s ) { f = 1; if( s == '-' ) f = -1; s = getchar(); }
    while( '0' <= s && s <= '9' ) { x = ( x << 3 ) + ( x << 1 ) + ( s - '0' ), s = getchar(); }
    x *= f;
}

template<typename _T>
void write( _T x )
{
    if( x < 0 ) putchar( '-' ), x = -x;
    if( 9 < x ) write( x / 10 );
    putchar( x % 10 + '0' );
}

template<typename _T>
inline _T MAX( const _T a, const _T b )
{
	return a > b ? a : b;
}

template<typename _T>
inline _T MIN( const _T a, const _T b )
{
	return a < b ? a : b;
}

template<typename _T>
inline _T ABS( const _T a )
{
	return a < 0 ? -a : a;
}

const int MAXN = 1e5 + 5;

char str[MAXN];
int N;
bool app[30];

int main()
{
	read( N );
	scanf( "%s", str + 1 );
	if( str[1] ^ str[N] ) return puts( "1" ), 0;
	bool flg = false;
	rep( i, 2, N - 2 )
		if( str[i] != str[1] && str[i + 1] != str[1] )
		{ flg = true; break; }
	puts( flg ? "2" : "-1" );
	return 0;
}