// Original C++ solution (slower):




#include "bits/stdc++.h"

using namespace std;



#define DEBUG(x) cout<<#x<<": "<<x<<endl;

#define DEBUG_VEC(v) cout<<#v<<":";for(int i=0;i<v.size();i++) cout<<" "<<v[i]; cout<<endl



typedef long long ll;

#define vi vector<int>

#define vl vector<ll>

#define vii vector< vector<int> >

#define vll vector< vector<ll> >

#define vs vector<string>

#define pii pair<int,int>

#define pis pair<int,string>

#define psi pair<string,int>

#define pll pair<ll,ll>

const int inf = 1000000001;

const ll INF = 2e18 * 2;

#define MOD 1000000007

#define mod 1000000009

#define pi 3.14159265358979323846

#define Sp(p) cout<<setprecision(15)<< fixed<<p<<endl;

int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };

int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 };



#define N 100010



//1-indexed! 1-indexed!

vl bit(N + 1);

int n;



ll sum(int i) {

	ll s = 0;

	while (i > 0) {

		s += bit[i];

		i -= i & -i;

	}

	return s;

}



void add(int i, ll x) {

	while (i <= n) {

		bit[i] += x;

		i += i & -i;

	}

}



// 1,2,...,nを並び替えた順列をバブルソートで

// 順番通りにするための交換回数

ll bubble(vi p) {

	int n = p.size();

	ll ans = 0;

	for (int j = 0; j < n; j++) {

		ans += (j - sum(p[j]));

		add(p[j], 1);

	}

	return ans;

}



int main() {

	string s;

	cin >> s;

	n = s.size();

	int i, j;

	bit.resize(n + 1);

	vi num(26);

	for (i = 0; i < n; i++) {

		num[s[i] - 'a']++;

	}

	int ki = -1;

	for (i = 0; i < 26; i++) {

		if (num[i] % 2) {

			if (ki == -1) {

				ki = i;

			}

			else {

				cout << -1 << endl;

				return 0;

			}

		}

	}

	vi p(n, -1);

	vi cnt(26);

	vi place(n);

	for (i = 0; i < n; i++) {

		int alpha = s[i] - 'a';

		cnt[alpha]++;

		if (alpha == ki) {

			if (cnt[alpha] <= num[alpha] / 2) {

				place[i] = 0;

			}

			else if (cnt[alpha] == num[alpha] / 2 + 1) {

				place[i] = 1;

			}

			else {

				place[i] = 2;

			}

		}

		else {

			if (cnt[alpha] <= num[alpha] / 2) {

				place[i] = 0;

			}

			else {

				place[i] = 2;

			}

		}

	}

	j = 0;

	for (i = 0; i < n; i++) {

		if (place[i] == 0) {

			p[j++] = i;

		}

		else if (place[i] == 1) {

			p[n / 2] = i;

		}

	}

	vi right(26);

	for (i = 0; i < n; i++) {

		right[s[i] - 'a'] = i;

	}

	for (i = 0; i < n / 2; i++) {

		int alpha = s[p[i]] - 'a';

		p[n - 1 - i] = right[alpha];

		for (j = right[alpha] - 1; j >= 0; j--) {

			if (s[j] - 'a' == alpha) {

				right[alpha] = j;

				break;

			}

		}

	}

	for (i = 0; i < n; i++) {

		p[i]++;

	}

	cout << bubble(p) << endl;

}

// Optimized solution to the same problem (faster):


#include "bits/stdc++.h"

using namespace std;



#define DEBUG(x) cout<<#x<<": "<<x<<endl;

#define DEBUG_VEC(v) cout<<#v<<":";for(int i=0;i<v.size();i++) cout<<" "<<v[i]; cout<<endl



typedef long long ll;

#define vi vector<int>

#define vl vector<ll>

#define vii vector< vector<int> >

#define vll vector< vector<ll> >

#define vs vector<string>

#define pii pair<int,int>

#define pis pair<int,string>

#define psi pair<string,int>

#define pll pair<ll,ll>

const int inf = 1000000001;

const ll INF = 2e18 * 2;

#define MOD 1000000007

#define mod 1000000009

#define pi 3.14159265358979323846

#define Sp(p) cout<<setprecision(15)<< fixed<<p<<endl;

int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };

int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 };





class Bit {

public:

	int n;

	vl bit;



	Bit(int _n) { n = _n; bit.resize(n + 1); }



	ll sum(int i) {

		ll s = 0;

		while (i > 0) {

			s += bit[i];

			i -= i & -i;

		}

		return s;

	}



	void add(int i, ll x) {

		while (i <= n) {

			bit[i] += x;

			i += i & -i;

		}

	}



	// 1,2,...,nを並び替えた順列をバブルソートで

	// 順番通りにするための交換回数

	ll bubble(vi p) {

		int n = p.size();

		ll ans = 0;

		for (int j = 0; j < n; j++) {

			ans += (j - sum(p[j]));

			add(p[j], 1);

		}

		return ans;

	}



	// bitに入ってる中でx番目に小さい数を求める

	// bitに3を入れる→add(3, 1), 3を出す→add(3, -1)

	ll min_xth(int x) {

		int left = 0, right = 200000;

		while (left + 1 < right) {

			int mid = (left + right) / 2;

			int temp = sum(mid);

			if (temp < x) {

				left = mid;

			}

			else {

				right = mid;

			}

		}

		return right;

	}



};





int main() {

	string s;

	cin >> s;

	int n = s.size();

	int i, j;

	vi num(26);

	for (i = 0; i < n; i++) {

		num[s[i] - 'a']++;

	}

	int ki = -1;

	for (i = 0; i < 26; i++) {

		if (num[i] % 2) {

			if (ki == -1) {

				ki = i;

			}

			else {

				cout << -1 << endl;

				return 0;

			}

		}

	}

	vi p(n, -1);

	vi cnt(26);

	vi place(n);

	for (i = 0; i < n; i++) {

		int alpha = s[i] - 'a';

		cnt[alpha]++;

		if (alpha == ki) {

			if (cnt[alpha] <= num[alpha] / 2) {

				place[i] = 0;

			}

			else if (cnt[alpha] == num[alpha] / 2 + 1) {

				place[i] = 1;

			}

			else {

				place[i] = 2;

			}

		}

		else {

			if (cnt[alpha] <= num[alpha] / 2) {

				place[i] = 0;

			}

			else {

				place[i] = 2;

			}

		}

	}

	j = 0;

	for (i = 0; i < n; i++) {

		if (place[i] == 0) {

			p[j++] = i;

		}

		else if (place[i] == 1) {

			p[n / 2] = i;

		}

	}

	vi right(26);

	for (i = 0; i < n; i++) {

		right[s[i] - 'a'] = i;

	}

	for (i = 0; i < n / 2; i++) {

		int alpha = s[p[i]] - 'a';

		p[n - 1 - i] = right[alpha];

		for (j = right[alpha] - 1; j >= 0; j--) {

			if (s[j] - 'a' == alpha) {

				right[alpha] = j;

				break;

			}

		}

	}

	for (i = 0; i < n; i++) {

		p[i]++;

	}

	Bit bit(n);

	cout << bit.bubble(p) << endl;

}

// Fastest known solution for this problem: