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 <vector>
#include <algorithm>

using namespace std;

int n, c, res;
vector<int> as;

int main(){
    c = getchar();
    while(c == 'a' || c == 'b'){
        if(c == 'a') as.push_back(n);
        n++;
        c = getchar();
    }
    if(!(n&1) && (as.size())&1) printf("-1\n");
    else{
        for(int i = 0; i < (as.size() / 2); i++){
            res += abs(as[i] + as[as.size() - i - 1] - n + 1);
        }
        res += (as.size())&1 * abs(as[as.size() / 2] - (n / 2));
        printf("%d\n", res);
    }
}