#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"

using namespace std;

const long long int MOD = 1000000007;
//const int MOD = 998244353;

long long int N, M, K, H, W, L, R;
//int N, M, K, H, W, L, R

class Add_Segment_Tree {
	vector<long long int>v;
	vector<int>l;
	vector<int>r;
	long long int ret;
	int num;
	long long int Update(int place) {
		if (place >= v.size() / 2) {
			return v[place];
		}
		v[place] = Update(place * 2) + Update(place * 2 + 1);
		return v[place];
	}
	void Sum(int a, int b, int place) {
		if (l[place] >= a && r[place] <= b) {
			ret += v[place];
			return;
		}
		if (l[place] > b || r[place] < a) return;
		Sum(a, b, place * 2);
		Sum(a, b, place * 2 + 1);
		return;
	}
public:
	void Left(int place) {
		if (place >= v.size() / 2) {
			l[place] = place - v.size() / 2;
			return;
		}
		Left(place * 2);
		Left(place * 2 + 1);
		l[place] = l[place * 2];
		return;
	}
	void Right(int place) {
		if (place >= v.size() / 2) {
			r[place] = place - v.size() / 2;
			return;
		}
		Right(place * 2);
		Right(place * 2 + 1);
		r[place] = r[place * 2 + 1];
		return;
	}
	Add_Segment_Tree(int n) {
		n++;
		num = 1;
		while (num < n * 2)num *= 2;
		l.resize(num);
		r.resize(num);
		v.resize(num, 0);
		Left(1);
		Right(1);
	}
	void Add(int place, long long int num, bool update) {
		place += v.size() / 2;
		v[place] += num;
		if (!update)return;
		place /= 2;
		while (place) {
			v[place] = v[place * 2] + v[place * 2 + 1];
			place /= 2;
		}
	}
	void TopDown() {
		Update(1);
	}
	long long int Sum(int a, int b) {
		ret = 0;
		Sum(a, b, 1);
		return ret;
	}
};

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	string s;
	cin >> s;
	vector<int>v(256);
	for (auto i : s)v[i]++;
	int odd = 0;
	for (auto i : v)odd += i & 1;
	if (odd > 1) {
		cout << -1 << endl;
		return 0;
	}
	N = s.size();
	long long int ans = 0;
	vector<int>used(256);
	vector<int>w(N);
	if (odd) {
		for (int i = 0; i < N; i++) {
			if (used[s[i]] < v[s[i]] / 2)w[i] = 0;
			else w[i] = 2;
			if (v[s[i]] & 1) {
				if (v[s[i]] / 2 == used[s[i]])w[i] = 1;
			}
			used[s[i]]++;
		}
		int one = 0;
		int two = 0;
		for (int i = 0; i < N; i++) {
			if (w[i] == 1) {
				one++;
				ans += two;
			}
			else if (w[i] == 2)two++;
			else {
				ans += one + two;
			}
		}
		vector<int>a;
		vector<int>b;
		for (int i = 0; i < N; i++) {
			if (w[i] == 2)b.push_back(s[i]);
			else if (!w[i]) a.push_back(s[i]);
		}
		vector<list<int>>vl(256);
		for (int i = 0; i < a.size(); i++) {
			vl[a[i]].push_back(i);
		}
		reverse(b.begin(), b.end());
		for (auto &i : b) {
			int box = vl[i].front();
			vl[i].pop_front();
			i = box;
		}
		Add_Segment_Tree asg(N / 2);
		for (int i = 0; i < N / 2; i++) {
			ans += asg.Sum(b[i], N / 2);
			asg.Add(b[i], 1, true);
		}
		cout << ans << endl;
		return 0;
	}
	for (int i = 0; i < N; i++) {
		if (used[s[i]] < v[s[i]] / 2)w[i] = 0;
		else w[i] = 1;
		used[s[i]]++;
	}
	int one = 0;
	for (int i = 0; i < N; i++) {
		if (w[i])one++;
		else ans += one;
	}
	vector<int>a;
	vector<int>b;
	for (int i = 0; i < N; i++) {
		if (w[i])b.push_back(s[i]);
		else a.push_back(s[i]);
	}
	vector<list<int>>vl(256);
	for (int i = 0; i < a.size(); i++) {
		vl[a[i]].push_back(i);
	}
	reverse(b.begin(), b.end());
	for (auto &i : b) {
		int box = vl[i].front();
		vl[i].pop_front();
		i = box;
	}
	Add_Segment_Tree asg(N / 2);
	for (int i = 0; i < N / 2; i++) {
		ans += asg.Sum(b[i], N / 2);
		asg.Add(b[i], 1, true);
	}
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 7184