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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <bits/stdc++.h>
#include <queue>
using namespace std;

string s;


void read() {
    cin>>s;
}

bool is_posible_palindorm() {
    int a = 0, b = 0;
    for(int i =0; i<s.size();i++) {
        if(s[i]=='a')
            a++;
        else 
            b++;
    }
    if(a%2==1 && b%2==1)
        return false;
    return true;
}


std::priority_queue<int> q_a;
std::priority_queue<int> q_b;
long long count_inversion() {
    long long inversion = 0;
    int suma_a = 0;
    int suma_b = 0;
    for(int i =0; i<s.size();i++) {
        if(s[i]=='a') {
            suma_a++;
            q_a.push(-i);
        }
        else {
            suma_b++;
            q_b.push(-i);
        }
    }
    int sa = suma_a / 2;
    int sb = suma_b / 2; 
    // pierwsza polowa - pilnujemy aby a i b bylo tyle samo
    int tmp_a = 0;
    int tmp_b = 0;
    for(int i =0; i<s.size()/2;i++) {
        if( sa == tmp_a && s[i]=='a') {
            tmp_b++;
            int w = -q_b.top();
            s[w] = 'a';
            s[i] = 'b';
            q_b.pop();
            q_a.pop();
            q_a.push(-w);
            inversion += (w-i);

        } else if (sb == tmp_b && s[i]=='b' ) {
            tmp_a++;
            int w = -q_a.top();
            s[w] = 'b';
            s[i] = 'a';
            q_a.pop();
            q_b.pop();
            q_b.push(-w);
            inversion += (w-i);
            
        } else {
            // jest ok idziemy dalej    
            if(s[i]=='a') {
                tmp_a++;
                q_a.pop();
            } else { 
                tmp_b++;
                q_b.pop();
            }
        }
    }
    // jesli nie parzysty
    int k = 1;
    if(s.size()%2==1){
        k = 2;
        int np = (s.size()-1)/2;
        if(suma_a%2==1) {
            if(s[np]=='a'){
              q_a.pop();  

            } else {
                
                int w = -q_a.top();
                s[w] = 'b';
                s[np] = 'a';
                q_a.pop();
                q_b.pop();
                q_b.push(-w);
                inversion += (w-np);
            
            }
        }
        else {
            if(s[np]=='b'){
              q_b.pop(); 
            } else {
                int w = -q_b.top();
                s[w] = 'a';
                s[np] = 'b';
                q_b.pop();
                q_a.pop();
                q_a.push(-w);

                inversion += (w-np);
            }
        }
    }
    /*cout<<"A: \n";
    while(!q_a.empty()) {
        cout<<-q_a.top()<<"\n";
        q_a.pop();
    }
    cout<<"\nB: \n";
    while(!q_b.empty()) {
        cout<<-q_b.top()<<"\n";
        q_b.pop();
    }
    cout<<"\n";
    */

    for(int i =(s.size()+1)/2; i<s.size();i++) {
        if(s[i]==s[i-k]){
            if(s[i]=='a') {
                q_a.pop();
            } else { 
                q_b.pop();
            }
        } else {
             if(s[i]=='a') {
                int w = -q_b.top();
                s[w] = 'a';
                s[i] = 'b';
                q_b.pop();
                q_a.pop();
                q_a.push(-w);
                inversion += (w-i);
            } else { 
                int w = -q_a.top();
                s[w] = 'b';
                s[i] = 'a';
                q_a.pop();
                q_b.pop();
                q_b.push(-w);
                inversion += (w-i);
            }
        }
        k+=2;
    }
    return inversion;
}

void solve() {
    read();
    if(is_posible_palindorm()) {
        cout<<count_inversion()<<"\n";
    } else {
        cout<<-1<<"\n";
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    solve();
    return 0;
}