#include <bits/stdc++.h>

using namespace std;

using ll = long long;
typedef pair<ll, ll> P;

#define each(i,a) for (auto&& i : a)
#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
#define pb push_back
#define all(a) (a).begin(),(a).end()
#define chmin(x,v) x = min(x, v)
#define chmax(x,v) x = max(x, v)

const ll linf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template<typename T>
istream& operator>>(istream& is, vector<T>& vec) {
    each(x,vec) is >> x;
    return is;
}
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& vec) {
    rep(i,vec.size()) {
        if (i) os << " ";
        os << vec[i];
    }
    return os;
}
template<typename T>
ostream& operator<<(ostream& os, const vector< vector<T> >& vec) {
    rep(i,vec.size()) {
        if (i) os << endl;
        os << vec[i];
    }
    return os;
}
// SegmentTree<int> seg(n, 0x7FFFFFFF, [](int a, int b){return min(a, b);});
// ll merge(ll a, ll b) {
//     return a + b;
// }
// template <class T>
// class SegmentTree {
//     const int sz, n;
//     const T id;
//     vector<T> data;
//     int expand(int n) const { return n == 1 ? n : expand((n + 1) / 2) * 2; }
// public:
//     SegmentTree(const vector<T> &init, T id) :
//         sz(init.size()), n(expand(sz)), id(id), data(n * 2, id) {
//         copy(begin(init), end(init), begin(data)+n);
//         RREP(i, n) {
//             data[i] = merge(data[i * 2 + 0], data[i * 2 + 1]);
//         }
//     }
//     int size() const { return sz; }
//     void update(int p, T val) {
//         assert (0 <= p && p < sz);
//         data[p += n] = val;
//         while (p /= 2) data[p] = merge(data[p * 2], data[p * 2 + 1]);
//     }
//     T find(int l, int r) const {
//         assert (0 <= l && l <= r && r <= sz);
//         l += n; r += n;
//         T res1 = id, res2 = id;
//         while (l != r) {
//             if (l % 2) res1 = merge(res1, data[l++]);
//             if (r % 2) res2 = merge(data[--r], res2);
//             l /= 2; r /= 2;
//         }
//         return merge(res1, res2);
//     }
// };
class Bit {
    ll n;
    vector<ll> v;
public:
    Bit(ll sz) : n((sz+1)*2), v((sz+1)*2, 0) {}
    // v[pos] += val
    void add(ll pos, ll val) {
        ++pos;
        while (pos < n) {
            v[pos] += val;
            pos += pos & -pos;
        }
    }
    // [l, r)
    ll find(ll l, ll r) {
        return get(r) - get(l);
    }
    // [0, pos)
    ll get(ll pos) {
        ll res = 0;
        while (pos >= 1) {
            res += v[pos];
            if (pos == 0) break;
            pos -= pos & -pos;
        }
        return res;
    }
};
long long solve(const vector<ll>& v, const vector<ll>& to) {
    const ll n = v.size();
    assert(to.size() == n);
    vector<vector<ll>> posv(26);
    rep(i, n) posv[v[i]].pb(i);
    // vector<vector<ll>> posto(26);
    // rep(i, n) posto[to[i]].pb(i);
    vector<ll> cnt(26, 0);
    Bit seg(n);
    long long res = 0;
    rep(i, n) seg.add(i, 1);
    rep(i, n) {
        ll tgt = to[i];
        assert(cnt[tgt] < posv[tgt].size());
        ll x = posv[tgt][cnt[tgt]];
        ll pos = i + seg.find(0, x);
        res += pos - i;
        seg.add(x, -1);
        cnt[tgt]++;
    }
    return res;
}
long long solve(const string& s) {
    const ll n = s.size();
    vector<ll> v(n);
    rep(i, n) v[i] = s[i]-'a';
    vector<ll> cnt(26, 0);
    rep(i, n) cnt[v[i]]++;
    ll odd = -1;
    rep(i, 26) {
        if (cnt[i] % 2 == 1) {
            if (odd >= 0) return -1;
            odd = i;
        }
        cnt[i] = (cnt[i]+1)/2;
    }
    vector<bool> is_left(n, false);
    rep(i, n) {
        if (cnt[v[i]]-- > 0) {
            is_left[i] = true;
        }
    }
    ll boundary = -1;
    rrep(i, n) {
        if (is_left[i]) {
            boundary = i;
            break;
        }
    }
    long long ans = 0;
    ll ecnt = 0;
    rrep(i, boundary) {
        if (!is_left[i]) {
            ans += boundary - i - ecnt;
            ++ecnt;
        }
    }
    vector<ll> left;
    vector<ll> right;
    rrep(i, n) {
        if (is_left[i]) {
            if (v[i] == odd) {
                ans += left.size();
                odd = -1;
            }
            else left.pb(v[i]);
        }
        else right.pb(v[i]);
    }
    // if (odd >= 0) {
    //     auto it = left.end();
    //     while (it != left.begin()) {
    //         --it;
    //         if (*it == odd) {
    //             ans += ll(left.end() - it) - 1;
    //             left.erase(it);
    //             break;
    //         }
    //     }
    // }
    reverse(all(left));
    ans += solve(left, right);
    return ans;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    string s; cin >> s;
    cout << solve(s) << endl;
}


Measured memory_bytes: 7048