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
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <ranges>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

int solve() {
    string s;
    cin >> s;

    int l = 0;
    int r = s.size() - 1;

    int i = l, j = r;

    int res = 0;
    while (l < r) {
        if (s[l] == s[r]) {
            l++;
            r--;
            continue;
        }
        if (r == l + 1) return -1;
        i = max(i, l);
        j = min(j, r);
        while (s[l] == s[i]) i++;
        while (s[r] == s[j]) j--;

        if (i - l < r - j) {
            res += i - l;
            swap(s[i], s[l]);
            j++;
        } else {
            res += r - j;
            swap(s[j], s[r]);
            i--;
        }
        assert(s[l] == s[r]);
    }
    string rev = s;
    reverse(all(rev));
    assert(rev == s);
    return res;
}

#undef int
int main() {
    ios::sync_with_stdio(false);
    cin.exceptions(cin.failbit);
    cin.tie(0);
    cout << solve() << endl;
}