#include<bits/stdc++.h>
using namespace std;
using namespace std::chrono; 
//const int mod=1e9+7;
const int mex=200005;
#define ll long long 
#define test int t;cin>>t;while(t--)
#define fast  ios_base::sync_with_stdio(false);cin.tie(NULL);
#define fo(i,a,n) for(int i=a;i<n;i++)
#define rfo(i,a,b) for(int i=a;i>=b;i--)
#define bg begin()
#define en end()
#define fi first
#define se second
#define ub upper_bound
#define lb lower_bound
#define pb push_back
#define veci vector<int>
#define veclli vector<long long int> 
#define all(x) x.begin(),x.end()
#define sci(x) scanf("%d",&x);
#define scc(x) scanf("%c",&x);
#define scs(x) scanf("%s",x);
#define debug(arr,n) for(int i=0;i<n;i++) printf("%d ",arr[i]);
#define sz(x) x.size()
#define loop(x) for(auto it=x.begin();it!= x.end();it++)
#define int long long
int power(int a,int b,int mod)
{
	int ans=1,f=a;
	while(b)
	{
		if(b&1ll) ans=ans*f%mod;
		f=f*f%mod;
		b=b>>1ll;
	}
	return ans;
}

signed main()
{
	string s;
    cin>>s;
    int n=sz(s);
    int fr[26]={0},ans=0;
    rfo(i,sz(s)-1,1)
    {
        if(s[i]==s[i-1])
        {
            ans=ans+(n-i-1-fr[s[i]-'a']);
            fo(i,0,26)
            fr[i]=0;
            fr[s[i]-'a']=n-i;
        }
        else fr[s[i]-'a']++;
    }
    cout<<ans<<endl;
    
	
}
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) begin(x),end(x)
#define F(i,n) for (int i = 0; i < n; ++i)
#define F1(i,n) for (int i = 1; i <= n; ++i)
#define dbg(x) cerr << #x << " = " << x << endl
#define dbgg(x) cerr << #x << " = " << x << ' '
#define T(x) x[pool]
#define mineq(x,y) { if ((x) > (y)) (x) = (y); }
#define maxeq(x,y) { if ((x) < (y)) (x) = (y); }
#define MEOW cout << "meowwwww" << '\n'; system("pause");
#define int long long
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;

template<typename T>
ostream& operator <<(ostream &s, const vector<T> &c)
{
	s << "[ "; for (auto it : c) s << it << " "; s << "\b]\n";
	return s;
}

template<typename T>
ostream& operator <<(ostream &s, const pair<int, T> &c)
{
	s << "[ "; cout << c.fi << " , " << c.se << " ] ";
	return s;
}

int n;
string a;

void input()
{
	ios::sync_with_stdio(false); cin.tie(0);
	cin >> n >> a;
}

void solve()
{
	int ok = 0;
	if (a[0] == a[n - 1])
	{
		for (int i = 0; i < n - 1; ++i)
			if (a[i] != a[0] && a[i + 1] != a[0])
				ok = 2;
	} else ok = 1;
	if (ok) cout << ok << '\n';
	else cout << "-1\n";
}

main()
{
	input();
	solve();
}
