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
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>
#include <vector>
#include <queue>
#include <bitset>
#include <utility>
#include <stack>

using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define MP make_pair
#define FOR(v,p,k) for(int v=(p);v<=(k);++v)
#define FORD(v,p,k) for(int v=(p);v>=(k);--v)
#define REP(i,n) for(int i=0;i<(n);++i)
#define VAR(v,i) __typeof(i) v=(i)
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)
#define PB push_back
#define ST first
#define ND second
#define SIZE(x) (int)x.size()
#define ALL(c) c.begin(),c.end()

#define ODD(x) ((x)%2)
#define EVEN(x) (!(ODD(x)))

char t[200009];

int main() {
    scanf("%s\n", t);
    int n=0, a_cnt=0;
    while (t[n]) {
        if (t[n] == 'a') a_cnt++;
        n++;
    }
    int b_cnt = n-a_cnt;
    if (b_cnt % 2 && a_cnt % 2) {
        printf("-1\n");
        return 0;
    }

    VI cnts;
    cnts.reserve(n);
    char prev=-1;
    int curr_cnt=0;
    REP(i,n) {
        char c = t[i];
        if (c != prev) {
            if (curr_cnt > 0) cnts.PB(curr_cnt);
            curr_cnt=1;
            prev=c;
        } else {
            curr_cnt++;
        }
    }
    cnts.PB(curr_cnt);

    int i=0, j=cnts.size()-1;
    LL res=0;
    while (i<j) {
        if ((j-i)% 2 == 0) {
            if (i==j) break;
            cnts[i]--;
            cnts[j]--;
            if (cnts[i]==0) i++;
            if (cnts[j]==0) j--;
        } else {
            if (i == j-1) {
                while (cnts[i] >= 2) {
                    res += cnts[j];
                    cnts[i] -= 2;
                }
                if (cnts[i] == 1) {
                    res += cnts[j]/2;
                }
                break;
            }
            cnts[j-1]--;
            cnts[i]--;
            if (cnts[i]==0) i++;
            res += cnts[j];
            if (cnts[j-1] == 0) {
                cnts[j-2] += cnts[j];
                cnts[j]=0;
                j-=2;
            }
        }
    }
    printf("%lld\n", res);
    return 0;
}