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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <string>
#include <vector>

using namespace std;
using LL = long long;

LL solve(string word)
{
	int a = 0;
	int b = 0;

	for (int i = 0; i < word.length(); ++i)
	{
		if (word[i] == 'a')
			++a;
		else
			++b;
	}

	if (a == 0 || b == 0)
		return 0;

	if (a % 2 != 0 && b % 2 != 0)
		return -1;

	LL res = 0;
	char c = (a % 2 == 0 ? 'a' : 'b');
	char c2 = (a % 2 == 0 ? 'b' : 'a');
	vector<int> pos;

	for (int i = 0; i < word.length(); ++i)
	{
		if (word[i] == c)
			pos.push_back(i);
	}

	int ind1 = 0;
	int ind2 = word.length() - 1;
	int pos1 = 0;
	int pos2 = pos.size() - 1;
	
	while (ind1 < ind2)
	{
		if (word[ind1] != word[ind2])
		{
			if (word[ind1] == c)
			{
				res += ind2 - pos[pos2];
				word[pos[pos2]] = c2;
			}
			else
			{
				res += pos[pos1] - ind1;
				word[pos[pos1]] = c2;
			}

			++pos1;
			--pos2;
		}
		else if (word[ind1] == c)
		{
			++pos1;
			--pos2;
		}

		++ind1;
		--ind2;
	}

	return res;
}

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

	LL res = solve(word);

	cout << res << endl;

	return 0;
}