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
import sys

s = list(sys.stdin.readline())
n = len(s) - 1

cnt_a = s.count('a')
cnt_b = s.count('b')
if cnt_a % 2 + cnt_b % 2 != n % 2:
    print('-1')
    exit(0)

result, r = 0, 1
for i in range(0, n // 2):
    j = n - i - 1
    if s[i] == s[j]:
        r = 1
    else:
        while s[i + r] != s[j] and s[i] != s[j - r]:
            r = r + 1
        result += r
        if s[i + r] == s[j]:
            s[i + r] = s[i]
        else:
            s[j - r] = s[j]
        r = max(1, r - 2)

print(result)