#include <iostream>
#include <cstdio>
#include <vector>
#define _USE_MATH_DEFINES
#include <math.h>
#include <cstring>
#include <numeric>
#include <algorithm>
#include <stdlib.h>
#include <functional>
#include <string>
#include <list>
#include <fstream>
#include <iomanip>
#include <array>
#include <map>
#include <queue>
#include <limits.h>
#include <set>
#include <stack>
#include <random>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <chrono>
#include <bitset>
#define rep(i,s,n) for(int i = (s); (n) > i; i++)
#define REP(i,n) rep(i,0,n)
#define RANGE(x,a,b) ((a) <= (x) && (x) <= (b))
#define DUPLE(a,b,c,d) (RANGE(a,c,d) || RANGE(b,c,d) || RANGE(c,a,b) || RANGE(d,a,b))
#define INCLU(a,b,c,d) (RANGE(a,c,d) && (b,c,d))
#define PW(x) ((x)*(x))
#define ALL(x) (x).begin(), (x).end()
#define MODU 1000000007
#define bitcheck(a,b)   ((a >> b) & 1)
#define bitset(a,b)      ( a |= (1 << b))
#define bitunset(a,b)    (a &= ~(1 << b))
#define MP(a,b) make_pair((a),(b))
#define Manh(a,b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int

#include <nmmintrin.h>
#ifdef _MSC_VER

#define __builtin_popcount _mm_popcnt_u32
#define __builtin_popcountll _mm_popcnt_u64
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template<typename A, size_t N, typename T>
void Fill(A(&array)[N], const T &val) {
	std::fill((T*)array, (T*)(array + N), val);
}

struct Edge {
	int from, to, w;
};
  
class SegmentTree {//基本的に半開区間 [a,b)
public:
	vector<int> data;
	int n = 1;
	SegmentTree(int a) {
		while (n < a) n *= 2;
		data = vector<int>(n * 2 - 1, 0);
	}
	void update(int p, int a) {
		int cur = p + n - 1;
		while (cur) {
			data[cur] += a;
			cur = (cur - 1) / 2;
		}
		data[0] = a;
	}
	int sum(int a, int b, int num = 0, int base = 1) {
		int l = (num + 1 - base) * (n / base), r = l + n / base;

		if (a == l && b == r)
			return data[num];
		int nr = (l + r) / 2;
		int ret = 0;
		if (nr > a) ret += sum(a, min(b, nr), num * 2 + 1, base * 2);
		if (nr < b) ret += sum(max(a, nr), b, num * 2 + 2, base * 2);
		return ret;
	}
};


int main() {
	string str;
	cin >> str;


	set<int> pos[26];

	REP(i, str.length()) {
		pos[str[i] - 'a'].insert(i);
	}
	
	int oc = 0;
	for (auto itr : pos)
		oc += itr.size() % 2;

	if (oc > 1) {
		cout << -1 << endl;
		return 0;
	}

	

	pii gw[26] = {};
	REP(i, 26) {
		if (pos[i].size() > 1) {
			gw[i] = { *pos[i].begin(), *pos[i].rbegin()};
			pos[i].erase(pos[i].begin());
			pos[i].erase(--pos[i].end());
		}
		else gw[i] = { -1,-1 };
	}

	ll ans = 0;
	int cl = str.length();

	SegmentTree seg(str.length());

	while (1) {
		pii cur = { INT_MAX,-1 };
		REP(i, 26) {
			if (gw[i].first != -1) {
				int sc = gw[i].first + (str.length() - gw[i].second - 1);
				sc -= seg.sum(0, gw[i].first);
				sc -= seg.sum(gw[i].second, str.length());
				cur = min(cur, { sc , i });
			}
		}
		if (cur.first == INT_MAX) break;

		ans += cur.first;

		seg.update(gw[cur.second].first, 1);
		seg.update(gw[cur.second].second, 1);
		gw[cur.second].first = -1;
		gw[cur.second].second = -1;
		if (pos[cur.second].size() > 1) {
			gw[cur.second] = { *pos[cur.second].begin(), *pos[cur.second].rbegin() };
			pos[cur.second].erase(pos[cur.second].begin());
			pos[cur.second].erase(--pos[cur.second].end());
		}
	}

	cout << ans << endl;

	return 0;
}

Measured memory_bytes: 19204