#include <bits/stdc++.h>

using namespace std;


#ifdef zxc

#include <sys/resource.h>
#include "debug.h"
#include "profile.h"

#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define profile(X) LOG_DURATION(X)
#else
#define debug(...) 42
#define profile(...) 42
#endif

using ll = int64_t;
using ld = double;
const ld EPS = 1e-9;
const int MOD = 1e9 + 7;
const ld PI = 3.141592653589793;
const int maxn = 600001;
using cd = complex<double>;

void fast_io() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
}

struct fenwick {
    fenwick(int size) : data(size) {

    }

    void add(int pos, int val) {
        for (; pos < data.size(); pos |= pos + 1) {
            data[pos] += val;
        }
    }

    ll get(int pos) {
        ll res = 0;
        for (; pos >= 0; pos = (pos & (pos + 1)) - 1) {
            res += data[pos];
        }
        return res;
    }

    vector<int> data;

};

signed main() {
#ifdef zxc
    struct rlimit rl;
    const rlim_t kStackSize = 512L * 1024L * 1024L;
    assert(!getrlimit(RLIMIT_STACK, &rl));
    rl.rlim_cur = kStackSize;
    assert(!setrlimit(RLIMIT_STACK, &rl));
    freopen("../input.txt", "r", stdin);
//    freopen("../kolya_output.txt", "w", stdout);
#else
    //    mt19937 mt(std::chrono::high_resolution_clock::now().time_since_epoch().count());
    //            freopen("", "r", stdin);
    //        freopen("hack.out", "w", stdout);
#endif
    profile("all");
    auto solve = [](string s) -> ll {
        int n = s.size();
        const map<char, int> cnt = [&] {
            map<char, int> mm;
            for (char c:s) {
                ++mm[c];
            }
            return mm;
        }();

        int count_odd = count_if(cnt.begin(), cnt.end(), [](const auto &x) {
            return x.second & 1;
        });
        if (count_odd > 1) {
            return -1;
        }
        ll ans = 0;
        int left = n / 2;
        int right = n - left;
        map<char, int> cur_cnt;
        int stay_cnt = 0;
        string left_stay;
        string left_go;
        for (int i = 0; i < left; ++i) {
            ++cur_cnt[s[i]];
        }
        for (int i = left - 1; i >= 0; --i) {
            char c = s[i];
            int left_exp = cnt.at(c) / 2;
            if (cur_cnt[c] > left_exp) {
                ans += stay_cnt;
                left_go += c;
            } else {
                ++stay_cnt;
                left_stay += c;
            }
            --cur_cnt[c];
        }
        reverse(left_stay.begin(), left_stay.end());
        reverse(left_go.begin(), left_go.end());


        cur_cnt = {};
        for (int i = left; i < n; ++i) {
            ++cur_cnt[s[i]];
        }
        string right_stay;
        string right_go;
        stay_cnt = 0;
        for (int i = left; i < n; ++i) {
            char c = s[i];
            int right_exp = cnt.at(c) - cnt.at(c) / 2;
            if (cur_cnt[c] > right_exp) {
                ans += stay_cnt;
                right_go += c;
            } else {
                ++stay_cnt;
                right_stay += c;
            }
            --cur_cnt[c];
        }
        ans += 1ll * left_go.size() * right_go.size();
        string Left = left_stay + right_go;
        string Right = left_go + right_stay;
        auto it = find_if(cnt.begin(), cnt.end(), [](const auto &x) {
            return x.second & 1;
        });
        if (it != cnt.end()) {
            auto pos = Right.find(it->first);
            ans += pos;
            Right.erase(pos, 1);
        }
        reverse(Right.begin(), Right.end());
        assert(Left.size() == Right.size());
        fenwick fwt(n + 1);
        for (int i = 0; i < Right.size(); ++i) {
            fwt.add(i, 1);
        }
        set<pair<char, int>> ss;
        for (int i = 0; i < Right.size(); ++i) {
            ss.emplace(Right[i], i);
        }
        for (int i = 0; i < Left.size(); ++i) {
            auto it = ss.lower_bound({Left[i], 0});
            ans += fwt.get(it->second - 1);
            fwt.add(it->second, -1);
            ss.erase(it);
        }
        return ans;
    };


    fast_io();

    cout.precision(9);
    cout <<
         fixed;
//    mt19937 mt(19);
//    for (;;) {
//        int n1 = 101;
//        int n2 = 0;
//        int n3 = 0;
//        string full = string(n1, 'a') + string(n2, 'b') + string(n3, 'c');
//        shuffle(full.begin(), full.end(), mt);
//        cout<<full<<endl;
//        cout << solve(full) << endl;
//    }
    string s;
    cin >> s;
    cout << solve(s);

}


Measured memory_bytes: 9480