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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <bits/stdc++.h>
#include <vector>
using namespace std;
#pragma GCC maksuje_zadania_na_essie("pot200");

using ll = long long;

string word;
int cnt[2] = {};

ll solve(char x) {
	vector<int> positions;
	for(int i = 0; i < word.size(); i++) {
		if(word[i] == x) {
			positions.push_back(i);
		}
	}
	ll res = 0;
	for(int i = 0; i < positions.size()/2; i++) {
		res += abs(ll(word.size()-1-positions[positions.size()-i-1] - positions[i]));
	}
	if((cnt[x-'a']&1) == 1) {
		res += abs(ll(positions[positions.size()/2] - word.size()/2));
	}
	return res;
}

int main() {

    cin.tie(0); cout.tie(0);
    ios_base::sync_with_stdio(false);
    cin >> word;

	for(char c : word)
		cnt[c-'a']++;

	int parity_sum = (cnt[0]&1) + (cnt[1]&1);

	if(parity_sum > (word.length()&1)) {
		cout << (-1) << '\n';
		return 0;
	}

	if(cnt[1] == 1) cout << solve('b') << '\n';
	else cout << solve('a') << '\n';

}