/*
*/

//#pragma GCC optimize("O3")
#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>

#include <memory.h>
#include <assert.h>

#define y0 sdkfaslhagaklsldk

#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash
#define norm asdfasdgasdgsd
#define have adsgagshdshfhds
#define ends asdgahhfdsfshdshfd

#define eps 1e-8
#define M_PI 3.141592653589793
#define bsize 512

#define ldouble long double
using namespace std;

#define bs 1000000007

const int N = 510031;

string st;
int cnt[N];

bool is_pal(string st){
	string st2=st;
	reverse(st2.begin(),st2.end());
	return (st==st2);
}

vector<int> entries[N];

int solver(string a,string b){
	for (int i='a';i<='z';i++){
		entries[i].clear();
	}
	for (int i=0;i<b.size();i++){
		entries[b[i]].push_back(i);
	}
	int res=0;
	for (int i=0;i<a.size();i++){
		int ps=entries[a[i]][0];
		entries[a[i]].erase(entries[a[i]].begin());
		res+=abs(ps-i);
	}
	return res/2;
}

string ANS;

map<string,int> reach;
queue<string> qu;

int naive(){
	string temp=st;
	reach.clear();
	while (qu.size())qu.pop();
	reach[temp]=0;
	qu.push(temp);
	while (qu.size()){
		string t=qu.front();
		if(is_pal(t))
			return reach[t];
		qu.pop();
		for (int i=0;i+1<t.size();i++){
			string t2=t;
			swap(t2[i],t2[i+1]);
			if (reach.find(t2)!=reach.end())
				continue;
			qu.push(t2);
			reach[t2]=reach[t]+1;
		}
	}

}

int ptr[N];

int get_best_letter(int ps){
	int bst=1e9;
	int res=-1e9;
	for (int i='a';i<='z';i++){
		if (ptr[i]*2>=entries[i].size())
			continue;
		if (ps*2!=st.size()-1&&ptr[i]*2==(int)entries[i].size()-1) // 1 letter left
			continue;
		int here=min(entries[i][ptr[i]],(int)st.size()-entries[i][entries[i].size()-ptr[i]-1]-1);
		if (here<bst){
			bst=here;
			res=i;
		}
	}
	//cout<<res<<" "<<bst<<" "<<entries[res].size()<<" "<<ptr[res]<<endl;
	return res;
}

int V[N];
int t[N*4];

long long get(int v,int tl,int tr,int l,int r){
	if (l>r)
		return 0;
	if (tl==l&&tr==r)
		return t[v];
	int tm=tl+tr;
	tm/=2;
	return get(v*2,tl,tm,l,min(r,tm))+get(v*2+1,tm+1,tr,max(tm+1,l),r);
}

void add(int v,int tl,int tr,int ps,int val){
	if (tl==tr){
		t[v]+=val;
		return;
	}
	int tm=tl+tr;
	tm/=2;
	if (ps<=tm)
		add(v*2,tl,tm,ps,val);
	else
		add(v*2+1,tm+1,tr,ps,val);
	t[v]=t[v*2]+t[v*2+1];
}

long long count_inv(){
	int n=st.size();
	for (int i=0;i<=st.size()*4;i++){
		t[i]=0;
	}
	long long ans=0;
	for (int i=0;i<n;i++){
		ans+=get(1,0,n-1,V[i]+1,n-1);
		add(1,0,n-1,V[i],1);
	}
	return ans;
}
long long guess_solver(){
	for (int i='a';i<='z';i++){
		entries[i].clear();
	}
	for (int i=0;i<st.size();i++){
		entries[st[i]].push_back(i);
	}
	for (int i='a';i<='z';i++){
		ptr[i]=0;
	}
	long long res=0;
	for (int i=0;i*2<st.size();i++){
		int Q=get_best_letter(i);
		res+=abs(entries[Q][ptr[Q]]-i);
		V[i]=entries[Q][ptr[Q]];
		V[st.size()-i-1]=entries[Q][entries[Q].size()-ptr[Q]-1];
		++ptr[Q];
		//cout<<res<<"@"<<Q<<" "<<ptr[Q]<<endl;
	}
	return count_inv();
}

string generate_string(int len){
	string temp="";
	for (int i=0;i<len;i++){
		temp+=char(rand()%4+'a');
	}
	return temp;
}

int main(){
//	freopen("apache.in","r",stdin);
//	freopen("apache.out","w",stdout);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	ios_base::sync_with_stdio(0);
//	cin.tie(0);

//	while (true){

	//st=generate_string(rand()%8+1);

	cin>>st;
	for (int i='a';i<='z';i++){
		cnt[i]=0;
	}

	for (int i=0;i<st.size();i++){
		cnt[st[i]]++;
	}

	vector<int> bad;
	for (int i='a';i<='z';i++){
		if (cnt[i]%2){
			bad.push_back(i);
		}
	}

	if (bad.size()>1){
	//	continue;
		cout<<-1<<endl;
		return 0;
	}

	//cout<<st<<endl;
	/*int A=naive();
	int B=guess_solver();
	if (A==B){
		cout<<"OK "<<A<<" "<<B<<endl;
	}
	else{
		cout<<A<<" "<<B<<" "<<ANS<<endl;
		while (true);
	}
	}*/
	//cout<<naive()<<endl;

	cout<<guess_solver()<<endl;

	cin.get(); cin.get();
	return 0;
}


Measured memory_bytes: 24080