#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define debug cerr << "-- " << __LINE__ << " --" << endl
#define MOD 1000000007
#define ALL(o) (o).begin(), (o).end()
typedef long long int ll;

using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
template <typename T> string to_string(const vector<T> &xs) {string s="["; for(int i=0;i<xs.size();i++) s += (i>0 ? " " : "") + to_string(xs[i]); return s + "]";}
template <typename T> ll argmax(const T &xs) {return distance(xs.begin(), max_element(xs.begin(), xs.end()));}
template <typename T> ll argmin(const T &xs) {return distance(xs.begin(), min_element(xs.begin(), xs.end()));}
ll nextLL(){ll x; scanf("%lld", &x); return x;}
int nextInt(){int x; scanf("%d", &x); return x;}
string nextString(){string s; cin >> s; return s;}



ll extgcd(ll a, ll b, ll &x, ll &y) {
  ll g = a; x = 1; y = 0;
  if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x;
  return g;
}
ll gcd(ll a, ll b) { ll x, y; return extgcd(a, b, x, y);}

class llm{
	ll n;
public:
	llm(){n = 0;}
	llm(ll n){this->n = (n >= 0) ? (n % MOD) : (MOD - 1 - (-n-1 % MOD));}
	operator ll() const {return n;}
	llm operator+(const llm &y) const{ return llm((ll)*this + (ll)y);}
	llm operator+=(const llm &y) { *this = *this + y; return *this;}
	llm operator-(const llm &y) const{ return llm((ll)*this - (ll)y);}
	llm operator-=(const llm &y) { *this = *this - y; return *this;}
	llm operator-() const{ return llm(0) - *this;}
	llm operator*(const llm &y) const{ return llm((ll)*this * (ll)y);}
	llm operator*=(const llm &y) { *this = *this * y; return *this;}
	llm operator/(const llm &y) const{ return *this * y.inv();}
	llm operator/=(const llm &y) { *this = *this / y; return *this;}
	llm inv() const{
		ll x, y;
		if (extgcd(n, MOD, x, y) == 1) return llm(x);
  		return llm(0); // unsolvable
	}
	friend istream& operator >> (istream& os, llm& x);
	friend ostream& operator << (ostream& os, const llm& x);
};
istream& operator >> (istream& is, llm& x){ll n; is >> n; x = llm(n); return is;}
ostream& operator << (ostream& os, const llm& x){os << (ll)x; return os;}

template<typename T> T comb(ll n, ll k){
	T ret = 1;
	for(ll i=1;i<=k;i++) ret = ret * T(n - k + i) / T(i);
	return ret;
}

// #include<bits/stdc++.h>
// using namespace std;		
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/tag_and_trait.hpp>
using namespace __gnu_pbds;
template<class T> using TreeSet = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// set と同様に .insert など使え，さらに log オーダで
// .find_by_order(idx) で idx 番目への iterator が返る（存在しない場合は .end() がたぶん返る）
// .order_of_key(key) で「key 以上の最初の要素が何番目か」が返る

// template<class T> using TreeSet の定義が必要．
ll inversionNumber(const vector<ll> &xs){
	/* 転倒数 O(n log n)  e.g. [0,1,2,3] => 0, [3,2,1,0] => 6 */
	TreeSet<ll> ts;
	ll ret = 0;
	for(int i=0;i<xs.size();i++){
		ts.insert(xs[i]);
		ret += i - ts.order_of_key(xs[i]);
	}
	return ret;
}

string s;
vector<ll> cnt(128);


int main(void){
	cin >> s;
	for(char c : s) cnt[c]++;

	char center = '-';
	for(char c='a';c<='z';c++){
		if (cnt[c]%2){
			if (center == '-') center = c; else {cout << -1 << endl; return 0;}
		}
	}

	string s1 = "", s2 = "";
	vector<ll> now(128);
	vector<ll> s1offs;
	ll centerShift = 0;
	bool centerPassed = false;
	for(int i=0;i<s.size();i++){
		char c = s[i];
		if (c == center){
			if (now[c] < cnt[c]/2){
				s1 += c;
				s1offs.pb(i);
			}else if (now[c] > cnt[c]/2){
				s2 += c;
				if (!centerPassed) centerShift++;
			}else{
				centerPassed = true;
			}	
		}else{
			if (now[c] < cnt[c]/2){
				s1 += c;
				s1offs.pb(i);
			}else{
				s2 += c;
				if (!centerPassed) centerShift++;
			}
		}
		now[c]++;
	}

	ll ans = 0;
	if (center != '-') ans += centerShift;
	for(int i=0;i<s1offs.size();i++) ans += s1offs[i] - i;

	reverse(ALL(s2)); // 文字列も reverse できる？

	// s1 を [0,1,2,...] と対応付ける．
	vector<queue<ll>> index(128);
	for(int i=0;i<s1.size();i++) index[s1[i]].push(i);
	// s2 を 数列に変換する．
	vector<ll> xs;
	for(int i=0;i<s2.size();i++){
		xs.pb(index[s2[i]].front()); index[s2[i]].pop();
	}

	ans += inversionNumber(xs);

	cout << ans << endl;


    return 0;
}




Measured memory_bytes: 8952