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

#define     M                     1000000007
#define     ll                    long long
#define     FIO                   ios_base::sync_with_stdio(false);cin.tie(NULL)
#define     ifor(st,lim)          for(int i=st;i<lim;i++)
#define     jfor(stt,llim)        for(int j=stt;j<llim;j++)
#define     pb                    push_back

void solve() {	
	int n; cin>>n;
	string s; cin>>s;

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

		for(int i=1;i<n-2;i++) {
			if(s[i] == s[i+1] and s[i] != c and s[i+1] != c) {
				cout<<2;
				exit(0);
			}else if(s[i] != s[i+1] and s[i] != c and s[i+1] != c) {
				cout<<2;
				exit(0);
			}
		}
		cout<<-1;
	}
}

void file_i_o() {
	#ifndef ONLINE_JUDGE  
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
	#endif
}
int main(){
	file_i_o();
	FIO;
	int t=1;
	while(t--) {
		solve();
	}
}	
#include <bits/stdc++.h>
using namespace std;
int n;
string s;
int main(){
  cin>>n>>s;
  if(s[0]!=s[n-1]){
    cout<<1<<endl;
    return 0;
  }
  for(int i=1;i<n-2;i++){
    if(s[0]!=s[i]&&s[i+1]!=s[n-1]){
      cout<<2<<endl;
      return 0;
    }
  }
  cout<<-1<<endl;
}