#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define re register
#define pb push_back
#define fi first
#define se second
const int N=1e7+10;
const int mod7=1e9+7;
const int mod=998244353;
const int M=1e7;
const double pi=acos(-1);
void read(int &a)
{
    a=0;int d=1;char ch;
    while(ch=getchar(),ch>'9'||ch<'0')
        if(ch=='-')
            d=-1;
    a=ch^48;
    while(ch=getchar(),ch>='0'&&ch<='9')
        a=(a<<3)+(a<<1)+(ch^48);
    a*=d;
}
void read(ll &a)
{
    a=0;int d=1;char ch;
    while(ch=getchar(),ch>'9'||ch<'0')
        if(ch=='-')
            d=-1;
    a=ch^48;
    while(ch=getchar(),ch>='0'&&ch<='9')
        a=(a<<3)+(a<<1)+(ch^48);
    a*=d;
}
int main()
{
	int n;read(n);
	printf("%d\n",n-1);
	return 0;
}
#include <bits/stdc++.h>
#define REP(i, a, b) for (int i = a; i <= b; ++i)
#define ll long long
#define N 100010
using namespace std;

int n, cnt;
bool f;
char ch, a[N], b[N];

inline int read(){
	int s = 0, w = 1;
	char ch = getchar();
	for (; ch < '0' || ch > '9'; w *= ch == '-' ? -1 : 1, ch = getchar());
	for (; ch >= '0' && ch <= '9'; s = s * 10 + ch - '0', ch = getchar());
	return s * w;
}

int main(){
	n = read();
	for (int i = 1; i <= n + 1; ++i){
		cin >> a[i];
		if (a[i] != a[i - 1]) b[++cnt] = a[i];
		if (a[i] == a[i - 1] && b[cnt] != b[cnt - 1]) b[++cnt] = a[i];
	}
	cnt--;
	if (b[1] != b[cnt]){
		puts("1");
		exit(0);
	}
	ch = b[1];
	f = 0;
	for (int i = 1; i <= cnt; ++i){
		if (ch == b[i]) f = 0;
		if (ch != b[i]){
			if (f == 1){
				puts("2");
				exit(0);
			}
			f = 1;
		}
	}
	if (f) puts("1");
	else puts("-1");
	return 0;
}