#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";
}

#include <iostream>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <string>
#include <sstream>
#include <algorithm>
#include <random>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <cassert>
#include <climits>
#include <bitset>
#include <functional>
#include <iomanip>
#include <random>

#define FOR_LT(i, beg, end) for (std::remove_const<decltype(end)>::type i = beg; i < end; i++)
#define FOR_LE(i, beg, end) for (std::remove_const<decltype(end)>::type i = beg; i <= end; i++)
#define FOR_DW(i, beg, end) for (std::remove_const<decltype(beg)>::type i = beg; end <= i; i--)
#define REP(n)              for (std::remove_const<decltype(n)>::type repeat_index = 0; repeat_index < n; repeat_index++)

using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout << fixed << setprecision(20);

	int n; cin >> n;
	string s; cin >> s;
	if (s.front() != s.back()) {
		cout << 1 << endl;
		return 0;
	}

	FOR_LT(i, 1, n - 2) {
		if (s[i] != s.front() && s[i + 1] != s.back()) {
			cout << 2 << endl;
			return 0;
		}
	}

	cout << -1 << endl;
	return 0;
}