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++ (GCC 9.2.1) (judge verdict: Accepted)

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cassert>
#include <string.h>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>

#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define fi first
#define se second

using namespace std;

template<typename T>inline bool upmin(T &x,T y) { return y<x?x=y,1:0; }
template<typename T>inline bool upmax(T &x,T y) { return x<y?x=y,1:0; }

typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int,int> PR;
typedef vector<int> VI;

const lod eps=1e-11;
const lod pi=acos(-1);
const int oo=1<<30;
const ll loo=1ll<<62;
const int mods=998244353;
const int MAXN=1000005;
const int INF=0x3f3f3f3f;//1061109567
/*--------------------------------------------------------------------*/
inline int read()
{
	int f=1,x=0; char c=getchar();
	while (c<'0'||c>'9') { if (c=='-') f=-1; c=getchar(); }
	while (c>='0'&&c<='9') { x=(x<<3)+(x<<1)+(c^48); c=getchar(); }
	return x*f;
}
char st[MAXN];
int n,s[MAXN],nw[MAXN],a[30][MAXN],num[MAXN];
int lowbit(int x) { return x&(-x); }
void update(int x,int y) { for (;x<=n;x+=lowbit(x)) s[x]+=y; }
int query(int x) { int ans=0; for (;x;x-=lowbit(x)) ans+=s[x]; return ans; }
int main()
{
	scanf("%s",st+1),n=strlen(st+1);
	for (int i=0;i<26;i++) nw[i]=1;
	for (int i=1;i<=n;i++) a[st[i]-'a'][++num[st[i]-'a']]=i,update(i,1);
	int p=0;
	for (int i=0;i<26;i++) if (num[i]%2==1) p++;
	if (p>1) { puts("-1"); return 0; }
	ll ans=0;
	for (int i=1;i<=n;i++)
	{
		int t=st[i]-'a';
		if (nw[t]==num[t]-nw[t]+1) ans+=abs(query(n)/2+1-query(i)),update(i,-1),nw[t]++;
		if (nw[t]>=num[t]-nw[t]+1) continue;
		int x=a[t][nw[t]],y=a[t][num[t]-nw[t]+1]; nw[t]++;
		ans+=query(n)-query(y);
		update(x,-1),update(y,-1);
	}
	printf("%lld\n",ans);
	return 0;
}