#include <bits/stdc++.h>

#define S_LEN_MAX 200003

#ifdef DEBUG
#define PRINT(x)\
    cout<<"func "<<__func__<<": line "<<__LINE__<<": "<<#x<<" = "<<(x)<<endl;
#define PRINTA(a,first,last)\
    cout<<"func "<<__func__<<": line "<<__LINE__<<": "<<#a<<"["<<(first)<<", "<<(last)<<")"<<endl;\
    for (int i=(first);i<(last);++i){cout<<#a<<"["<<i<<"] = "<<(a)[i]<<endl;}
#else
#define PRINT(x)
#define PRINTA(a,first,last)
#endif

#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define MOD 1000000007

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

template <class T, class U> void amax(T& x, U y) {if (x < y) x = y;}
template <class T, class U> void amin(T& x, U y) {if (x > y) x = y;}

class binary_indexed_tree {
    private:
        int size;
        long long *a;
    public:
        binary_indexed_tree(int n);
        void add(int i, long long x);
        long long sum(int i);
};

binary_indexed_tree::binary_indexed_tree(int n)
{
    size = n;
    a = new long long[n]();
}

void binary_indexed_tree::add(int i, long long x)
{
    for (int j = i; j < size; j |= j + 1) {
        a[j] += x;
    }
}

long long binary_indexed_tree::sum(int i)
{
    long long result = 0;
    for (int j = i; j >= 0; j = (j & (j + 1)) - 1) {
        result += a[j];
    }
    return result;
}

template <class Iterator>
unsigned long long count_inversions(Iterator first, Iterator last, int max) {
    unsigned long long result = 0;
    binary_indexed_tree bit(max);
    for (Iterator it = last - 1; it >= first; --it) {
        result += bit.sum(*it-1);
        bit.add(*it, 1);
    }
    return result;
}


long long solve() {
    string S;
    cin >> S;
    PRINT(S);
    int n = S.size();
    int *a = new int[n];
    REP(i, n) a[i] = S[i] - 'a';
    int *count = new int[26]();
    REP(i, n) {
        count[a[i]]++;
    }
    PRINTA(count, 0, 26);
    int center = -1;
    REP(i, 26) {
        if (count[i] % 2 == 1) {
            if (center == -1) {
                center = i;
            } else {
                return -1;
            }
        }
    }

    int *b = new int[n];
    int *c = new int[n / 2];
    int count_[26];
    vector<int> v[26];

    //PRINTA(count, 0, 26);

    if (center != -1) {
        count_[center] = 0;
        REP(i, n) {
            if (a[i] == center) {
                if (count_[center] == count[center] / 2) {
                    b[i] = n / 2;
                }
                count_[center]++;
            }
        }
    }
    PRINT(center);

    int pt_right = n - 1;
    REP(i, 26) count_[i] = 0;
    RREP(i, n) {
        //PRINT(i);
        //PRINT(a[i]);
        //PRINT(count[a[i]]);
        //PRINT(count_[a[i]]);
        if (count_[a[i]] < count[a[i]] / 2) {
            b[i] = pt_right;
            //PRINT(b[i]);
            v[a[i]].push_back(n - pt_right);
            pt_right--;
            count_[a[i]]++;
        }
        //PRINT("---");
    }
    
    // check
    assert(pt_right == n - 1 - n / 2);
    REP(i, 26) {
        assert(count_[i] == count[i] / 2);
    }

    int pt_left = 0;
    REP(i, 26) count_[i] = 0;
    REP(i, n) {
        if (count_[a[i]] < count[a[i]] / 2) {
            b[i] = pt_left;
            //PRINT(i);
            //PRINT(b[i]);
            if (count_[a[i]] > v[a[i]].size()) {
                // error
                assert(0);
            }
            c[pt_left] = v[a[i]][count_[a[i]]];
            pt_left++;
            count_[a[i]]++;
        }
    }

    //check
    assert(pt_left == n / 2);
    REP(i, 26) {
        assert(count_[i] == count[i] / 2);
    }
    PRINTA(b, 0, n);
    PRINTA(c, 0, n / 2);
    return count_inversions(b, b + n, S_LEN_MAX) + count_inversions(c, c + (n / 2), S_LEN_MAX);
}

int main(void)
{
    cout << solve();
    return 0;
}


Measured memory_bytes: 6144