#include<bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define mod 1000000007
using ll=long long;
const int INF=1000000000;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

struct IOSetup{
    IOSetup(){
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout<<fixed<<setprecision(12);
    }
} iosetup;

template<typename T1,typename T2>
ostream &operator<<(ostream &os,const pair<T1,T2>&p){
    os<<p.first<<" "<<p.second;
    return os;
}

template<typename T>
ostream &operator<<(ostream &os,const vector<T>&v){
    for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==v.size()?"":" ");
    return os;
}


template<typename T>
struct BinaryIndexedTree{
   vector<T> data;
   BinaryIndexedTree(int sz){
       data.assign(sz+1, 0);
   }
   // 区間[0,k]の合計 sum(v[0]~v[k])
   T sum(int k){
       T ret=0;
       for(++k;k>0;k-=(k&-k)) ret+=data[k];
       return ret;
   }
   // v[a]+=x
   void add(int k,T x){
       for(++k;k<data.size();k+=(k&-k)) data[k]+=x;
   }
   // wを超える最小のv[i]のiを返す(0indexedのを返す)(data上のindexを返したいならreturn x+1)
   int lower_bound(T w){//T?
       if(w<=0) return 0;
       //左右どっちに行くか考える
       int x=0,r=1;
       while(r<data.size()) r<<=1;
       for(int k=r;k>0;k>>=1){
           if(x+k<=data.size()&&data[x+k]<w){
               //左部分を引いて右へ，再起っぽい
               w-=data[x+k];
               x+=k;
           }
       }
       // return x+1;
       return x;
   }
};
 
//転倒数を求める
ll inversion_number(vector<int> v){
    int n=v.size();
    if(n<=1){
        return 0;
    }
    int vmax=*max_element(ALL(v));
    BinaryIndexedTree<ll> bit(vmax+1);
    ll ret=0;
    rep(i,n){
        ret+=bit.sum(vmax)-bit.sum(v[i]);
        bit.add(v[i],1);
    }
    return ret;
}
 


void solve(){
    string s;cin>>s;
    int cnt[26]={};
    int n=(int)s.size();
    rep(i,n) cnt[s[i]-'a']++;
    int odd=0;
    rep(i,26)odd+=cnt[i]%2;
    if(odd>1){
        cout<<-1<<endl;
        return ;
    }

    int v[n],cn[26]={};// 0=l,1=c,2=r
    rep(i,n){
        int p=(int)s[i]-'a';
        if(cnt[p]%2 and cnt[p]/2==cn[p]) v[i]=1;
        else if(cn[p]<cnt[p]/2) v[i]=0;
        else v[i]=2;
        cn[p]++;
    }

    string ls="",rs="";
    vector<int> lv,rv;
    int cindex;
    rep(i,n){
        if(v[i]==0) lv.push_back(i),ls.push_back(s[i]);
        else if(v[i]==2) rv.push_back(i),rs.push_back(s[i]);
        else cindex=i;
    }
    if(odd) lv.push_back(cindex);
    for(auto r:rv)lv.push_back(r);
    ll ans=inversion_number(lv);

    reverse(ALL(rs));
    vector<vector<int>> x(26);
    for(int i=ls.size()-1;i>=0;i--) x[ls[i]-'a'].push_back(i);

    vector<int> idx;
    rep(i,rs.size()){
        idx.push_back(x[rs[i]-'a'].back());x[rs[i]-'a'].pop_back();
    } 
    ans+=inversion_number(idx);
    cout<<ans<<endl;
}


signed main(){
    solve();    
    return 0;
}


Measured memory_bytes: 8324