#include<bits/stdc++.h>
using namespace std;
int n;
string s;
int main(){
	scanf("%d",&n),cin>>s,s=" "+s;
	if(s[1]!=s[n]){puts("1");return 0;}
	for(int i=2;i<n-1;++i)if(s[i]!=s[1]&&s[i+1]!=s[n]){puts("2");return 0;}
	puts("-1");
    return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
#define Fi(a,n) for(int i = a; i < n; i++)
#define Fd(n,a) for(int i = n; i >= a; i--)
#define all(a) a.begin(), a.end()
#define pb push_back
void solve(){
	int n;
	cin >> n;
	string s;
	cin >> s;
	if (s[0] != s[n-1]){
		cout << 1;
		return;
	}
	if (n <= 3)
		cout << -1;
	else{
		vector<int> a(n , 0);
		for (int i = 1; i<n; i++){
			if (s[i] != s[0])
				a[i] = a[i-1] + 1;
		}
		if (a[n-1]!=0)
			cout << 2;
		else{
			int m = 0, c = 0;
			for (int i = 0; i<n; i++){
				if (a[i] == 1)
					c++;
				m = max(m, a[i]);
			}
			if (m > 1)
				cout << 2;
			else{
				
				cout << -1;
			}
		}
	}
}
 
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    solve();
}
