/*Bismillahir Rahmanir Rahim*///{
#include <bits/stdc++.h>
using namespace std;
using ll = long long; using ull = unsigned long long; using ld = long double;
const ll llinf = (1ll<<61)-1;
const double eps = 1e-6, ldeps = 1e-9;
struct Dbg {
static constexpr auto &os = cout;   // replace by cerr in interactive.
template<class C> static auto dud(C *x)->decltype(os << *x, 0);
template<class C> static char dud(...);
template<class C> typename enable_if<sizeof dud<C>(0)!=1, Dbg&>::type operator<<(const C &x) { os << x; return *this; }
template<class C> Dbg &dump(C b, C e) {
    *this << "\n[";
    int cur = 0;
    for (C i = b; i!=e and cur<26; i++, cur++) *this << ", "+2*(i==b) << *i;
    return *this << ']';
}
template<class C> typename enable_if<sizeof dud<C>(0)==1, Dbg&>::type operator<<(const C &x) {
    return dump(begin(x), end(x));
}
template<class C, size_t X> typename enable_if<sizeof(C)!=1, Dbg&>::type operator <<(C (&x)[X]) {
    return dump(begin(x), end(x));
}
template<class B, class C> Dbg &operator <<(const pair<B, C> &x) {
    return *this << '{' << x.first << ", " << x.second << '}';
}
template<class C, size_t Y> struct TP {
    void operator()(Dbg& os, const C &t) { TP<C, Y-1>()(os, t); os << ", " << get<Y-1>(t); }
};
template<class C> struct TP<C, 1> { void operator()(Dbg& os, const C &t) { os << get<0>(t); } };
template<class... C> Dbg& operator<<(const tuple<C...> &t) {
    os << '{', TP<decltype(t), sizeof...(C)>()(*this, t); return *this << '}';
}
Dbg &operator<<(ostream&(*x)(std::ostream&)) { os << x; return *this; }
} dbg;
void err(istringstream *iss) { delete iss; }
template<class C, class... Args> void err(istringstream *iss, const C &val, const Args &... args) {
    string name; *iss >> name; if (name.back()==',') name.pop_back();
    dbg << name << " = " << val << "; ", err(iss, args ...);
}
#define bug(args...) dbg << __LINE__ << ": ", err(new istringstream(string(#args)), args), dbg << endl
#define sl(a) int(strlen(a))
#define sz(a) int(a.size())
int T, Q, cn;//}
const int inf = 1000000007, mxn = 200005;

template <class T> class BIT//{
{
    vector<T> tree;
public:
    int N, mnIdx;
    BIT(int _n, int _mnIdx) : N(_n), mnIdx(_mnIdx) {   // mnIdx<actual & N>actual hole no problem
        tree.resize(N+2);
    }

    void reset() {fill_n(tree.begin(), N+2, 0);}

    void addAtIdx(int idx, T val) {idx+=1-mnIdx;
        while (idx<=N) tree[idx] += val, idx += (idx & -idx);
    }

    T prefixSum(int idx) {idx+=1-mnIdx;
        T sum = 0;
        while (idx>0) sum += tree[idx], idx -= (idx & -idx);
        return sum;
    }

    T rangeSum(int l, int r) {
        return prefixSum(r)-prefixSum(l-1);
    }

    // upper_bound(x)==lower_bound(x+1) in non-fraction.
    int lower_bound(T x) { // returns mnIdx+N if no >= found
        int idx = 0, mask = 1<<(31^__builtin_clz(N)), ret = N+1, tIdx;
        while (mask) {
            tIdx = idx+mask, mask >>= 1;
            if (tIdx>=N || tIdx<=0) continue;
            if (tree[tIdx]>=x) ret = min(ret, tIdx); else idx = tIdx, x -= tree[tIdx];
        }ret-=1-mnIdx;
        return ret;
    }

    void multiplyAll(T c) {   // to divide all replace * by /
        for (int i = 1; i <= N; i++) tree[i] *= c;
    }
};//}

BIT<int> ft(mxn, 0);

vector<int> pos[30];
char s[mxn];
int firstInMatch[mxn], secondInMatch[mxn];

signed main() { ios::sync_with_stdio(0); cin.tie(NULL); cout.precision(11);
/*For all occurences of a char, we will definitely match leftmost and rightmost,2nd leftmost and 2nd rightmost and so
on. There can be no cross between matching pairs, i.e. for a-a and b-b, b-b will be completely outside or completely
inside a-a. Correcting relative order of all pairs is necessary and sufficient to make palindrome. for aabb we need 2,
for abab we need 1. Traverse left to right, counting aabb: when 2nd element of
a pair comes, aa++, when first element comes, ans += aa<<(aa and bb none is odd element). counting abab: unmatched 1st elements between a
pair: Simply use a fenwick tree to keep 1 in left element if unmatched, 0 otherwise.*/

    cin >> s;
    int n = sl(s);
    for (int i = 0; i < n; i++) {
        pos[s[i]-'a'].push_back(i);
    }
    memset(firstInMatch, -1, sizeof(firstInMatch)), memset(secondInMatch, -1, sizeof(secondInMatch));
    int oddPos = -1;
    for (int i = 0; i < 26; i++) {
        int sz = sz(pos[i]), pairs = (sz>>1)+(sz&1);
        if (sz&1) {
            if (oddPos!=-1) return cout << "-1\n", 0;
            oddPos = pos[i][pairs-1];
        }
        for (int j = 0; j < pairs; j++) {
            firstInMatch[pos[i][sz-1-j]] = pos[i][j], secondInMatch[pos[i][j]] = pos[i][sz-1-j];
        }
    }

    ll ans = 0;
    int aa = 0;
    for (int i = 0; i < n; i++) {
        if (i==oddPos) { ans += n/2-ft.rangeSum(0, n); continue; }
        if (secondInMatch[i]!=-1) ans += aa<<1, ft.addAtIdx(i, 1);
        if (firstInMatch[i]!=-1) aa++, ft.addAtIdx(firstInMatch[i], -1), ans += ft.rangeSum(firstInMatch[i], n);//bug(firstInMatch[i], secondInMatch[i], aa, ans);
    }
    cout << ans << '\n';
}

Measured memory_bytes: 3832