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
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
#include <vector>
using namespace std;

int find(vector<int> *arr, int was, int s){
    int u, d, c;
    u = s - 1;
    d = 0;
    c = d + (u - d)/2;
    while (u - d > 1){
        if (arr->at(c) == was){
            return c;
        }
        else if (arr->at(c) > was){
            u = c;
        }
        else{
            d = c;
        }
        c = (u - d)/2 + d;
    }
    if (arr->at(c) == was){
        return u;
    }
    if (arr->at(0) == was){
        return 0;
    }
    return -1;
    
}


int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    vector<int> a;
    vector<int> b;
    int moves = 0;
    int aS = 0;
    int bS = 0;
    int i, n;
    string word;
    cin >> word;
    n = word.size();
    for (i = 0; i < n; i++){
        if (word[i] == 'a'){
            a.push_back(i);
            aS++;
        }
        else{
            b.push_back(i);
            bS++;
        }
    }


    int currA, currB, pos;

    for (i = 0; i < n/2; i++){
        if (word[i] != word[n - 1 - i]){
            if (word[i] = 'a'){
                if (bS == 0 || aS == 0){
                    cout << -1;
                    return 0;
                }
                currB = b[0];
                currA = a[aS - 1];
                if (currB - i <= n - 1 - i - currA){
                    pos = currB;
                    while (pos != i){
                        a[find(&a, pos - 1, aS)]++;
                        word[pos] = 'a';
                        word[pos - 1] = 'b';
                        pos--;
                        moves++;
                    }
                }
                else{
                    pos = currA;
                    while (pos != i){
                        b[find(&b, pos + 1, bS)]--;
                        word[pos] = 'b';
                        word[pos + 1] = 'a';
                        pos++;
                        moves++;
                    }
                }
            }
            else{
                if (bS == 0 || aS == 0){
                    cout << -1;
                    return 0;
                }
                currA = a[0];
                currB = b[bS - 1];
                if (currA - i <= n - 1 - i - currB){
                    pos = currA;
                    while (pos != i){
                        b[find(&b, pos - 1, bS)]++;
                        word[pos] = 'b';
                        word[pos - 1] = 'a';
                        pos--;
                        moves++;
                    }
                }
                else{
                    pos = currB;
                    while (pos != i){
                        a[find(&a, pos + 1, aS)]--;
                        word[pos] = 'a';
                        word[pos + 1] = 'b';
                        pos++;
                        moves++;
                    }
                }
            }
            if (a[0] == i){
                a.erase(a.begin());
                aS--;
            }
            if (a[aS - 1] == n - 1 - i){
                a.pop_back();
                aS--;
            }
            if (b[0] == i){
                b.erase(b.begin());
                bS--;
            }
            if (b[bS - 1] == n - 1 - i){
                b.pop_back();
                bS--;
            }
        }
    }
    cout << moves;
    return 0;
}