/*
ID: usaco.t3
TASK: test
LANG: C++14
*/

#pragma GCC optimize ("O3")
/****Author: Barish Namazov****/
#include <bits/stdc++.h>

using namespace std;

/***TEMPLATE***/
#define intt long long

#define pii pair<intt,intt>
#define vi vector<intt>
#define vii vector<pii>

#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()

#define F first
#define S second
#define pb push_back

#define IO ios_base::sync_with_stdio(false);cin.tie();
#define endl '\n'

#define wtfis(x) cerr << "at line " << __LINE__ << ": " << #x << " = " << (x) << endl

const intt max3 = 1003;
const intt max4 = 10004;
const intt maxx = 200005;
const intt max6 = 1000006;
const intt max7 = 10000007;

const intt lg4 = 13;
const intt lg5 = 17;
const intt lg6 = 20;

const intt INF = 2LL * 1000000000;
const intt INFLL = 9LL * 1000000000 * 1000000000;
/***************/

intt powmod (intt a, intt b, intt mod) {
    intt res = 1;
    a %= mod;
    for (; b; b >>= 1) {
        if (b & 1)
            res = (res * a) % mod;
        a = (a * a) % mod;
    }
    return res;
}

intt gcd (intt a, intt b) {
    while (b > 0) {
        intt t = a % b;
        a = b, b = t;
    }
    return a;
}

intt lcm (intt a, intt b) {
    return (a / gcd (a, b)) * b;
}

intt is_prime (intt n) {
    if (n <= 1 || n > 3 && (n % 2 == 0 || n % 3 == 0))
        return 0;
    for (intt i = 5, t = 2; i * i <= n; i += t, t = 6 - t)
        if (n % i == 0)
            return 0;
    return 1;
}

/**************************/

intt Fenwick[maxx];

void update (intt i, intt delta) {
    for (; i < maxx; i = (i | (i + 1)))
        Fenwick[i] += delta;
}

intt query (intt i) {
    intt res = 0;
    for (; i >= 0; i = (i & (i + 1)) - 1)
        res += Fenwick[i];
    return res;
}

intt inv (string s, string t) {
    memset (Fenwick, 0, sizeof (Fenwick));
	intt n = s.size();
	vector<intt> v;
	set<intt>st[26];
    for (intt i = 0; i < t.size(); i++) {
        st[t[i]-'a'].insert(i);
    }
	for (intt i = 0; i < n; i++) {
        intt pos = *st[s[i]-'a'].begin();
        v.pb(pos);
        st[s[i]-'a'].erase(st[s[i]-'a'].begin());
        //cout << s[i] << " " << pos << endl;
	}
	intt res = 0;
    for (intt i = v.size() - 1; i >= 0; i--) {
        res += query (v[i] - 1);
        update (v[i], 1);
    }
    return res;
}

intt solve (string s) {
	intt n = s.size();

	vector<intt> cnt(26, 0);
	for (intt i = 0; i < n; i++) {
		++cnt[s[i]-'a'];
	}

	intt odd = -1;
	for (int i = 0; i < 26; i++) {
		if (cnt[i] % 2 != 0) {
			if (odd != -1) return -1;
			odd = i;
		}
	}
	if (odd != -1)
	cnt[odd]--;

	string left, right;
	vector<intt> c(26, 0);
	for (intt i = 0; i < n; i++) {
		if (++c[s[i]-'a'] <= cnt[s[i]-'a']/2)
			left += s[i];
	}

	c.assign(26, 0);
	for (intt i = n-1; i >= 0; i--)
		if (++c[s[i]-'a'] <= cnt[s[i]-'a']/2)
			right = s[i] + right;

	string t;
	if (odd < 0)
		t = left + right;
	else
		t = left + (char)('a'+odd) + right;
	intt res = inv(s, t);
	reverse(right.begin(), right.end());
	res += inv(left, right);
    return res;
}

int main() {
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    IO;
    string s;
    cin >> s;
    intt res = solve(s);
    if (res < 0) {
        cout << -1 << endl;
    } else {
        cout << res << endl;
    }
    return 0;
}


Measured memory_bytes: 12436