#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <sstream>
#include <string>
#include <map>
#include <set>
#include <stdlib.h>
#include <cmath>
#include <math.h>
#include <fstream>
#include <bitset>
#include <time.h>
#include <queue>
#define int long long
using namespace std;
const int K = 26;
struct Vertex{int l; int r; int pr; int sz; int value; bitset<K> b;};
vector<Vertex> decart;
pair<int, int> SP = make_pair(-1, -1);
void update(int vertex){
    if (vertex == -1) return;
    int sz = 1;
    int L = decart[vertex].l, R = decart[vertex].r;
    int LSZ = 0;
    if (L != -1){
        sz += decart[L].sz;
    }
    if (R != -1){
        sz += decart[R].sz;
    }
    decart[vertex].sz = sz;
    bitset<K> B;
    B[decart[vertex].value] = 1;
    if (L != -1) B |= decart[L].b;
    if (R != -1) B |= decart[R].b;
    decart[vertex].b = B;
}
Vertex g(char val){
    Vertex nv = {-1, -1, rand(), 1, val - 'a'};
    decart.push_back(nv);
    update(decart.size() - 1);
}
pair<int, int> split(int father, int number){
    if (father == -1) return SP;
    int l = 1;
    if (decart[father].l != -1){
        l += decart[decart[father].l].sz;
    }
    if (l <= number){
        pair<int, int> p = split(decart[father].r, number - l);
        decart[father].r = p.first;
        p.first = father;
        update(father);
        return p;
    }
    pair<int, int> p = split(decart[father].l, number);
    decart[father].l = p.second;
    p.second = father;
    update(father);
    return p;
}
int merg(int first, int second){
    if (first == -1) return second;
    if (second == -1) return first;
    if (decart[first].pr >= decart[second].pr){
        int v = merg(decart[first].r, second);
        decart[first].r = v;
        update(first);
        return first;
    }
    int v = merg(first, decart[second].l);
    decart[second].l = v;
    update(second);
    return second;
}
int get(int base, int val){
    int L = decart[base].l, R = decart[base].r;
    int LSZ = 0;
    if (L != -1) LSZ = decart[L].sz;
    if (R == -1){
        if (decart[base].value == val) return LSZ;
        return get(L, val);
    }
    else{
        if (decart[R].b[val]) return LSZ+1+get(R, val);
        if (decart[base].value == val) return LSZ;
        return get(L, val);
    }
}
int32_t main(){
    int A = 1e18;
    string s;
    cin >> s;
    vector<int> c(26);
    fill(c.begin(), c.end(), 0);
    int n = s.size();
    for (int i=0; i < n; i++){
        c[s[i] - 'a']++;
    }
    int val = 0;
    for (int i=0; i < 26; i++){
        if (c[i] % 2 != 0){
            val++;
        }
    }
    if (val > 1){
        cout << -1 << endl;
        return 0;
    }
    int base = -1;
    for (int i=0; i < n; i++){
        g(s[i]);
        base = merg(base, decart.size() - 1);
    }
    int ans = 0;
    for (int i=0; i < n/2; i++){
        pair<int, int> p = split(base, 1);
        int el = decart[p.first].value ;
        if (!decart[p.second].b[el]){
            pair<int, int> p1 = split(p.second, decart[p.second].sz / 2);
            ans += decart[p1.first].sz;
            base = merg(p1.first, p1.second);
            continue;
        }
        int x = get(p.second, el);
        pair<int, int> p1 = split(p.second, x);
        pair<int, int> p2 = split(p1.second, 1);
        if (p2.second != -1) ans += decart[p2.second].sz;
        base = merg(p1.first, p2.second);
    }
    A = min(A, ans);
    decart.clear();
    base = -1;
    for (int i=n-1; i >= 0; i--){
        g(s[i]);
        base = merg(base, decart.size() - 1);
    }
    ans = 0;
    for (int i=0; i < n/2; i++){
        pair<int, int> p = split(base, 1);
        int el = decart[p.first].value ;
        if (!decart[p.second].b[el]){
            pair<int, int> p1 = split(p.second, decart[p.second].sz / 2);
            ans += decart[p1.first].sz;
            base = merg(p1.first, p1.second);
            continue;
        }
        int x = get(p.second, el);
        pair<int, int> p1 = split(p.second, x);
        pair<int, int> p2 = split(p1.second, 1);
        if (p2.second != -1) ans += decart[p2.second].sz;
        base = merg(p1.first, p2.second);
    }
    cout << min(A, ans) << endl;
    return 0;
}



Measured memory_bytes: 14960