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
#include <bits/stdc++.h>
using namespace std;

bool possible(string word) {
    int a = 0;
    int b = 0;
    for (int i=0; i<word.size(); i++)
        if (word[i] == 'a')     a++;
        else                    b++;
    return !(a%2 == 1 && b%2 == 1);
}


int main() {
    string word;
    int compare_left, compare_right, operations = 0;
    cin >> word;
    if (possible(word) == 0)
        cout << "-1";
    else {
        int left = 0, right = word.size()-1;
        while (left < right) {
            if (word[left] != word[right]) {
                operations++;
                compare_left = left + 1; compare_right = right - 1;
                while (word[left] == word[compare_left] && word[right] == word[compare_right]) {
                    compare_left++; compare_right--;
                    operations++;
                }
                if (word[compare_left] != word[left])
                    word[compare_left] = word[left];
                else
                    word[compare_right] = word[right];
            }
            left++; right--;
        }

        cout << operations;
    }
    
}