#include <bits/stdc++.h>
using namespace std;

#define pb push_back

#define int ll
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vpii = vector<pii>;
using vvpii = vector<vpii>;

template<typename T, typename U>
ostream& operator<<(ostream& o, const pair<T,U>& p) {
  return o << "(" << p.first << "," << p.second << ")";
}

template<typename T, typename U>
ostream& operator<<(ostream& o, const map<T,U>& m) {
  o << "{";
  for (const pair<T,U>& p : m)
    o << p.first << ": " << p.second << ",";
  return o << "}";
}

template<typename T>
ostream& operator<<(ostream& o, const set<T>& s) {
  o << "{";
  for (const T& e : s)
    o << s << ",";
  return o << "}";
}

template<typename T>
ostream& operator<<(ostream& o, const vector<T>& v) {
  o << "[";
  for (const T& x : v)
    o << x << ",";
  return o << "]";
}

template<typename T>
ostream& operator<<(ostream& o, const vector<vector<T>>& v) {
  o << "[";
  for (const vector<T>& x : v)
    o << x << ",\n";
  return o << "]";
}


signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

    int n;
    string s;
    cin >> n >> s;

    if (s[0] != s[n-1]) {
        cout << "1\n";
        return 0;
    }
    for (int i=1; i<n-2; ++i) {
        if (s[0] != s[i] && s[i+1] != s[n-1]) {
            cout << "2\n";
            return 0;
        }
    }
    cout << "-1\n";
}

// chrono::system_clock::now().time_since_epoch().count()
#include <bits/stdc++.h>

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define debug(x) cerr << #x << " = " << x << endl

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int MAXN = (int)1e5 + 5;
const int INF = (int)1e9;

int dp[MAXN], mem[26];
int n;
string s;

void solve() {
    cin >> n;   
    cin >> s;
    s = '#' + s;
    fill(dp, dp + n + 1, INF);
    fill(mem, mem + 26, INF);
    dp[0] = 0;
    mem[s[1] - 'a'] = 0;

    rep (i, 1, n + 1) {
        rep (j, 0, 26) {
            if (s[i] != j + 'a') {
                dp[i] = min(dp[i], mem[j] + 1);
            }
        }

        if (i < n) {
            mem[s[i + 1] - 'a'] = min(mem[s[i + 1] - 'a'], dp[i]);
        }
    }

    if (dp[n] == INF) {
        dp[n] = -1;
    }

    cout << dp[n] << '\n';
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int tt = 1;

    for (int i = 1; i <= tt; ++i) {
        solve();
    }

    return 0;
}