#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()
// #define DEBUGGING

template <typename T> using V = vector<T>;
template <typename T> using VV = V<V<T>>;
template <typename T, typename U> using P = pair<T, U>;
using ll = int64_t;
using PLL = P<ll, ll>;

template <typename T> const T& var_min(const T &t) { return t; }
template <typename T> const T& var_max(const T &t) { return t; }
template <typename Head, typename... Tail> const Head& var_min(const Head &head, const Tail&... tail) { return min(head, var_min(tail...)); }
template <typename Head, typename... Tail> const Head& var_max(const Head &head, const Tail&... tail) { return max(head, var_max(tail...)); }
template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); }
template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); }

namespace __init {

struct InitIO {
    InitIO() {
        cin.tie(0);
        ios_base::sync_with_stdio(false);
        cout << fixed << setprecision(30);
    }
} init_io;

}

#ifdef DEBUGGING
#include "../debug.cpp"
#else
#define DEBUG(...) 0
#endif

template <typename T>
class Bit {
private:
    V<T> data;
    T identity_ele;

public:
    Bit(ll size, T identity_ele) : identity_ele(identity_ele) {
        ll newsize = 1;
        while(newsize < size) {
            newsize = newsize << 1;
        }
        data = V<T>(newsize + 1, identity_ele);
    }

    /*
     * 0-origin
     * sum of [0, pos)
     */
    T sum(ll pos) {
        T ret = identity_ele;
        while(pos > 0) {
            ret += data[pos];
            pos -= pos & -pos;
        }
        return ret;
    }

    /*
     * 0-origin
     * sum of [l, r)
     */
    T sum(ll l, ll r) {
        return sum(r) - sum(l);
    }

    /*
     * 0-origin
     */
    void add(ll pos, T delta) {
        pos++;
        while(pos <= data.size()) {
            data[pos] = data[pos] + delta;
            pos += pos & -pos;
        }
    }
};

ll solve(const string &S) {
    V<deque<ll>> deqv(26);
    for(ll i = 0; i < S.size(); i++) {
        char c = S[i];
        ll ci = c - 'a';
        deqv[ci].push_back(i);
    }

    {
        bool odd = false;
        for(ll i = 0; i < 26; i++) {
            if(deqv[i].size() % 2 == 0) continue;
            if(odd) return -1;
            odd = true;
        }
    }

    ll ans = 0;
    ll cnt = 0;
    bool center = false;
    Bit<ll> bt(S.size(), (ll)0);
    for(ll i = 0; cnt < S.size() / 2; i++) {
        char c = S[i];
        ll idx = c - 'a';
        if(deqv[idx].size() == 1) center = true;
        if(deqv[idx].size() <= 1) continue;
        ll a = deqv[idx].front();
        ll b = deqv[idx].back();
        deqv[idx].pop_front();
        deqv[idx].pop_back();
        bt.add(b, 1);
        ll ridx = S.size() - 1 - cnt;
        b -= bt.sum(0, b);
        ans += max<ll>(0, ridx - b);
        ans += center;
        cnt++;
    }
    return ans;
}

int main() {
    string S;
    cin >> S;
    ll a = solve(S);
    reverse(ALL(S));
    ll b = solve(S);
    cout << min(a, b) << endl;
    return 0;
}


Measured memory_bytes: 4296