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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
 * 0-pal.cpp
 *
 *  Created on: 6 gru 2022
 *      Author: poz
 */

#include <bits/stdc++.h>
using namespace std;

const int M = 200'000;

const char A = 'a';
const char B = 'b';


long long count(vector<int> &pos, const int S) {
	const int L = pos.size();
	int left, right;
	long long result = 0;
	for (int i = 0; i < L / 2; i++) {
		left = pos[i];
		right = S - pos[L - i - 1] - 1;
		result += abs(left - right);
	}
	return result;
}


int main() {
	string text;
	cin >> text;

	const int S = text.size();

	vector<int> pos_a, pos_b;

	for (int i = 0; i < S; i++) {
		if (text[i] == A) {
			pos_a.push_back(i);
		} else {
			pos_b.push_back(i);
		}
	}

	const int a_size = pos_a.size();
	const int b_size = pos_b.size();
	const bool a_odd = a_size % 2 != 0;
	const bool b_odd = b_size % 2 != 0;

	if (a_odd && b_odd) {
		cout << "-1" << endl;
		return 0;
	}

	long long result;
	if (a_odd) {
		result = count(pos_b, S);
	} else if (b_odd) {
		result = count(pos_a, S);
	} else if (a_size < b_size) {
		result = count(pos_a, S);
	} else {
		result = count(pos_b, S);
	}

	cout << result << endl;

	return 0;
}