#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); ++i)
using ll = long long;

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

    if(s.front() != s.back()) return 1;

    char a = s.front();
    for(int i = 1; i < n - 2; ++i) {
        if(s[i] != a && s[i + 1] != a) return 2;
    }

    return -1;
}

int main() {
    cout << solve() << '\n';
    return 0;
}

#include<iostream>
#include<cstdio>
#include<cstring>
#define re register
#define X first
#define Y second
#define mp std::make_pair
#define pb push_back
#define ohh(hhh...) fprintf(stderr,hhh)
typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int,int> pii;
template<class T1> inline bool cmax(T1 &x,T1 y) {return x<y?(x=y,1):0;}
template<class T1> inline bool cmin(T1 &x,T1 y) {return x>y?(x=y,1):0;}
template<class T1> inline T1 dmax(T1 x,T1 y) {return x>y?x:y;}
template<class T1> inline T1 dmin(T1 x,T1 y) {return x<y?x:y;}
int n;
bool flag;
char s[100010];
int read()
{
	int x=0,w=0;
	char ch=0;
	while(ch<'0'||ch>'9') w|=ch=='-',ch=getchar();
	while('0'<=ch&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
	return w?-x:x;
}
int main()
{
	n=read();
	scanf("%s",s+1);
	if(s[1]!=s[n]) {printf("1\n");return 0;}
	for(re int i=2;i<n-1;i++) if(s[i]!=s[1]&&s[i+1]!=s[1]) flag=1;
	if(flag) printf("2\n");
	else printf("-1\n");
	return 0;
}