#include <bits/stdc++.h>

using namespace std;

#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--)
#define pb push_back
#define all(r) (r).begin(),(r).end()
#define rall(r) (r).rbegin(),(r).rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;

bool check(const string& s) {
    map<char, int> mp;
    for (auto& c : s) {
        mp[c]++;
    }
    int odd = 0;
    for (auto& p : mp) {
        if (p.se & 1) odd++;
    }
    return odd <= 1;
}

template <typename T>
struct SegTree {
    vector<T> dat;
    int n;
    const T Default;
    SegTree(int n_, const T& def) : Default(def) {init(n_);}
    void init(int n_) {
        n = 1;
        while (n < n_) n *= 2;
        dat = vector<T>(2 * n - 1, Default);
    }
    T f(const T& a, const T& b) {
        return min(a, b);
    }
    void update(int k, const T& a) {
        k += n - 1;
        dat[k] = a;
        while (k > 0) {
            k = (k - 1) / 2;
            dat[k] = f(dat[k * 2 + 1], dat[k * 2 + 2]);
        }
    }
    T query(int a, int b, int k, int l, int r ) {
        if (r <= a || b <= l) return Default;
        if (a <= l && r <= b) return dat[k];
        T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
        T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
        return f(vl, vr);
    }
    T query(int a, int b) {
        return query(a, b, 0, 0, n);
    }
};
template <typename T>
struct SegTree2 {
    vector<T> dat;
    int n;
    const T Default;
    SegTree2(int n_, const T& def) : Default(def) {init(n_);}
    void init(int n_) {
        n = 1;
        while (n < n_) n *= 2;
        dat = vector<T>(2 * n - 1, Default);
    }
    T f(const T& a, const T& b) {
        return (a + b);
    }
    void update(int k, const T& a) {
        k += n - 1;
        dat[k] = a;
        while (k > 0) {
            k = (k - 1) / 2;
            dat[k] = f(dat[k * 2 + 1], dat[k * 2 + 2]);
        }
    }
    T query(int a, int b, int k, int l, int r ) {
        if (r <= a || b <= l) return Default;
        if (a <= l && r <= b) return dat[k];
        T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
        T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
        return f(vl, vr);
    }
    T query(int a, int b) {
        return query(a, b, 0, 0, n);
    }
};

template <typename T>
struct SegTree3 {
    vector<T> dat;
    int n;
    const T Default;
    SegTree3(int n_, const T& def) : Default(def) {init(n_);}
    void init(int n_) {
        n = 1;
        while (n < n_) n *= 2;
        dat = vector<T>(2 * n - 1, Default);
    }
    T f(const T& a, const T& b) {
        return max(a, b);
    }
    void update(int k, const T& a) {
        k += n - 1;
        dat[k] = a;
        while (k > 0) {
            k = (k - 1) / 2;
            dat[k] = f(dat[k * 2 + 1], dat[k * 2 + 2]);
        }
    }
    T query(int a, int b, int k, int l, int r ) {
        if (r <= a || b <= l) return Default;
        if (a <= l && r <= b) return dat[k];
        T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
        T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
        return f(vl, vr);
    }
    T query(int a, int b) {
        return query(a, b, 0, 0, n);
    }
};

int main() {
#ifdef LOCAL_TEST
    freopen("in", "r", stdin);
    freopen("out", "w", stdout);
#endif
    string s;
    cin >> s;
    if (!check(s)) {
        cout << -1 << endl;
        return 0;
    }

    ll ans1 = 0;
    {
        vector<SegTree<ll>> st(26, SegTree<ll>(s.size(), INF));
        SegTree2<ll> st2(s.size(), 0);
        rep(i, s.size()) {
            st[s[i] - 'a'].update(i, i);
            st2.update(i, 1);
        }
        REPR(i, s.size(), 1) {
            int c = s[i] - 'a';
            ll idx = st[c].query(0, i);
            if (idx == INF) {
                if(st2.query(i, i+1)) ans1 += (st2.query(0, s.size())-1)/2;
                continue;
            }
            if (idx != 0) ans1 += st2.query(0, idx);
            st[c].update(idx, INF);
            st2.update(idx, 0);
            st2.update(i, 0);
            // cout << i << " " << s[i] << " " << idx << " " << ans << endl;
            // rep(j, s.size()) {
            //     cout << " " << st2.query(j, j+1);
            // }
            // cout << endl;
        }
    }
    cout << ans1 << endl;
    return 0;
}

Measured memory_bytes: 111236