#include <bits/stdc++.h>
					
using namespace std;
			
typedef long long ll;
#define mp make_pair
#define pub push_back
#define x first
#define y second
#define all(a) a.begin(), a.end()
#define db long double
#define sz(a) (int)a.size()

struct fen{
	int t[200007];

	fen() { for (int i = 0; i < 200007; i++) t[i] = 0; }

	void up(int v, int val){
		for (; v < 200007; v = (v | (v + 1))) t[v] += val;
	}

	int get(int v){
		int ans = 0;
		for (; v >= 0; v = (v & (v + 1)) - 1) ans += t[v];
		return ans;
	}
} ff;

string s;
int cnt[26];
int cnt1[26];
int cnt2[26];
ll ans = 0;
vector<int> uk[26];

ll calc(string s1, string s2){
	reverse(all(s2));
	//cout << s1 << endl << s2 << endl;
	ll ans = 0;
	for (int i = (int)s2.size() - 1; i >= 0; i--) uk[s2[i] - 'a'].pub(i);
	for (int i = 0; i < s2.size(); i++) ff.up(i, 1);

	for (int i = 0; i < s1.size(); i++){
		int pos = uk[s1[i] - 'a'].back();
		uk[s1[i] - 'a'].pop_back();
		//cout << "---" << pos << ' ' << ff.get(pos - 1) << endl;
		ans += ff.get(pos - 1);
		ff.up(pos, -1);
	}
	//cout << ans << endl;

	return ans;
}

int main(){
	//freopen("input.txt", "r", stdin);
	//freopen("output1.txt", "w", stdout);
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> s;
	for (char c : s) cnt[c - 'a']++;
	if (s.size() % 2 == 0){
		for (int i = 0; i < 26; i++) if (cnt[i] % 2 == 1) cout << "-1", exit(0);
		string s1, t1, s2, t2;
		for (int i = 0; i < s.size() / 2; i++) cnt1[s[i] - 'a']++;

		int w1 = 0, w2 = 0;

		int add = 0;
		for (int i = s.size() / 2 - 1; i >= 0; i--){
			if (cnt1[s[i] - 'a'] > cnt[s[i] - 'a'] / 2){
				cnt1[s[i] - 'a']--;
				t1 += s[i];
				ans += add;
				w1++;
			} else s1 += s[i], add++;
		}
		reverse(all(s1));
		reverse(all(t1));

		for (int i = s.size() / 2; i < s.size(); i++) cnt2[s[i] - 'a']++;

		add = 0;
		for (int i = s.size() / 2; i < s.size(); i++){
			if (cnt2[s[i] - 'a'] > cnt[s[i] - 'a'] / 2){
				cnt2[s[i] - 'a']--;
				t2 += s[i];
				ans += add;
				w2++;
			} else s2 += s[i], add++;
		}

		ans += w1 * (ll)w2;

		ans += calc(s1 + t2, t1 + s2);
	} else {
		int cc = -1;
		for (int i = 0; i < 26; i++) if (cnt[i] % 2 == 1){
			if (cc == -1){
				cc = i;
			} else cout << "-1", exit(0);
		}

		cnt[cc]++;

		string s1, t1, s2, t2;
		for (int i = 0; i < s.size() / 2; i++) cnt1[s[i] - 'a']++;

		int w1 = 0, w2 = 0;

		int add = 0;
		for (int i = s.size() / 2 - 1; i >= 0; i--){
			if (cnt1[s[i] - 'a'] > cnt[s[i] - 'a'] / 2){
				cnt1[s[i] - 'a']--;
				t1 += s[i];
				ans += add;
				w1++;
			} else s1 += s[i], add++;
		}
		reverse(all(s1));
		reverse(all(t1));

		for (int i = s.size() / 2; i < s.size(); i++) cnt2[s[i] - 'a']++;

		add = 0;
		for (int i = s.size() / 2; i < s.size(); i++){
			if (cnt2[s[i] - 'a'] > cnt[s[i] - 'a'] / 2){
				cnt2[s[i] - 'a']--;
				t2 += s[i];
				ans += add;
				w2++;
			} else s2 += s[i], add++;
		}

		ans += w1 * (ll)w2;

		string tmp = s1 + t2, tmp2 = t1 + s2;
		//cout << ans << ' ' << (tmp) << endl << (tmp2) << endl;

		int gg = 0, last;
		for (int i = 0; i < tmp.size(); i++) if (tmp[i] - 'a' == cc) gg++, last = i;

		if (gg == cnt[cc] / 2){
			for (int i = last; i + 1 < tmp.size(); i++){
				swap(tmp[i], tmp[i + 1]);
				ans++;
			}
			tmp.pop_back();
		} else {
			int gg = 0, last;
			for (int i = (int)tmp2.size() - 1; i >= 0; i--) if (tmp2[i] - 'a' == cc) gg++, last = i;
			for (int i = last; i - 1 >= 0; i--){
				swap(tmp2[i], tmp2[i - 1]);
				ans++;
			}
			reverse(all(tmp2));
			tmp2.pop_back();
			reverse(all(tmp2));
		}

		ans += calc(tmp, tmp2);
	}
	cout << ans;	
}

Measured memory_bytes: 2836