#include<bits/stdc++.h>
#define ll long long
#define N 200015
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define fi first
#define se second
#define lowbit(i) ((i)&(-i))
#define VI vector<int>
#define all(x) x.begin(),x.end()
#define SZ(x) ((int)x.size())
using namespace std;
int n;
char s[N];
int main(){
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	scanf("%d",&n);
	scanf("%s",s+1);
	if(s[1] != s[n]) return puts("1"),0;
	else{
		rep(i,2,n-2) 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;

int n;
string s;

int main() {
    cin >> n >> s;
    if (s[0] != s[n - 1]) {
        cout << 1 << endl;
    } else {
        for (int i = 1; i + 1 < n - 1; i++) {
            if (s[i] != s[0] && s[i + 1] != s[n - 1]) cout << 2 << endl, exit(0);
        }
        if (n % 2 == 1) cout << -1 << endl, exit(0);
        bool flag = 1;
        for (int i = 0; i < n; i += 2) {
            flag &= (s[i] != s[i + 1]);
        }
        if (flag) cout << n / 2 << endl;
        else cout << -1 << endl;
    }
    return 0;
}