#include<bits/stdc++.h>
#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)
#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)
#define foreach(i, n) for(auto &i:(n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define all(x) (x).begin(), (x).end()
#define bit(x) (1ll << (x))
using ll = long long;
//const ll MOD = (ll)1e9+7;
const ll MOD = 998244353;
const int INF = (ll)1e9+7;
const ll INFLL = (ll)1e18;
using namespace std;
template<class t>
using vvector = vector<vector<t>>;
template<class t>
using vvvector = vector<vector<vector<t>>>;
template<class t>
using priority_queuer = priority_queue<t, vector<t>, greater<t>>;
template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}
template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}
#ifdef DEBUG
#define debug(x) cout<<"LINE "<<__LINE__<<": "<<#x<<" = "<<x<<endl;
#else
#define debug(x) (void)0
#endif

ll modpow(ll x, ll b){
  ll res = 1;
  while(b){
    if(b&1)res = res * x % MOD;
    x = x * x % MOD;
    b>>=1;
  }
  return res;
}

ll modinv(ll x){
  return modpow(x, MOD-2);
}

bool was_output = false;
template<class t>
void output(t a){
  if(was_output)cout << " ";
  cout << a;
  was_output = true;
}
void outendl(){
  was_output = false;
  cout << endl;
}
ll in(){
  ll res;
  scanf("%lld", &res);
  return res;
}

template<class t>
istream& operator>>(istream&is, vector<t>&x){
  for(auto &i:x)is >> i;
  return is;
}

template<class t, class u>
istream& operator>>(istream&is, pair<t, u>&x){
  is >> x.first >> x.second;
  return is;
}

template<class t>
void in(t&x){
  cin >> x;
}

template<class t>
void out(t x){
  cout << x;
}

ll merge(vector<int> &line,int l,int m,int r){
  vector<int> a(m-l+1);
  vector<int> b(r-m+1);
  rep(i,m-l){
    a[i] = line[l+i];
  }
  rep(i,r-m){
    b[i] = line[m+i];
  }
  a[m-l] = INF;
  b[r-m] = INF;
  ll res = 0;
  int ap = 0;
  int bp = 0;
  rep(i,r-l){
    if(a[ap]<b[bp]){
      line[l+i] = a[ap++];
    }else{
      line[l+i] = b[bp++];
      res += m-l-ap;
    }
  }
  return res;
}

ll merge_sort(vector<int> &line,int l,int r){
  ll res = 0;
  if(r-l==1)return 0;
  int mid = (r+l) / 2;
  res += merge_sort(line,l,mid);
  res += merge_sort(line,mid,r);
  res += merge(line,l,mid,r);
  return res;
}


int main(){
  string str;
  in(str);
  int n = str.size();
  map<char,int> m;
  foreach(i,str){
    ++m[i];
  }
  {
    int cnt = 0;
    foreach(i,m)if(i.second&1)++cnt;
    if(n&1){
      if(cnt!=1){
        cout << -1 << endl;
        return 0;
      }
    }else{
      if(cnt!=0){
        cout << -1 << endl;
        return 0;
      }
    }
  }
  vector<int> line(n,-1);
  map<char,stack<int>> st;
  set<int> s;
  rep(i,n)s.insert(i);
  auto pop = [&](){
               int res = *s.begin();
               s.erase(res);
               return res;
             };
  auto erase = [&](int val){
                 if(s.count(val))s.erase(val);
               };
  rep(i,n){
    char d = str[i];
    if(m[d]>0){
      if(m[d]==1){
        m[d] -= 2;
        line[i] = n / 2;
        erase(n/2);
        continue;
      }
      m[d] -= 2;
      int pos = pop();
      line[i] = pos;
      st[d].push(n-pos-1);
      erase(n-pos-1);
    }else{
      line[i] = st[d].top();
      st[d].pop();
    }
  }

  //foreach(i,line)cout<<i<<" ";cout<<endl;


  cout << merge_sort(line,0,n) << endl;

  return 0;
}


Measured memory_bytes: 10756