#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i, n) for (int (i) = 0; (i) < (n); ++(i))

template<typename T> struct BinaryIndexedTree {
    vector< T > data;
    BinaryIndexedTree(int sz) { data.assign(++sz, 0); }
    T sum(int k) {
        if (k < 0) return T(0);
        T ret = 0;
        for (++k; k > 0; k -= k & -k) ret += data[k];
        return (ret);
    }
    T sum(int l, int r) {
        assert(l <= r);
        return sum(r - 1) - sum(l - 1);
    }
    void add(int k, T x) {
        for (++k; k < data.size(); k += k & -k) data[k] += x;
    }
};

template<typename T> int_fast64_t InversionNumber(vector< T > &v) {
    int_fast64_t sz = v.size(), ret = 0;
    vector< T > rev, v_cp = v; sort(v_cp.begin(), v_cp.end());
    for (auto &e : v) rev.emplace_back(lower_bound(v_cp.begin(), v_cp.end(), e) - v_cp.begin());
    BinaryIndexedTree< T > bit(sz);
    for (int i = 0; i < sz; ++i) {
        ret += i - bit.sum(rev[i]);
        bit.add(rev[i], 1);
    }
    return ret;
}

signed main() {

    string S;
    while (cin >> S) {
        int N = S.size();

        int cnt[26], now[26];
        REP(i, 26) cnt[i] = now[i] = 0;
        REP(i, N) cnt[S[i] - 'a']++;

        int odd = 0;
        REP(i, 26) odd += cnt[i] % 2;
        if (odd > N % 2) {
            cout << -1 << endl;
            continue;
        }

        if (N % 2 == 0) {
            // 塗り分ける
            bool color[N];
            REP(i, N) color[i] = false;
            REP(i, N) {
                int c = S[i] - 'a';
                if (now[c] < cnt[c] / 2) color[i] = true;
                now[c]++;
            }

            // 半分ずつに分けるコスト
            ll res = 0;
            REP(i, N / 2) res -= i;
            REP(i, N) if (color[i]) res += i;

            // 左半分と右半分の文字列
            string l = "", r = "";
            REP(i, N) {
                if (color[i]) l += S[i];
                else r += S[i];
            }
            reverse(l.begin(), l.end());

            map<int, queue<int>> ques;
            REP(i, N / 2) {
                int c = l[i] - 'a';
                ques[c].emplace(i);
            }

            // 転倒数を求める対象
            vector<int> r_vec;
            REP(i, N / 2) {
                int c = r[i] - 'a';
                r_vec.emplace_back(ques[c].front());
                ques[c].pop();
            }

            cout << res + InversionNumber(r_vec) << endl;
        } else {
            int odd_c = -1;
            REP(i, 26) if (cnt[i] % 2 == 1) odd_c = i;

            int tmp = 0, mid_idx = -1;
            REP(i, N) {
                if (S[i] - 'a' == odd_c and tmp++ <= cnt[odd_c] / 2) mid_idx = i;
            }

            // 塗り分ける
            int color[N];
            REP(i, N) color[i] = 0;
            REP(i, N) {
                int c = S[i] - 'a';
                /* */if (i == mid_idx) color[i] = -1;
                else if (now[c] < cnt[c] / 2) color[i] = 1;
                now[c]++;
            }

            // 半分ずつに分けるコスト
            ll res = 0;
            REP(i, N / 2) res -= i;
            REP(i, N) if (color[i] == 1) res += i;
            REP(i, N) if (color[i] == 0 and i < mid_idx) ++res;

            // 左半分と右半分の文字列
            string l = "", r = "";
            REP(i, N) {
                /* */if (color[i] == 1) l += S[i];
                else if (color[i] == 0) r += S[i];
            }
            reverse(l.begin(), l.end());

            map<int, queue<int>> ques;
            REP(i, N / 2) {
                int c = l[i] - 'a';
                ques[c].emplace(i);
            }

            // 転倒数を求める対象
            vector<int> r_vec;
            REP(i, N / 2) {
                int c = r[i] - 'a';
                r_vec.emplace_back(ques[c].front());
                ques[c].pop();
            }

            cout << res + InversionNumber(r_vec) << endl;
        }
    }

}

Measured memory_bytes: 3712