/* Lucky_Glass */
#include <cstdio>
#include <cstring>
#include <algorithm>

const int N = 1e5 + 10, INF = 0x3f3f3f3f;

int n;
char str[N];
int dp[N], mndp[30];

int main() {
  scanf("%d%s", &n, str + 1);
  memset(mndp, 0x3f, sizeof mndp);

  mndp[str[1] - 'a'] = 0;
  for (int i = 1; i <= n; ++i) {
    int &now = dp[i] = INF;
    for (int c = 0; c < 26; ++c)
      if (c != str[i] - 'a')
        now = std::min(now, mndp[c] + 1);
    if (i < n)
      mndp[str[i + 1] - 'a'] = std::min(mndp[str[i + 1] - 'a'], now);
  }
  printf("%d\n", dp[n] == INF ? -1 : dp[n]);
  return 0;
}
//  by pakhomovee
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <cmath>
#include <unordered_map>
#include <cassert>
#include <queue>
#include <unordered_set>

using namespace std;

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define int long long
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ll long long
#define dbg(x) std::cerr << #x << ": " << x << '\n';
#define FOR(i, n) for (int i = 0; i < n; ++i)
#define FEL(a, x) for (auto& a : x)
#define REV reverse
#define asrt assert
#define unique1(x) x.resize(unique(all(x)) - x.begin())

const int mod = 1e9 + 7;

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

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    t = 1;
    while (t--) {
        solve();
    }
}
