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
#ifdef _MSC_VER
  #ifndef __GNUC__
    #pragma warning(disable: 4996)
  #endif
  #define main main0
#endif
#include <iostream>
#include <string>
using namespace std;
typedef long long           ll;
typedef unsigned long long ull;
typedef unsigned int      uint;

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);

  ll wynik = 0;
  string slowo;

  getline(cin, slowo);

  if((slowo.size() & 1) == 0) {
    int i = 0;
    for(string::iterator it = slowo.begin(); it != slowo.end(); ++it)
      if(*it == 'a')
        ++i;
    if((i & 1) == 1) {
      cout << "-1\n";
      return 0;
    }
  }

  for(int p0 = 0, p1, k0 = slowo.size() - 1; ; ++p0, --k0) {
    while(p0 < k0 && slowo[p0] == slowo[k0]) {
      ++p0;
      --k0;
    }
    if(p0 >= k0) {
      cout << wynik << endl;
      return 0;
    }
    for(p1 = p0 + 1; slowo[p0] == slowo[p1]; ++p1)
      ;
    wynik += p1 - p0;
    slowo[p1] = slowo[p0];
  }

  return 0;
}