#include <bits/stdc++.h>

#define FOR(i,x,n) 	for(int i=x; i<n; i++)
#define F0R(i,n) 	FOR(i,0,n)
#define ROF(i,x,n) 	for(int i=n-1; i>=x; i--)
#define R0F(i,n) 	ROF(i,0,n)

#define WTF 		cerr << "WTF" << endl
 
#define IOS 		ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define F 			first
#define S	 		second
#define pb 			push_back
#define mp 			make_pair

#define ALL(x) 		x.begin(), x.end()
#define RALL(x) 	x.rbegin(), x.rend()
#define FILL(a, b) 	memset(a, b, sizeof(a));

#define TOGGLE(x) (x) = 1 - (x)

using namespace std;
typedef double 			db;
typedef long long 		LL;
typedef string 			str;

typedef pair<int, int> 	PII;
typedef pair<LL, LL> 	PLL;

typedef vector<int> 	VI;
typedef vector<LL> 		VLL;
typedef vector<bool> 	VB;
typedef vector<char> 	VC;
typedef vector<PII> 	VPII;
typedef vector<PLL> 	VPLL;
typedef vector<string> 	VSTR;

typedef set<int> 		SI;
typedef set<PII> 		SPII;
typedef set<PLL> 		SPLL;

typedef priority_queue<PII> DIPQ;

const int MAXN = 2e5 + 7;
const int MAXA = 1e7 + 7;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const int LOG = 20;
const LL LONG_INF = 1e18;

int n;
str inp;

void Solve () {
	cin >> n >> inp;
	if (inp[0] != inp[n - 1]) {
		cout << 1 << endl;
		return;
	}
	
	else {
		FOR (i, 1, n - 2) {
			if (inp[i] != inp[0] && inp[i + 1] != inp[0]) {
				cout << 2 << endl;
				return;
			}
		}
	}
	
	cout << -1 << endl;
}


int main() {
	IOS;
	Solve ();
}
#include <bits/stdc++.h>
using namespace std;
#define ll long long 


int main(){
    ios_base::sync_with_stdio(false);cin.tie(0);
    int n;
    cin>>n;
    string s;
    cin>>s;
    vector<int> min_rem(n+1, n);
    min_rem[n]=0;
    vector<int> ending_with_char(26, -1);
    for(int i=n-1;i>=0;i--){
        if(i==n-1){
            min_rem[i]=-1;
            ending_with_char[(int)(s.at(i)-'a')]=1;
        }
        else{
            min_rem[i]=n;
            for(int ch=0;ch<26;ch++){
                if(ch!=(int)(s.at(i)-'a')){
                    if(ending_with_char[ch]!=-1){
                        min_rem[i]=min(min_rem[i], ending_with_char[ch]);
                    }
                }
            }
            min_rem[i]=(min_rem[i]==n?-1:min_rem[i]);
            if(min_rem[i+1]!=-1){
                ending_with_char[(int)(s.at(i)-'a')]=min(ending_with_char[(int)(s.at(i)-'a')]==-1?n:ending_with_char[(int)(s.at(i)-'a')], min_rem[i+1]+1);
            }
        }
    }
    cout<<min_rem[0]<<"\n";
}
