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
#include <bits/stdc++.h>
#define ft first
#define sc second
#define pb push_back
#define FOR(i,n) for(int i=0; i<n; i++)
#define watch(x) cout << (#x) << " == " << (x) << endl
typedef long long ll;
typedef long double ld;
using namespace std;

int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);

string s;
cin>>s;

int a=0, b=0;
FOR(i,s.size()){
  if (s[i]=='a'){a++;}
  else {b++;}
}

char ch;
if (a%2==0) {ch='a';}
else if (b%2==0) {ch='b';}
else {cout<<"-1\n"; return 0;}

ll ile=0, l=0, r=s.size()-1;
while (1){
  while (l<r && s[l] != ch) {l++;}
  while (l<r && s[r] != ch) {r--;}
  if (l>=r) {break;}
  ile += abs((ll)s.size()-1-r - l);
  l++;
  r--;
}
cout<<ile;

return 0;
}