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
#include <iostream>
#include <string>
#include <cstdint>



int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);


    std::string str;
    std::getline(std::cin, str);

    int64_t result = 0;

    // count a and b
    int n = str.size();
    int a=0, b=0;
    for( auto &ch : str )
    {
        if( ch == 'a' )
            a++;
        else
            b++;
    }

    // impossible
    if( n%2 == 0 && a%2 == 1 && b%2 == 1 )
    {
        std::cout << "-1" << std::endl;
        return 0;
    }
    // only a or only b
    if( n == a || n == b)
    {
        std::cout << "0" << std::endl;
        return 0;
    }

    //std::cerr << "n a b\n";
    //std::cerr << n << " " << a << " " << b << "\n";

    // swap a and b - after that 'a' should be finally in the middle
    if( n%2 == 1 && a%2 == 0 && b%2 == 1 )
    {
        for( auto &ch : str )
        {
            if( ch == 'a' )
                ch = 'b';
            else
                ch = 'a';
        }
        int tmp = a;
        a = b;
        b = tmp;
    }


    // swap begin and end
    int half_b_sum = 0;
    int other_half_b_sum = 0;
    for( int i=0; i<str.size()/2; i++ )
    {
        if( str[i] == 'b' )
            half_b_sum++;
    }
    if( n%2 == 0 )
        other_half_b_sum = n - half_b_sum;
    else
    {
        if( str[n/2 + 1] == 'b' )
            other_half_b_sum = b - 1 - half_b_sum;
        else
            other_half_b_sum = b - half_b_sum;
    }
    if(half_b_sum < other_half_b_sum)
    {
        for( int i = 0; i < str.size()/2 ; i++ )
        {
            char c = str[i];
            str[i] = str[n-i-1];
            str[n-i-1] = c;
        }
    }


    // compute result
    int pos=0;
    int sum=0;
    while(sum < b/2)
    {
        if( str[pos] == 'b' )
            sum++;
        pos++;
    }
    int64_t previous_height = 0;
    int b_moved = 0;
    while( b_moved + (n-n/2) > pos )
    {
        if( str[pos] == 'b' )
        {
            str[pos] = 'a';
            b_moved++;
            previous_height += pos;
        }
        pos++;
    }
    int64_t new_height = 0;
    for( int i=0; i<b_moved; i++ )
    {
        str[n-n/2+i] = 'b';
        new_height += n-n/2+i;
    }
    result = new_height - previous_height;

    //std::cerr << str << "\n";
    //std::cerr << "partial result " << result << "\n";


    int left = n/2, right = n-n/2-1;
    int iter_count = 0;
    while( left >= 0 && right < n )
    {
        do{
            left--;
        } while( left >= 0 && str[left] == 'a' );
        do{
            right++;
        } while( right < n && str[right] == 'a' );
        //std::cerr << "left " << left << " right " << right << " iter_count " << iter_count++ << "\n";
        if( left < 0 || right >= n )
            break;
        int left_from_center = n/2 - left;
        int right_from_center = right - n + n/2 + 1;
        if( left_from_center == right_from_center )
            continue;
        
        if( left_from_center < right_from_center )
            result += (right_from_center-left_from_center);
        else
            result += (-right_from_center+left_from_center); 
    }

    std::cout << result << std::endl;

    return 0;
}