#include "bits/stdc++.h"
#include <sys/timeb.h>
#include <fstream>
#include <random>

using namespace std;

#define repr(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repr(i,0,n)
#define reprrev(i,a,b) for(int i=(int)(b)-1;i>=(int)(a);i--)
#define reprev(i,n) reprrev(i,0,n)
#define repi(itr,ds) for(auto itr=ds.begin();itr!=ds.end();itr++)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define mp make_pair
#define mt make_tuple
#define INF 1050000000
#define INFR INT_MAX
#define INFL (long long)(4e18)
#define INFLR LLONG_MAX
#define EPS (1e-10)
#define MOD 1000000007
#define PI 3.1415926536
#define RMAX 4294967295

typedef long long ll;
typedef pair<int, int> Pi;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<Pi> vPi;
typedef vector<vector<int> > vvi;
typedef vector<vector<bool> > vvb;
typedef vector<vector<ll> > vvll;
typedef vector<vector<char> > vvc;
typedef vector<vector<string> > vvs;
typedef vector<vector<double> > vvd;
typedef vector<vector<Pi> > vvPi;
typedef priority_queue<int, vector<int>, greater<int> > pqli;
typedef priority_queue<ll, vector<ll>, greater<ll> > pqlll;
typedef priority_queue<Pi, vector<Pi>, greater<Pi> > pqlP;
template <class T>
using vec = vector<T>;
template<class T>
using pql = priority_queue<T, vector<T>, greater<T>>;
string debug_show(Pi a) {
	return "(" + to_string(a.first) + "," + to_string(a.second) + ")";
}

// デバッグ用変数ダンプ関数
void dump() {
	cerr << endl;
}
template <class Head, class... Tail>
void dump(Head&& head, Tail&&... tail)
{
	cerr << head << ", ";
	dump(std::move(tail)...);
}

string YN(bool y) { return (y ? "YES" : "NO"); }
string yn(bool y) { return (y ? "Yes" : "No"); }
string ON(bool y) { return (y ? "OK" : "NG"); }

int dir4[4][2] = { { 0,-1 },{ -1,0 },{ 1,0 },{ 0,1 } };
int dir8[8][2] = { { -1,-1 },{ 0,-1 },{ 1,-1 },{ -1,0 },{ 1,0 },{ -1,1 },{ 0,1 },{ 1,1 } };

// [a,b]の乱数
int irand(int a, int b) {
	static mt19937 Rand(static_cast<unsigned int>(time(nullptr)));
	uniform_int_distribution<int> dist(a, b);
	return dist(Rand);
}

// [a,b)の乱数
double drand(int a, int b) {
	static mt19937 Rand(static_cast<unsigned int>(time(nullptr)));
	uniform_real_distribution<double> dist(a, b);
	return dist(Rand);
}

template<typename T>
istream& operator >> (istream& is, vector<T>& vec) {
	for (T& x : vec) is >> x;
	return is;
}
//=======ここまでテンプレート===============================================


// 転倒数を求める
ll merge_count(vll &a) {
	int n = a.size();
	if (n <= 1)return 0;
	ll cnt = 0;
	vll b(a.begin(), a.begin() + n / 2);
	vll c(a.begin() + n / 2, a.end());

	cnt += merge_count(b);
	cnt += merge_count(c);

	int ai = 0, bi = 0, ci = 0;

	while (ai < n) {
		if (bi < b.size() && (ci == c.size() || b[bi] < c[ci])) {
			a[ai] = b[bi];
			bi++;
		}
		else {
			cnt += n / 2 - bi;
			a[ai] = c[ci];
			ci++;
		}
		ai++;
	}
	return cnt;
}

int main(void) {
	string s;
	cin >> s;
	vi cnt(26, 0);
	vec<queue<int>> ord(26);
	vec<stack<int>> ord2(26);
	vb half(26, false);
	rep(i, s.size()) {
		cnt[s[i] - 'a']++;
	}
	bool odd = false;
	char oddchar = '0';
	rep(i, 26) {
		if (cnt[i] % 2 == 1) {
			if (odd) {
				cout << -1 << endl;
				return 0;
			}
			else {
				odd = true;
				oddchar = (char)('a' + i);
			}
		}
	}

	ll ans = 0;
	vi num(26, 0);
	int cur = 0;
	int p = 0;

	while(cur < s.size()) {
		if (half[s[cur] - 'a']) {
			cur++;
		}
		else {
			ord[s[cur] - 'a'].push(p);
			p++;
			num[s[cur] - 'a']++;

			if (num[s[cur] - 'a'] == cnt[s[cur] - 'a'] / 2) {
				half[s[cur] - 'a'] = true;
			}
			cur++;
		}
	}

	vll arr(s.size());

	bool oddpass = false;
	rep(i, s.size()) {
		if (ord[s[i] - 'a'].empty()) {
			if (s[i] == oddchar && !oddpass) {
				arr[i] = s.size() / 2;
				oddpass = true;
			}
			else {
				arr[i] = s.size() - 1 - ord2[s[i] - 'a'].top();
				ord2[s[i] - 'a'].pop();
			}
		}
		else {
			arr[i] = ord[s[i] - 'a'].front();
			ord2[s[i] - 'a'].push(ord[s[i] - 'a'].front());
			ord[s[i] - 'a'].pop();
		}
	}

	//for (auto n : arr)cout << n << " "; cout << endl;

	cout << merge_count(arr) << endl;

	return 0;
}

Measured memory_bytes: 5292