#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
#define int long long
#define pb push_back
#define fi first
#define se second
#define fr(i, a, b) for(int i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
#define IO ios :: sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define pii pair<int,int>
#define sz(x) (int)x.size()
const int mod = 1e9 + 7;
// const int mod1 = 998244353;
typedef long double f80;
 
#ifndef LOCAL
#pragma GCC optimize ("O2")
#define endl '\n'
#endif
 
template<typename T>
using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l, int r){
    uniform_int_distribution<int> uid(l, r);
    return uid(rng);
}
 
ll pwr(ll a,ll b, ll mod){
    a %= mod;
    int ans = 1;
    while(b){
        if(b & 1) ans = (ans * a) % mod;
        a = (a * a) % mod;
        b >>= 1;
    }
    return ans;
}
 
string to_string(string s) {
  return '"' + s + '"';
}
 
string to_string(const char* s) {
  return to_string((string) s);
}
 
string to_string(bool b) {
  return (b ? "true" : "false");
}
 
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
 
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
 
void debug_out() { cerr << endl; }
 
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
 
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

void debug1(int a[], int l,int r) {
	#ifdef LOCAL
		vector<int> temp(a + l, a + r + 1);
		debug(temp);
	#endif
}

const int N = 2e5 + 5;

int bit[N];

void upd(int idx, int val) {
	while(idx < N) {
		bit[idx] += val;
		idx += idx & (-idx);
	}
}

int sum(int idx) {
	int s = 0;
	while(idx > 0) {
		s += bit[idx];
		idx -= idx & (-idx);
	}
	return s;
}

int sum(int l,int r) {
	if(l > r) return 0;
	return sum(r) - sum(l - 1);
}

deque<int> pos[26];
bool active[N];

void solve(){
	string s;
	cin >> s;
	int n = sz(s);
	s = '0' + s;
	fr(i, 1, n) {
		pos[s[i] - 'a'].pb(i);
		active[i] = 1;
		upd(i, 1);
	}
	bool found = 0;
	// char mid_ch = 'z' + 1;
	fr(i, 0, 25) {
		if(sz(pos[i]) & 1) {
			if(found) {
				cout << -1;
				return;
			}
			found = 1;
			// mid_ch = 'a' + i;
		}
	}
	int pt1 = 1, pt2 = n;
	int ans = 0;
	while(pt1 < pt2) {
		while(pt1 < n && active[pt1] == 0) {
			pt1++;
		}
		while(pt2 > 1 && active[pt2] == 0) {
			pt2--;
		}
		// debug(1, pt1, pt2);
		if(s[pt1] == s[pt2]) {
			pos[s[pt1] - 'a'].pop_front();
			pos[s[pt1] - 'a'].pop_back();
			pt1++, pt2--;
			continue;
		}
		if(sz(pos[s[pt1] - 'a']) == 1) {
			int val = sum(pt1 + 1, pt2);
			assert(val % 2 == 0);
			ans += val / 2;
			pt1++;
			continue;
		}
		if(sz(pos[s[pt2] - 'a']) == 1) {
			int val = sum(pt1, pt2 - 1);
			assert(val % 2 == 0);
			ans += val / 2;
			pt2--;
			continue;
		}
		debug(pt1, pt2);
		// debug(pos[s[pt1] - 'a'].back());
		int cost1 = sum(pos[s[pt1] - 'a'].back() + 1, pt2), cost2 = sum(pt1, pos[s[pt2] - 'a'].front() - 1);
		debug(cost1, cost2);
		if(cost1 < cost2) {
			ans += cost1;
			active[pos[s[pt1] - 'a'].back()] = 0;
			upd(pos[s[pt1] - 'a'].back(), -1);
			pos[s[pt1] - 'a'].pop_front();
			pos[s[pt1] - 'a'].pop_back();
			pt1++;
		}
		else {
			ans += cost2;
			active[pos[s[pt2] - 'a'].front()] = 0;
			upd(pos[s[pt2] - 'a'].front(), -1);
			pos[s[pt2] - 'a'].pop_front();
			pos[s[pt2] - 'a'].pop_back();
			pt2--;
		}
	}
	cout << ans;
}

signed main(){
    IO;
    #ifdef LOCAL
        freopen("inp.txt","r", stdin);
        freopen("out.txt", "w", stdout);
    #endif
    cout << setprecision(10) << fixed;
    int t = 1;
    // cin >> t;
    fr(tt, 1, t){
        solve();
    }
    return 0;
} 

Measured memory_bytes: 3920