#include <bits/stdc++.h>
#define rep(i, l, r) for(int i = (l); i <= (r); i++)
#define per(i, r, l) for(int i = (r); i >= (l); i--)
#define mem(a, b) memset(a, b, sizeof a)
#define For(i, l, r) for(int i = (l), i##e = (r); i < i##e; i++)
#define pb push_back
#define eb emplace_back
#define upd(a, b) (a = min(a, b))

using namespace std;

const int N = 1e5 + 5;

int n, mi[128], f[N];
char s[N];

int main() {
    mem(f, 63), mem(mi, 63);
#ifdef local
	// freopen(".in", "r", stdin);
#endif
    scanf("%d%s", &n, s + 1);
    mi[s[1]] = 0;
    rep(i, 2, n) {
        rep(j, 'a', 'z') if(j ^ s[i]) upd(f[i], mi[j] + 1);
        upd(mi[s[i + 1]], f[i]);
    }
    if(f[n] < 0x3f3f3f3f) cout << f[n];
    else puts("-1");
    return 0;
}
#include <bits/stdc++.h>
#define int int64_t
using namespace std;

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    string s;
    int n;  cin >> n >> s;
    if(s[0] != s[n - 1])
        return cout << "1", 0;
    for(int i = 1; i < n - 2; i++) {
        if(s[i] != s[0] and s[i + 1] != s[0])
            return cout << "2", 0;
    }
    cout << "-1";
}