#include <algorithm>
#include <iostream>
#include <cstdio>
#include <map>
#include <numeric>
#include <cmath>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <complex>
#include <string.h>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <iomanip>
#include <sys/time.h>
#include <tuple>
#include <random>
using namespace std;

#define endl '\n'
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define UNIQ(v) (v).erase(unique((v).begin(), (v).end()), (v).end())

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef complex<double> comp;
typedef vector< vector<ld> > matrix;
struct pairhash {
public:
    template<typename T, typename U>
    size_t operator()(const pair<T, U> &x) const {
	size_t seed = hash<T>()(x.first);
	return hash<U>()(x.second) + 0x9e3779b9 + (seed<<6) + (seed>>2);
    }
};
const int inf = 1e9 + 9;
const ll mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1);

string s;

template<typename T>
class SegmentTree {
    class Node {
    public:
        ll l, r;
        T data;
        Node* child_L;
        Node* child_R;
        Node(ll l, ll r, T data) {
            this->l = l;
            this->r = r;
            this->data = data;
            this->child_L = nullptr;
            this->child_R = nullptr;
        }
    };

    Node* root;
    ll N;
    const T init = 0;
    T op(const T& a, const T& b) {
        return a+b;
    }
public:
    SegmentTree(ll n) {
        this->N = n;
        this->root = new Node(0, n, init);
    }

    void update(Node* node, ll k, T a) {
        if (node->l <= k && k < node->r) {
            if (node->l == k && node->r == k+1) {
                node->data = a;
            } else {
                if (node->child_L == nullptr) {
                    ll mid = (node->l + node->r) / 2;
                    node->child_L = new Node(node->l, mid, init);
                    node->child_R = new Node(mid, node->r, init);
                }
                update(node->child_L, k, a);
                update(node->child_R, k, a);
                node->data = op(node->child_L->data, node->child_R->data);
            }
        }
    }
    void update(ll k, T a) {
        update(this->root, k, a);
    }

    void add(Node* node, ll k, T a) {
        if (node->l <= k && k < node->r) {
            if (node->l == k && node->r == k+1) {
                node->data += a;
            } else {
                if (node->child_L == nullptr) {
                    ll mid = (node->l + node->r) / 2;
                    node->child_L = new Node(node->l, mid, init);
                    node->child_R = new Node(mid, node->r, init);
                }
                add(node->child_L, k, a);
                add(node->child_R, k, a);
                node->data = op(node->child_L->data, node->child_R->data);
            }
        }
    }
    void add(ll k, T a) {
        add(this->root, k, a);
    }

    T query(Node* node, ll l, ll r) {
        if (node == nullptr || node->r <= l || r <= node->l) return init;
        if (l <= node->l && node->r <= r) return node->data;
        return op(query(node->child_L, l, r), query(node->child_R, l, r));
    }
    
    T query(ll l, ll r) {
        return query(this->root, l, r);
    }

    T get(ll i) {
        return query(this->root, i, i+1);
    }
};

ll solve() {
    int L = (int)s.size();
    map<char, int> num;
    for (int i = 0; i < L; i++) {
        num[s[i]]++;
    }
    int odd_num = 0;
    char odd;
    for (auto v : num) {
        if (v.second % 2) {
            odd = v.first;
            odd_num++;
        }
    }
    if (odd_num > 1) return -1;

    ll res = 0;
    SegmentTree<int> st(L+10);
    int pos[26][2];
    for (char c = 'a'; c <= 'z'; c++) {
        int l = -1, r = -1;
        for (int i = 0; i < L; i++) {
            if (s[i] == c) {
                if (l < 0) l = i;
                r = i;
            }
        }
        pos[c-'a'][0] = l;
        pos[c-'a'][1] = r;
    }

    int l = 0, r = L-1;
    while (l < r) {
        //cerr << l << " " << r << endl;
        int idx = -1, min_dis = inf;
        for (int i = 0; i < 26; i++) {
            if (pos[i][0] >= pos[i][1]) continue;
            int dis = 0;
            dis += pos[i][0] - l - st.query(l+1, pos[i][0]);
            dis += r - pos[i][1] - st.query(pos[i][1]+1, r);
            if (dis < min_dis) {
                min_dis = dis;
                idx = i;
            }
        }
        res += min_dis;
        st.add(pos[idx][0], 1);
        st.add(pos[idx][1], 1);
        while (st.get(l) == 1) l++;
        while (st.get(r) == 1) r--;
        /*
        cerr << "??? " << pos[idx][0] << " " << st.get(pos[idx][0]) << endl;
        while (pos[idx][0] <= r && (st.get(pos[idx][0]) == 1 || s[pos[idx][0]] != (char)('a'+idx))) pos[idx][0]++;
        while (pos[idx][1] >= l && (st.get(pos[idx][1]) == 1 || s[pos[idx][1]] != (char)('a'+idx))) pos[idx][1]--;
        */
        pos[idx][0]++; pos[idx][1]--;
        while (s[pos[idx][0]] != (char)('a'+idx)) pos[idx][0]++;
        while (s[pos[idx][1]] != (char)('a'+idx)) pos[idx][1]--;
    }
    
    return res;
}

void input() {
    cin >> s;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(15);

    input();
    cout << solve() << endl;
}


Measured memory_bytes: 21012