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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <cstdint>
#include <cstdio>
#include <cmath>
#include <deque>

using namespace std;

class LetterRange
{
private:
    char letter;
    int count;

public:
    LetterRange(char letter, int startPosition) : letter(letter), count(1) { }

    inline char GetLetter() { return letter; }
    inline int GetCount() { return count; }

    inline void Push(int size = 1) { count+=size; }
    inline void Pop(int size = 1) { count-=size; }
};

class PalindromFixer
{
private:
    deque<LetterRange> inscription;

public:
    PalindromFixer(const char* input)
    {
        Initialize(input);
    }

    int64_t Score()
    {
        return CanBeFixed() ? Fix() : -1;
    }

    bool CanBeFixed()
    {
        int count_a = 0;
        int count_b = 0;

        for(auto& r : inscription)
        {
            if (r.GetLetter() == 'a')
            {
                count_a += r.GetCount();
            }
            else if(r.GetLetter() == 'b')
            {
                count_b += r.GetCount();
            }
        }

        return ((count_a % 2) != 1) || ((count_b % 2) != 1);
    }

    int64_t Fix()
    {
        int64_t steps = 0;

        while (inscription.size() > 1)
        {
            if (GetFirstLetter() == GetLastLetter())
            {
                int toRemove = min(GetFirstRange().GetCount(), GetLastRange().GetCount());
                PopFront(toRemove);
                PopBack(toRemove);
            }
            else
            {
                int step_front = GetFirstRange().GetCount();
                int step_back = GetLastRange().GetCount();

                steps += min(step_front, step_back);

                if(step_front <= step_back)
                {
                    SwapAtFront();
                }
                else
                {
                    SwapAtBack();
                }
            }
        }

        return steps;
    }

private:
    void Initialize(const char* input)
    {
        inscription.clear();

        for (int i = 0; input[i] == 'a' || input[i] == 'b'; i++)
        {
            if (GetLastLetter() == input[i])
            {
                inscription.back().Push();
            }
            else
            {
                inscription.push_back(LetterRange(input[i], i));
            }
        }
    }

    inline char GetFirstLetter() { return inscription.size() > 0 ? inscription.front().GetLetter() : '\0'; }
    inline char GetLastLetter() { return inscription.size() > 0 ? inscription.back().GetLetter() : '\0'; }
    inline LetterRange& GetFirstRange() { return inscription.front(); }
    inline LetterRange& GetLastRange() { return inscription.back(); }

    inline void PopFront(int size)
    {
        if (GetFirstRange().GetCount() < size)
        {
            throw exception();
        }

        if (GetFirstRange().GetCount() == size)
        {
            inscription.pop_front();
        }
        else
        {
            GetFirstRange().Pop(size);
        }
    }

    inline void PopBack(int size)
    {
        if (GetLastRange().GetCount() < size)
        {
            throw exception();
        }

        if (GetLastRange().GetCount() == size)
        {
            inscription.pop_back();
        }
        else
        {
            GetLastRange().Pop(size);
        }
    }

    inline void SwapAtFront()
    {
        LetterRange& front = inscription[0];
        LetterRange& next = inscription[1];

        if (next.GetCount() == 1)
        {
            if (inscription.size() > 2)
            {
                inscription[2].Push(front.GetCount());
                inscription.pop_front();
            }
            else
            {
                inscription[0] = next;
                inscription[1] = front;
            }
        }
        else
        {
            next.Pop();
            inscription.push_front(LetterRange(next.GetLetter(), 1));
        }
    }

    inline void SwapAtBack()
    {
        LetterRange& back = inscription[inscription.size() - 1];
        LetterRange& prev = inscription[inscription.size() - 2];

        if (prev.GetCount() == 1)
        {
            if (inscription.size() > 2)
            {
                inscription[inscription.size() - 3].Push(back.GetCount());
                inscription.pop_back();
            }
            else
            {
                inscription[inscription.size() - 1] = prev;
                inscription[inscription.size() - 2] = back;
            }
        }
        else
        {
            prev.Pop();
            inscription.push_back(LetterRange(prev.GetLetter(), 1));
        }
    }
};

int main()
{
    const int BufferSize = 200001;
    char buffer[BufferSize];

    fgets(buffer, BufferSize, stdin);

    auto fixer = PalindromFixer(buffer);
    int64_t result = fixer.Score();

    printf("%lld\n", result);
    
    return 0;
}