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
30
31
32
#include <cmath>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define PB push_back
#define SIZE(c) ((int)(c).size())
#define LINE(n,x) char x[n]; fgets(x, n, stdin)

typedef long long LL;
typedef vector<int> VI;

int main() {
	LINE(200002, a);
	int n = strlen(a);
	while (a[n - 1] == '\n') --n;
	VI v;
	REP(i,n) if (a[i] == 'a') v.PB(i);
	int m = SIZE(v);
	if (!(n & 1) && (m & 1)) {
		printf("-1\n");
		return 0;
	}
	LL r = 0;
	int m2 = m >> 1;
	REP(i,m2) r += abs(v[i] + v[m - i - 1] - n + 1);
	if (m & 1) r += abs(v[m2] - (n >> 1));
	printf("%lld\n", r);
}