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

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
#define FOR(i, n) for(int (i)=0; (i)<(n); (i)++)
#define FOR1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FORI(i, n) for(int (i)=n-1; (i)>=0; (i)--)
template<class T, class U> void umin(T& x, const U& y){ x = min(x, (T)y);}
template<class T, class U> void umax(T& x, const U& y){ x = max(x, (T)y);}
template<class T> void print(T& v){ for(auto it : v) cout << it << " "; cout << endl; }
template<class T, class U> void init(vector<T> &v, U x, size_t n) { v=vector<T>(n, (T)x); }
template<class T, class U, typename... W> void init(vector<T> &v, U x, size_t n, W... m) { v=vector<T>(n);  for(auto& a : v) init(a, x, m...); }

int main(int argc, char** argv) {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(15);
    if (argc == 2 && atoi(argv[1]) == 123456789) freopen("d:\\code\\cpp\\contests\\stdin", "r", stdin);

    int n;
    cin >> n;
    string s;
    cin >> s;

    if (s[0] != s[n-1]){
        cout << 1 << endl;
        return 0;
    }

    bool ok = 0;
    for(int i=1; i<n-1; i++){
        if (s[0] != s[i] && s[i+1] != s[n-1]){
            ok = 1;
            break;
        }
    }

    if (ok)
        cout << 2 << endl;
    else
        cout << -1 << endl;

    if (argc == 2 && atoi(argv[1]) == 123456789) cout << clock()*1.0/CLOCKS_PER_SEC << " sec\n";
    return 0;
}


#include <cstdio>
#include <cstring>
#define N 100010

using namespace std;

int n;
char s[N];

int main(){
//	freopen("funny.in", "r", stdin);
	scanf("%d", &n);
	scanf("%s", s+1);
	if (s[1]!=s[n]){
		printf("1\n");
	}
	else{
		bool pd=0;
		for (int i=2; i<=n; i++){
			if (s[i]!=s[1] && s[i-1]!=s[1]) pd=1;
		}
		if (pd) printf("2\n");
		else printf("-1\n");
	}
	return 0;
}