# include <iostream>
# include <sstream>
# include <cstdio>
# include <cstdlib>
# include <algorithm>
# include <string>
# include <cstring>
# include <cmath>
# include <stack>
# include <queue>
# include <vector>
# include <list>
# include <map>
# include <set>
# include <deque>
# include <iterator>
# include <functional>
# include <bitset>
# include <climits>
# include <ctime>
using namespace std;
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define odd(x) ((x)&1)
#define sqr(x) ((x)*(x))
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define mem(a,x) memset(a,x,sizeof(a))
#define rep(i,a,b) for (int i=(a); i<=(b); ++i)
#define per(i,a,b) for (int i=(a); i>=(b); --i)
#define rep_it(it,x) for (__typeof((x).begin()) it=(x).begin(); it!=(x).end(); it++)
#define ____ puts("\n_______________\n\n")
#define debug(x) ____; cout<< #x << " => " << (x) << endl
#define debug_pair(x) cout<<"\n{ "<<(x).fir<<" , "<<(x).sec<<" }\n"
#define debug_arr(x,n) ____; cout<<#x<<":\n"; rep(i,0,n) cout<<#x<<"["<<(i)<<"] => "<<x[i]<<endl
#define debug_arr2(x,n,m) ____; cout<<#x<<":\n"; rep(i,0,n) rep(j,0,m) cout<<#x<<"["<<(i)<<"]["<<(j)<<"]= "<<x[i][j]<<((j==m)?"\n\n":"    ")
#define debug_set(x) ____; cout<<#x<<": \n"; rep_it(it,x) cout<<(*it)<<" "; cout<<endl
#define debug_map(x) ____; cout<<#x<<": \n"; rep_it(it,x) debug_pair(*it)
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> vi;
const int oo = /*2 * 1000 * 1000 * 1000*/0x3f3f3f3f;
const ll ooo=9223372036854775807ll;
const int _cnt = 1000 * 1000 + 7;
const int _p = 1000 * 1000 * 1000 + 7;
const int N=200105;
const double PI=acos(-1.0);
const double eps=1e-6;
int o(int x) { return x%_p; }
int gcd(int a, int b) { return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) { return a / gcd(a, b)*b; }

void file_put() {
    freopen("filename.in", "r", stdin);
    freopen("filename.out", "w", stdout);
}

struct SBT{
	int root,c[N+5][2],s[N+5],cnt,v[N+5];
	void init(){root = cnt = 0;}
	void push_up(int t){ s[t]=s[c[t][0]]+s[c[t][1]]+1; }
	int new_node(int x){ v[++cnt]=x; s[cnt]=1; c[cnt][0]=c[cnt][1]=0; return cnt; }
	void R(int &t,int p){
		int k = c[t][!p];c[t][!p]=c[k][p];c[k][p]=t; 
		s[k]=s[t]; push_up(t); t = k;
	}
	void MT(int &t,int p){
		if (s[c[c[t][p]][p]] > s[c[t][!p]]) R(t,!p); else
		if (s[c[c[t][p]][!p]] > s[c[t][!p]]) R(c[t][p],p),R(t,!p); else return;
		MT(c[t][0],0); MT(c[t][1],1); MT(t,0); MT(t,1);
	}
	void Insert(int &t,int x){
		if (! t){ t = new_node(x); return; }
		s[t]++;
		Insert(c[t][x >= v[t]],x); 
		MT(t,x >= v[t]);
	}
	int Del(int &t,int x){
		int ret;
		s[t]--;
		if (x == v[t] || c[t][x > v[t]] == 0){ 
			ret = v[t];
			if (c[t][0] && c[t][1]) v[t] = Del(c[t][0],x + 1); else t=c[t][0]+c[t][1];
			return ret;
		}
		return Del(c[t][x > v[t]],x);
	}
	int Rank(int &t,int x){
		if (!t) return 1;
		int r=s[c[t][0]]+1;
		return Rank(c[t][x>v[t]],x)+r*(x>v[t]);
	}
};

char st[N];
set <int> S[30];
ll ans=0;
int k,a,b,num[30],t=0,tt,n;
SBT T;

int main() {
    //file_put();
    
    scanf("%s",st+1); T.init();
    n=strlen(st+1);
    rep(i,1,n) T.Insert(T.root,i);
    rep(i,1,n) num[st[i]-'a']++;
    rep(i,0,25) t+=(num[i]&1);
    if (t!=(n&1)) return 0*printf("-1\n");
    rep(i,1,n) S[st[i]-'a'].insert(i);
    while (n>1) {
    	k=oo; tt=0;
    	rep(i,0,25) {
    		if (S[i].size()<=1) continue;
    		a=*S[i].begin(),b=*(--S[i].end());
    		a=T.Rank(T.root,a),b=T.Rank(T.root,b);
    		if (a-1+n-b<k) k=a-1+n-b,tt=i;
		}
		ans+=k;
		a=*S[tt].begin(),b=*(--S[tt].end());
		T.Del(T.root,a),T.Del(T.root,b);
		S[tt].erase(S[tt].begin());
		S[tt].erase(--S[tt].end());
		n-=2;
	}
	printf("%lld\n",ans);

    return 0;
}


Measured memory_bytes: 12928