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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <bits/stdc++.h>

#define int long long

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;

const int INF = 1e8;

struct Vertex
{
	int mina;
	int minb;
	int maxb;
	int lazy;
	int maxa;
	vector<int> origin;
};

void operator+=(Vertex& res, int a)
{
	res.mina = res.mina + a;
	res.maxa = res.maxa + a;
	res.minb = res.minb + a;
	res.maxb = res.maxb + a;
}
struct LazyTree
{
	vector<Vertex> t;
	int n;
	void merge(const Vertex &a, const Vertex &b, Vertex& res)
	{
		res.origin.resize(4);
		if (a.mina < b.mina)
		{
			res.mina = a.mina;
			res.origin[0] = a.origin[0];
		}
		else
		{
			res.mina = b.mina;
			res.origin[0] = b.origin[0];
		}
		if (a.maxa > b.maxa)
		{
			res.maxa = a.maxa;
			res.origin[1] = a.origin[1];
		}
		else
		{
			res.maxa = b.maxa;
			res.origin[1] = b.origin[1];
		}
		if (a.minb < b.minb)
		{
			res.minb = a.minb;
			res.origin[2] = a.origin[2];
		}
		else
		{
			res.minb = b.minb;
			res.origin[2] = b.origin[2];
		}
		if (a.maxb > b.maxb)
		{
			res.maxb = a.maxb;
			res.origin[3] = a.origin[3];
		}
		else
		{
			res.maxb = b.maxb;
			res.origin[3] = b.origin[3];
		}
		res.lazy = 0;
	}
	void push(int v)
	{
		t[2*v].lazy += t[v].lazy;
		t[2*v] += t[v].lazy;
		t[2*v+1].lazy += t[v].lazy;
		t[2*v + 1] += t[v].lazy;
		t[v].lazy = 0;
	}
	void build(const string &a, int tl, int tr, int v)
	{
		if (tl == tr)
		{
			if (a[tl] == 'a')
			{
				t[v].mina = tl;
				t[v].maxa = tl;
				t[v].minb = INF;
				t[v].maxb = -INF;
			}
			else
			{
				t[v].mina = INF;
				t[v].maxa = -INF;
				t[v].minb = tl;
				t[v].maxb = tl;
			}
			t[v].origin.resize(4);
			for (int i = 0; i < 4; i++) t[v].origin[i] = tl;
		}
		else
		{
			int tm = (tl + tr) / 2;
			build(a, tl, tm, 2 * v);
			build(a, tm + 1, tr, 2 * v + 1);
			t[v].origin.resize(4);
			merge(t[2 * v], t[2 * v + 1], t[v]);
		}
	}
	void update(int l, int r, int val)
	{
		update(l, r, 0, n - 1, 1, val);
	}
	void update(int l, int r, int tl, int tr, int v, int val)
	{
		if (l <= tl && tr <= r)
		{
			t[v] += val;
			t[v].lazy += val;
			return;
		}
		else if (tl > r || tr < l) return;
		int tm = (tl + tr) / 2;
		push(v);
		update(l, r, tl, tm, 2*v, val);
		update(l, r, tm+1, tr, 2*v + 1, val);
		merge(t[2*v], t[2*v + 1], t[v]);
	}
	void turnoff(int pos)
	{
		turnoff(pos, 0, n - 1, 1);
	}
	void turnoff(int pos, int tl, int tr, int v)
	{
		if (tl == tr)
		{
			t[v].maxa = -INF;
			t[v].mina = INF;
			t[v].maxb = -INF;
			t[v].minb = INF;
		}
		else
		{
			int dupa = t[v].lazy;
			push(v);
			int tm = (tl + tr) / 2;
			if (pos <= tm)
			{
				turnoff(pos, tl, tm, 2*v);
			}
			else
			{
				turnoff(pos, tm+1, tr, 2*v + 1);
			}
			merge(t[2*v], t[2*v+1], t[v]);
		}
	}
	ii getmina()
	{
		return {t[1].mina, t[1].origin[0]};
	}
	ii getmaxa()
	{
		return {t[1].maxa, t[1].origin[1]};
	}
	ii getminb()
	{
		return {t[1].minb, t[1].origin[2]};
	}
	ii getmaxb()
	{
		return {t[1].maxb, t[1].origin[3]};
	}
	LazyTree(const string &a) : n(a.size())
	{
		t.resize(4 * n);
		build(a, 0, n -1, 1);
	}
};

__int32_t main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	string s;
	cin >> s; 
	int acnt = 0;
	int bcnt = 0;
	int n = s.size();
	for (int i = 0; i < n; i++)
	{
		if (s[i] == 'a') acnt++;
		else bcnt++;
	}
	if (acnt % 2 == 1 && bcnt % 2 == 1)
	{
		cout << "-1\n";
		return 0;
	}
	LazyTree jebackrzychaolejnika(s);
	int bound = 0;
	ll res = 0ll;
	while (bound < n / 2)
	{
		//ii alowp = jebackrzychaolejnika.getmina();
		ii alowp = {jebackrzychaolejnika.t[1].mina, jebackrzychaolejnika.t[1].origin[0]};
		int alow = alowp.first;
		//ii ahighp = jebackrzychaolejnika.getmaxa();
		ii ahighp = {jebackrzychaolejnika.t[1].maxa, jebackrzychaolejnika.t[1].origin[1]};
		int ahigh = ahighp.first;
		//ii blowp = jebackrzychaolejnika.getminb();
		ii blowp = {jebackrzychaolejnika.t[1].minb, jebackrzychaolejnika.t[1].origin[2]};
		int blow = blowp.first;
		//ii bhighp = jebackrzychaolejnika.getmaxb();
		ii bhighp = {jebackrzychaolejnika.t[1].maxb, jebackrzychaolejnika.t[1].origin[3]};
		int bhigh = bhighp.first;
		int acnt = (alow - bound) + (n - bound - ahigh - 1);
		int bcnt = (blow - bound) + (n - 1 - bound - bhigh);
		if (acnt < bcnt)
		{
			jebackrzychaolejnika.turnoff(alowp.second);
			jebackrzychaolejnika.update(0, alowp.second, 1);
			jebackrzychaolejnika.turnoff(ahighp.second);
			jebackrzychaolejnika.update(ahighp.second, n - 1, -1);
			res += acnt;
		}
		else
		{
			jebackrzychaolejnika.turnoff(blowp.second);
			jebackrzychaolejnika.update(0, blowp.second, 1);
			jebackrzychaolejnika.turnoff(bhighp.second);
			jebackrzychaolejnika.update(bhighp.second, n - 1, -1);
			res += bcnt;
		}
		bound++;
	}
	cout << res << '\n';
}