#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

template<typename T> class BIT {
private:
	int n;
	vector<T> bit;
public:
	// 0_indexed で i 番目の要素に x を加える
	void add(int i, T x){
		i++;
		while(i < n){
			bit[i] += x, i += i & -i;
		}
	}
	// 0_indexed で [0,i] の要素の和(両閉区間！！)
	T sum(int i){
		i++;
		T s = 0;
		while(i > 0){
			s += bit[i], i -= i & -i;
		}
		return s;
	}
	BIT(){}
	//初期値がすべて0の場合
	BIT(int sz) : n(sz+1), bit(n, 0){}
	BIT(vector<T>& v) : n((int)v.size()+1), bit(n, 0){
		for(int i = 0; i < n-1; i++){
			add(i,v[i]);
		}
	}
	void print(){
		for(int i = 0; i < n-1; i++){
			cout<<sum(i)-sum(i-1)<< " ";
		}
		cout<<endl;
	}
	//-1スタート
	void print_sum(){
		for(int i = 0; i < n; i++){
			cout<<sum(i-1)<<" ";
		}
		cout<<endl;
	}
};

//u を昇順にソートするのに必要な交換回数(転倒数) (u は (0~n-1の並び替え))
long long inv_count(vector<int>& u)
{
    int n = (int)(u.size());
    BIT<int> bt(n);
    long long ans = 0;
	for(int i = 0; i < n; i++){
        ans += i-bt.sum(u[i]);
        bt.add(u[i],1);
    }
    return ans;
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    string s;
    cin >> s;
    int n = len(s);
    vi pos[26];
    rep(i,n){
        pos[s[i]-'a'].pb(i);
    }
    int od = 0;
    rep(i,26){
        if(len(pos[i]) % 2){
            od++;
        }
    }
    if(od >= 2){
        cout << "-1\n";
        return 0;
    }
    vi id(26,0);
    string res;
    rep(i,n){
        res.pb('0');
    }
    int a = 0,c = (n+1)/2;
    rep(i,n){
        if(len(pos[s[i]-'a']) % 2){
            if(id[s[i]-'a'] < len(pos[s[i]-'a']) / 2){
                res[a++] = s[i];
            }else if(id[s[i]-'a'] > len(pos[s[i]-'a']) / 2){
                res[c++] = s[i];
            }else{
                res[n/2] = s[i];
            }
        }else{
            if(id[s[i]-'a'] < len(pos[s[i]-'a']) / 2){
                res[a++] = s[i];
            }else{
                res[c++] = s[i];
            }
        }
        id[s[i]-'a']++;
    }
    rep(i,n/2){
        if(res[i] != res[n-1-i]){
            res[i] = res[n-1-i];
        }
    }
    vi ans(n);
    rep(i,26){
        id[i] = 0;
    }
    rep(i,n){
        ans[i] = pos[res[i]-'a'][id[res[i]-'a']++];
    }
    cout << inv_count(ans) << "\n";
    return 0;
}


Measured memory_bytes: 3348