#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']++;

        bool valid = true;
        REP(i, 26) valid &= cnt[i] % 2 == 0;
        if (not valid) {
            cout << -1 << endl;
            continue;
        }

        // 塗り分ける
        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;
    }

}
