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

ll inf = -1e12;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	
	int n; string str;
	cin >> n >> str;
	
	if(str[0] != str[n-1]){
		cout << 1;
		return 0;
	}
	
	for(int i=1; i<=n-3; i++){
		if(str[i] != str[0] && str[i+1] != str[0]){
			cout << 2;
			return 0;
		}
	}
	cout << -1;
	
	return 0;
}
#include<bits/stdc++.h>
using namespace std ;
#define Next( i, x ) for( int i = head[x]; i; i = e[i].next )
#define rep( i, s, t ) for( int i = (s); i <= (t); ++ i )
#define drep( i, s, t ) for( int i = (t); i >= (s); -- i )
#define mp make_pair
#define pi pair<int, int>
#define pb push_back
#define vi vector<int>
int gi() {
	char cc = getchar() ; int cn = 0, flus = 1 ;
	while( cc < '0' || cc > '9' ) {  if( cc == '-' ) flus = - flus ; cc = getchar() ; }
	while( cc >= '0' && cc <= '9' )  cn = cn * 10 + cc - '0', cc = getchar() ;
	return cn * flus ;
}
const int N = 1e5 + 5 ; 
int n ; 
char s[N] ;  
signed main() {
	n = gi() ; 
	cin >> s + 1 ; 
	n = strlen(s + 1) ; 
	if(s[1] != s[n]) {
		puts("1") ; exit(0) ; 
	}
	for(int i = 2; i < n; ++ i) {
		if(s[i] != s[1] && s[i + 1] != s[1]) {
			puts("2") ; exit(0) ; 
		}
	}
	puts("-1") ; 
	return 0 ;
}