#include<bits/stdc++.h>
#define mxn 100005
#define inf 1e9
using namespace std;
int n, mem[mxn][26];
char str[mxn];
int dp(int pos, int ch)
{
    if(pos==n)return inf;
    if(pos==n-1 && (str[n-1]-'a')!=ch)return 1;
    int &ret= mem[pos][ch];
    if(ret!=-1)return ret;

    ret= dp(pos+1, ch);
    if((str[pos]-'a')!=ch)ret= min(ret, 1+dp(pos+2, str[pos+1]-'a'));
    return ret;
}
int main()
{
    scanf("%d %s", &n, str);
    memset(mem, -1, sizeof mem);
    int res= dp(1, str[0]-'a');
    if(res==inf)res= -1;
    printf("%d\n", res);

    return 0;
}

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