1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<string>
#include<algorithm>
#include<iostream>

using namespace std;

#define REP(i,n) for(int i=0;i<(n);i++)

int main(void) {
	string s;
	cin >> s;
	int n = s.size();

	int ca = 0;	REP(i, n) if (s[i] == 'a') ca++;
	if (!(n & 1) && (ca&1)) { cout << -1; return 0; }

	int l = 0, r=0, p = 0;
	long long d = 0;
	
	REP(i, n >> 1) {
		if (s[i] == 'a') l++;
		if (s[n - 1 - i] == 'a') r++;
		d += abs(l -r) + p;
		p = abs(l-r);		
	}
	if (n & 1) d += p;
	cout << (d >> 1);
	return 0;
}