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
#include <cstdio>
#include <cstring>

typedef long long int i64;

template <typename T> T abs(T a) { return a < 0 ? -a : a; }

int main() {
  char word[200200];
  scanf("%s", word);
  i64 sum = 0;
  for (int i = 0, j = 0, L = strlen(word) - 1; word[i]; ++i) {
    if (word[i] == 'b') {
      while (word[L - j] != 'b') {
        j += 1;
      }
      sum += abs(i - j);
      ++j;
    }
  }

  printf("%lld\n", sum & 1 ? -1 : sum / 2);

  return 0;
}