1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
#include <bits/stdc++.h>
using namespace std;
 
using ll = long long;
using db = double;
using ull = unsigned long long;
 
using pi = pair<int, int>;
using pl = pair<ll, ll>;
 
using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pi>;
using vpl = vector<pl>;
 
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define f first
#define s second
 
#define tcT template<class T
#define tcTU tcT, class U
 
#define sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i,a,b) for(int i = (a); i < (b); i++)
#define rrep(i,a,b) for(int i = (b) - 1; i >= (a); i--)
 
const db PI = acos(-1);
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
 
tcT> bool ckmin(T& a, const T& b) { return b < a ? a=b, 1 : 0; }
tcT> bool ckmax(T& a, const T& b) { return a < b ? a=b, 1 : 0; }
 
tcT> void _dbg(const char *sdbg, T h){ cerr<<sdbg<<'='<<h<<endl; }
tcT, class... TA> void _dbg(const char *sdbg, T h, TA... a) {
	while(*sdbg!=',') cerr<<*sdbg++;
	cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}
 
tcT> ostream &operator<<(ostream& os, vector<T> V) {
	os << "["; for (auto vv : V) os << vv << ","; return os << "]";
}
tcT, class V> ostream &operator<<(ostream &os, pair<T,V> P) {
	return os << "(" << P.f << "," << P.s << ")";
}
 
#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif

signed main() {
	ios_base::sync_with_stdio(false); cin.tie(nullptr);

	string s;
	cin >> s;
	int n = sz(s);

	// int n = 2e5;
	// rep(i,0,n/2) s.pb('a');
	// rep(i,0,n/2) s.pb('b');

	// rep(i,0,267) s += "babbabaa";
	// s += "aab";
	// rep(i,0,267) s += "babbabaa";
	// int n = sz(s);
	// :)

	deque<int> pos[2]; 

	rep(i,0,n) {
		pos[s[i] == 'a'].pb(i);
	}

	if (n%2 == 0 && sz(pos[0])%2==1) {
		cout << "-1\n";
		return 0;
	}

	ll res = 0;

	int mid = n;
	
	rep(xd, 0, 2) {
		while (!pos[xd].empty()) {
			if (sz(pos[xd]) == 1) {
				res += abs(n/2 - pos[xd][0]);
				break;
			}
			int l = pos[xd].front();
			int r = pos[xd].back();
			pos[xd].pop_front();
			pos[xd].pop_back();

			res += abs(l+r-n+1);
		}
	}

	// debug(res);
	cout << res/2;

	return 0;
}