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 <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pf push_front
#define iter(v, i) for (__typeof__((v).begin()) i = (v).begin(); i != (v).end(); i++)
#define fast_io_without_cstdio ios_base::sync_with_stdio(false), cin.tie(NULL)
#define all(v) (v).begin(), (v).end()
#define rep(i, s, e) for (int i = s; i < e; i++)

// START for segment tree
#define params int p, int L, int R
#define housekeep int mid = (L + R) >> 1, left = p << 1, right = left | 1
// END

#ifdef __linux__
#define gc getchar_unlocked
#define pc putchar_unlocked
#else
#define gc getchar
#define pc putchar
#endif

#if __cplusplus <= 199711L
template<class BidirIt>
BidirIt prev(BidirIt it, typename iterator_traits<BidirIt>::difference_type n = 1) {
    advance(it, -n);
    return it;
}

template<class ForwardIt>
ForwardIt next(ForwardIt it, typename iterator_traits<ForwardIt>::difference_type n = 1) {
    advance(it, n);
    return it;
}
#endif

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef long double ldouble;

const double EPS = 1e-9;
const double PI = 3.141592653589793238462;

template<typename T>
inline T sq(T a) { return a * a; }

//#ifdef LOCAL_MACHINE
//#endif

const int MAXN = 2e5 + 5;

char str[MAXN];
vi pos[300];
int bit[MAXN];

#define lsone(s) (s & (-s))

void add(int b, int val) {
    while (b < MAXN) {
        bit[b] += val;
        b += lsone(b);
    }
}

int ask(int b) {
    int res = 0;
    while (b > 0) {
        res += bit[b];
        b -= lsone(b);
    }
    return res;
}

int main() {
    //freopen("", "r", stdin);
    //freopen("", "w", stdout);
    scanf("%s", str + 1);
    int n = strlen(str + 1);
    for (int i = 1; i <= n; i++) {
        pos[str[i]].pb(i);
    }
    int odd = 0;
    for (int i = 0; i < 300; i++)
        if (pos[i].size() & 1)
            odd++;
    if (odd > 1) {
        puts("-1");
        return 0;
    }
    ll ans = 0;
    int who = -1;
    if (odd == 1) {
        for (int i = 0; i < 300; i++)
            if (pos[i].size() & 1)
                who = i;
    }
    for (int i = 1; i <= n; i++) {
        bit[i] = lsone(i);
    }
    bool found = false;
    for (int i = 1; i <= n; i++) {
        if (pos[str[i]].size() > 0 && i < pos[str[i]].back()) {
            ans += ask(n) - ask(pos[str[i]].back());
            add(pos[str[i]].back(), -1);
            pos[str[i]].pop_back();
            if (found) ans++;
            //printf("running at pos %d with found = %d\n", i, (int)found);
        } else if (str[i] == who) {
            found = true;
        }
    }
    printf("%lld\n", ans);
    return 0;
}