// Libraries
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

// Aliases
#define nitro std::ios_base::sync_with_stdio(false), std::cin.tie(NULL)
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define sz(x) (int)x.size()
#define all(v) v.begin(), v.end()
#define case_g(x) cout<<"Case #"<<x<<": "
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using ul = unsigned long;
using vi = std::vector<int>;
using vl = std::vector<ll>;
using vb = std::vector<bool>;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;
using pib = std::pair<int, bool>;
using plb =  std::pair<ll, bool>;
using vii = std::vector<pii>;
using vll = std::vector<pll>;
using vib = std::vector<pib>;
using vlb = std::vector<plb>;
using vvi = std::vector<vi>;
using vvl = std::vector<vl>;
using vvb = std::vector<vb>;
using mapii = std::map<int, int>;
using mapll = std::map<ll, ll>;
using mapib = std::map<int, bool>;
using maplb = std::map<ll, bool>;
using hmapii = __gnu_pbds::gp_hash_table<int, int>;
using hmapll = __gnu_pbds::gp_hash_table<ll, ll>;
using hmapib = __gnu_pbds::gp_hash_table<int , bool>;
using hmaplb =  __gnu_pbds::gp_hash_table<ll, bool>;
using ranked_pairset = tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update>;
using ranked_set = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;
using ranked_map = tree<int, int, less<int>, rb_tree_tag, tree_order_statistics_node_update>;

// Debugging template
#ifndef LOCAL
#define cerr if (false) cerr
#endif
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
template<typename T>
void _printA(T *t, long long sz) { cout<<" { "; for (long long i=0; i<sz; i++) cout<<"["<<i<<"] = "<< t[i]<<endl; cout<<" } \n";}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#define debugA(x, y) cerr << "[" << #x << "] = "; _printA(x, y)

// Constants
const ll mod = 1000000007;
const ld pi = 3.141592653589793L;
const char nl = '\n';

// Colours for fun :)
#define RED "\033[31m"
#define GREEN "\033[32m"
#define RESET "\033[0m"

void precompute() {
}

void solve() {
	int n;
	string s;
	cin >> n >> s;
	
	if( s[0] != s[n - 1] ) {
		cout << 1 << nl;
		return;
	}
	
	for(int i = 1; i <= n - 2; ++i) {
		if( s[i] != s[0] && s[i + 1] != s[0] ) {
			cout << 2 << nl;
			return;
		}
	}
	
	cout << -1 << nl;
}

int main() {
	nitro;
	
	int tc = 1;
	//cin >> tc;
	
	precompute();
	for(int i = 1; i <= tc; ++i) {
		//case_g(i);
		solve();
	}
	return 0;
}


















/*Allah Vorosha*/
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define in insert
#define rev reverse
#define all(x) (x).begin(),(x).end()
#define all2(x) (x).rbegin(),(x).rend()
#define sz(v) (int)v.size()
#define yes cout << "YES\n"
#define no cout << "NO\n";
#define take for(auto &it : a) cin >> it;
#define vi vector<int>
#define vii vector<pair<int, int > >
#define vl vector<ll>
#define fi first
#define se second
#define l_b lower_bound
#define u_b upper_bound
#define ed printf("\n")
#define mems(a, b) memset(a, b, sizeof(a))
#define ios ios_base::sync_with_stdio(0);cin.tie();cout.tie();

using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T> void read(T &x) { cin >> x; }
template<class T, class ...U> void read(T &x, U& ... u) { read(x); read(u...); }
template<class T> void print1(const T &x) { cout << x << '\n'; }
template<class T, class ...U> void print1(const T &x, const U& ... u) { print1(x); print1(u...); }
template<class T> void print2(const T &x) { cout << x << ' '; }
template<class T, class ...U> void print2(const T &x, const U& ... u) { print2(x); print2(u...); }

const int N = 2e3 + 5, mod = 1e9 + 7, M = 5e5 + 5;
const ll inf = 9e18;
int cs = 1;

void solve() {
    int n;
    string s;
    read(n, s);
    int ans = -1;
    for(int i = 1; i < n; i++) {
        if(s[i] != s[0]) {
            if(i + 1 == n) {
                ans = 1;
                break;
            }
            if(s[i + 1] != s[n - 1]) {
                ans = 2;
            }
        }
    }
    print1(ans);
}

int main() {
    ios;
    //cout << fixed << setprecision(15);
    //int tc; read(tc); while(tc--> 0)
    solve();
    return 0;
}
