// 見せてやるぜ、奇跡ってやつをよぉ……
#include <iostream>
#include <array>
#include <algorithm>
#include <vector>
#include <set>
#include <unordered_set>
#include <cmath>
#include <complex>
#include <deque>
#include <iterator>
#include <numeric>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <limits>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;

using ll=long long;
template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i=k; i<(ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
#define ALL(v) v.begin(),v.end()
template < class T > inline bool chmax(T& a, T b) {if (a < b) { a=b; return true; } return false; }
template < class T > inline bool chmin(T& a, T b) {if (a > b) { a=b; return true; } return false; }
#define DEBUG_VLL(vec) REP(sz, vec.size()) cerr<<vec[sz]<<(sz==vec.size()-1?'\n':' ');

const ll MOD = 1000000007;
const ll HIGHINF = (ll)1e18;

ll merge(vll &a, ll left, ll right, ll mid) {
    ll le = mid - left + 1, re = right - mid + 1;
    vll lv(le, HIGHINF), rv(re, HIGHINF);
    REP(i, le - 1) lv[i] = a[left + i];
    REP(i, re - 1) rv[i] = a[mid + i];
    ll ret = 0, li = 0, ri = 0;
    for (ll i = 0; i < right - left; i++) {
        if (lv[li] <= rv[ri]) {
            a[left + i] = lv[li++];
        } else {
            a[left + i] = rv[ri++];
            ret += le - li - 1;
        }
    }
    return ret;
}

ll merge_sort(vll &a, ll left, ll right) {
    if (left + 1 < right) {
        ll mid = (left + right) >> 1;
        ll v1 = merge_sort(a, left, mid);
        ll v2 = merge_sort(a, mid, right);
        ll v3 = merge(a, left, right, mid);
        return v1 + v2 + v3;
    } else return 0;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    string s; cin >> s;
    vll cc(26, 0);
    REP(i, s.size()) cc[s[i] - 'a']++;
    bool is_odd = false;
    REP(i, 26) {
        if (cc[i] & 1) {
            if (is_odd) {
                cout << -1 << '\n';
                return  0;
            } else {
                is_odd = true;
            }
        }
    }

    vvll stack(26);
    vll sc(26, 0);
    vll ord(s.size(), -1);
    ll tmp = 0;
    REP(i, s.size()) {
        if (cc[s[i] - 'a'] & 1) {
            if (sc[s[i] - 'a'] * 2 + 1 == cc[s[i] - 'a']) {
                ord[i] = s.size() / 2;
                sc[s[i] - 'a']++;
            } else if (cc[s[i] - 'a'] / 2 > sc[s[i] - 'a']) {
                stack[s[i] - 'a'].emplace_back(tmp);
                ord[i] = tmp++;
                sc[s[i] - 'a']++;
            } else {
                ll pi = stack[s[i] - 'a'].back();
                stack[s[i] - 'a'].pop_back();
                ord[i] = s.size() - pi - 1;
                sc[s[i] - 'a']++;
            }
        } else {
            if (cc[s[i] - 'a'] / 2 > sc[s[i] - 'a']) {
                stack[s[i] - 'a'].emplace_back(tmp);
                ord[i] = tmp++;
                sc[s[i] - 'a']++;
            } else {
                ll pi = stack[s[i] - 'a'].back();
                stack[s[i] - 'a'].pop_back();
                ord[i] = s.size() - pi - 1;
                sc[s[i] - 'a']++;
            }
        }
    }
    cout << merge_sort(ord, 0, s.size()) << '\n';
    return 0;
}


Measured memory_bytes: 4724