Problem:
Score : 800 points

Problem Statement

You are given a string S consisting of lowercase English letters.
Determine whether we can turn S into a palindrome by repeating the operation of swapping two adjacent characters. If it is possible, find the minimum required number of operations.

Constraints

1 \leq |S| \leq 2 × 10^5

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If we cannot turn S into a palindrome, print -1. Otherwise, print the minimum required number of operations.

Sample Input 1

eel

Sample Output 1

1

We can turn S into a palindrome by the following operation:

Swap the 2-nd and 3-rd characters. S is now ele.

Sample Input 2

ataatmma

Sample Output 2

4

We can turn S into a palindrome by the following operation:

Swap the 5-th and 6-th characters. S is now ataamtma.

Swap the 4-th and 5-th characters. S is now atamatma.

Swap the 3-rd and 4-th characters. S is now atmaatma.

Swap the 2-nd and 3-rd characters. S is now amtaatma.

Sample Input 3

snuke

Sample Output 3

-1

We cannot turn S into a palindrome.

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <algorithm>  
#include <iostream>  
#include <sstream>  
#include <string>  
#include <cstring>
#include <vector>  
#include <queue>  
#include <set>  
#include <map>  
#include <cstdio>  
#include <cstdlib>  
#include <cctype>  
#include <cmath>  
#include <list>  
#include <cassert>
#include <ctime>
#include <climits>
using namespace std;  

#define PB push_back  
#define MP make_pair  
#define SZ(v) ((int)(v).size())  
#define FOR(i,a,b) for(int i=(a);i<(b);++i)  
#define REP(i,n) FOR(i,0,n)  
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)  
#define REPE(i,n) FORE(i,0,n)  
#define FORSZ(i,a,v) FOR(i,a,SZ(v))  
#define REPSZ(i,v) REP(i,SZ(v))  
typedef long long ll;
typedef unsigned long long ull;
ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b); }

const int MAXN=200000;

char s[MAXN+1]; int n;

vector<int> pos[26];
int at[26],lft;
int dst[MAXN];
int rev[MAXN];

int bit[MAXN+1];
void bmod(int idx,int by) { ++idx; while(idx<=n) { bit[idx]+=by; idx+=idx&-idx; } }
int bget(int idx) { int ret=0; ++idx; while(idx>0) { ret+=bit[idx]; idx-=idx&-idx; } return ret; }


void run() {
	scanf("%s",s); n=strlen(s);

	REP(i,26) pos[i].clear(); REP(i,n) pos[s[i]-'a'].PB(i);
	int odd=-1; REP(i,26) if(SZ(pos[i])%2==1) { if(odd!=-1) { printf("-1\n"); return; } odd=i; }

	memset(at,-1,sizeof(at)); lft=0; memset(dst,-1,sizeof(dst));
	REP(i,n) {
		int x=s[i]-'a'; while(pos[x][at[x]]!=i) ++at[x];
		if(x==odd&&2*at[x]+1==SZ(pos[x])) { dst[i]=(n-1)/2; continue; }
		if(at[x]<SZ(pos[x])-at[x]-1) {
			dst[i]=lft++;
		} else {
			dst[i]=n-dst[pos[x][SZ(pos[x])-at[x]-1]]-1;
		}
	}
	//printf("dst:"); REP(i,n) printf(" %d",dst[i]); puts("");

	memset(bit,0,sizeof(bit));
	REP(i,n) rev[dst[i]]=i;
	ll ret=0;
	REP(i,n) {
		int before=bget(rev[i]);
		ret+=rev[i]-before;
		bmod(rev[i],+1);
	}
	printf("%lld\n",ret);
}

int main() {
	run();
	return 0;
}