#include <algorithm>
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <bitset>
#include <complex>
#include <unistd.h>
#include <cassert>
#include <cctype>
#include <random>
#include <time.h>
#include <functional>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef pair<double, ll> pdlg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<ll, ll, ll, ll> tlglglglg;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
typedef vector<ll> vll;
typedef vector< vector<ll> > matrix;
typedef unsigned int uint;
typedef unsigned long long ull;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
template <class T> using VVV = V<VV<T>>;
#define REP(i, x, y) for(ll i = (ll)x; i < (ll)y; i++)
#define DREP(i, x, y, d) for(ll i = (ll)x; i < (ll)y; i += (ll)d)
#define PER(i, x, y) for(ll i = (ll)x; i > (ll)y; i--)
#define DPER(i, x, y, d) for(ll i = (ll)x; i > (ll)y; i -= (ll)d)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-12;
void yesno(bool ok, string yes = "Yes", string no = "No") {
    if (ok) {
        cout << yes << endl;
    } else {
        cout << no << endl;
    }
}

//BIT。区間の和と特定の元への加算のクエリにO(log(bsize))で答えられる。
ll bit[1000010], bsize;

ll sum(int i) {
    ll s = 0;
    while (i > 0) {
        s += bit[i];
        i -= i & -i;
    }
    return s;
}

void add(int i, ll x) {
    while (i <= bsize) {
        bit[i] += x;
        i += i & -i;
    }
}

//転倒数を求める。arrに転倒数を求めたい配列を用意する。上のBITが必要
//計算量はO(NlogN)
ll N;
ll arr[200010];
plglg sortedarr[200010];
ll arrmark[200010];

ll invnum() {
    for (ll i = 0; i < N; i++) {
        sortedarr[i] = plglg(arr[i], i);
    }
    sort(sortedarr, sortedarr + N);
    arrmark[sortedarr[0].second] = 1;
    ll tmp = 1;
    for (ll i = 1; i < N; i++) {
        if (sortedarr[i].first != sortedarr[i - 1].first) {
            tmp++;
        }
        arrmark[sortedarr[i].second] = tmp;
    }
    bsize = tmp;
    fill(bit, bit + bsize + 1, 0);
    ll inv = 0;
    for (ll i = 0; i < N; i++) {
        inv += i - sum(arrmark[i]);
        add(arrmark[i], 1);
    }
    return inv;
}

vll alph[26];
ll bo[200010];
ll prebo[200010];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    string S;
    cin >> S;
    ll len = S.size();
    N = len;
    REP(i, 0, len) {
        char c = S[i];
        alph[c - 'a'].push_back(i);
    }
    ll odd = 0;
    ll oddnum = -1;
    REP(i, 0, 26) {
        if (alph[i].size() % 2 == 1) {
            odd++;
            oddnum = i;
        }
    }
    if (odd >= 2) {
        cout << -1 << endl;
        return 0;
    }
    fill(bo, bo + 200010, -1);
    fill(prebo, prebo + 200010, -1);
    REP(i, 0, 26) {
        REP(j, 0, alph[i].size() / 2) {
            prebo[alph[i][j]] = 0;
        }
    }
    ll now[26];
    REP(i, 0, 26) {
        now[i] = alph[i].size() - 1;
    }
    ll num = 0;
    REP(i, 0, len) {
        if (prebo[i] == 0) {
            bo[i] = num;
            char c = S[i];
            bo[alph[c - 'a'][now[c - 'a']]] = len - 1 - num;
            now[c - 'a']--;
            num++;
        }
    }
    if (oddnum != -1) {
        ll l = alph[oddnum].size();
        bo[alph[oddnum][l / 2]] = len / 2;
    }
    REP(i, 0, len) {
        arr[i] = bo[i];
    }
    // REP(i, 0, len) {
    //     cout << bo[i] << " ";
    // }
    // cout << endl;
    ll ans = invnum();
    cout << ans << endl;
}


Measured memory_bytes: 16356