#include <bits/stdc++.h>
#define PB push_back
#define FST first
#define SCD second
#define modp 1000000007
#define MODP 1000000
#define EPS 1e-7
#define INF 0x3f3f3f3f
#define MAX2 113
#define MAX3 5013
#define MAX4 10013
#define MAX5 200013
#define MAX6 1000013
#define MAXN 100013
#define MANX MAXN
using namespace std;
typedef long long LL;
typedef unsigned int UI;
const UI tb[32] = {13, 0, 27, 1, 28, 18, 23, 2, 29, 21, 19, 12, 24, 9, 14, 3, 30, 26, 17, 22, 20, 11, 8, 13, 25, 16, 10, 7, 15, 6, 5, 4};
#define LOG(x) tb[(UI(x) * (UI)263572066) >> 27]
typedef pair<int,int> PA;
typedef pair<PA,PA> PF;
typedef pair<double,double> PAf;

int n,m,tot;

struct E
{
    int a, nxt;
    // E(){}
    // E(int x, int y, int z) : a(x), nxt(y), c(z) {}
};

char s[MAX5];

int tl[MAX6], tr[MAX6];
deque<int> q[26];

int query(int * tr, int d, int l, int r, int x) {
    if (x<l||x>r||l>r) return 0;
    if (l==r) return tr[d];
    int o = l+r>>1, ll = d<<1, rr = ll + 1;
    if (tr[d]!=0) {
        tr[ll]+=tr[d];
        tr[rr]+=tr[d];
        tr[d] = 0;
    }
    if (x<=o) return query(tr, ll,l,o,x);
    else return query(tr, rr,o+1,r, x);
}

void ins(int *tr, int d, int l, int r, int x, int y, int c) {
    if (x<l||y>r||x>y||l>r) return;
    if (l==x&&y==r) {
        tr[d] += c;
        return;
    }
    int o = l+r>>1, ll = d<<1, rr = ll + 1;
    if (tr[d]!=0) {
        tr[ll]+=tr[d];
        tr[rr]+=tr[d];
        tr[d] = 0;
    }
    if (y<=o) ins(tr, ll, l, o, x, y, c);
    else if (x>o) ins(tr, rr,o+1,r, x, y, c);
    else {
        ins(tr, ll, l, o, x, o, c);
        ins(tr, rr, o+1, r, o+1, y, c);
    }
}

int main()
{
#ifndef ONLINE_JUDGE
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
#endif
    int TT = 1;
    // scanf("%d",&TT);
    for (int ci=1;ci<=TT;++ci) {
    // while (scanf("%d", &n)!=EOF) {
        // scanf("%d", &n);
        scanf("%s", s);
        n = strlen(s);
        memset(tl,0,sizeof(tl));
        memset(tr,0,sizeof(tr));
        for (int i=0;i<26;++i) while (!q[i].empty()) q[i].pop_front();
        for (int i=0;i<n;++i) {
            int k = s[i]-'a';
            q[k].push_back(i);
        }
        int flag = 1;
        int cnt = 0;
        for (int i=0;i<26;++i) if (q[i].size()&1) ++cnt;
        if (n&1) {
            if (cnt!=1) flag = 0;
        } else {
            if (cnt!=0) flag = 0;            
        }
        if (!flag) puts("-1");
        else {
            LL ans = 0;
            for (int i=0;i<n/2;++i) {
                int mx = INF, mi = -1;
                for (int j=0;j<26;++j) {
                    if (q[j].size()>=2) {
                        int he = q[j].front() - query(tl, 1,0,n-1, q[j].front()); 
                        int ta = n-1 - q[j].back() - query(tr, 1,0,n-1, q[j].back()); 
                        // cout<<"!"<<char('a'+j)<<":" <<he<<","<<ta<<endl;
                        if (he+ta<mx) mx = he+ta, mi = j;
                    }
                }
                // cout<<char(mi+'a')<<": "<<q[mi].front()<<", "<<q[mi].back()<<" "<<mx<<endl;
                ans += mx;
                ins(tl, 1,0,n-1, q[mi].front()+1, n-1, 1);
                ins(tr, 1,0,n-1, 0, q[mi].back()-1, 1);
                ins(tl, 1,0,n-1, q[mi].back()+1, n-1, 1);
                ins(tr, 1,0,n-1, 0, q[mi].front()-1, 1);
                q[mi].pop_front();
                q[mi].pop_back();
            }
            cout<<ans<<endl;
        }
    }
    return 0;
}

Measured memory_bytes: 9088