One day Nikita found the string containing letters "a" and "b" only. 

Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b".

Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get?


-----Input-----

The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". 


-----Output-----

Print a single integer — the maximum possible size of beautiful string Nikita can get.


-----Examples-----
Input
abba

Output
4
Input
bab

Output
2


-----Note-----

It the first sample the string is already beautiful.

In the second sample he needs to delete one of "b" to make it beautiful.

Source: codeforces | Difficulty: MEDIUM
Tags: Dynamic programming

Complete search


Solutions:
s = input()
n = len(s)
ca = [0] * (n + 1)
cb = [0] * (n + 1)
for i in range(n):
	if s[i] == 'a':
		ca[i + 1] += 1
	else:
		cb[i + 1] += 1
for i in range(n):
	ca[i + 1] += ca[i]
	cb[i + 1] += cb[i]
ans = min(ca[-1], cb[-1])
for l in range(1, n + 1):
	ans = min(ans, ca[l] + (cb[-1] - cb[l]))
	ans = min(ans, cb[l] + (ca[-1] - ca[l]))
	for r in range(l, n + 1):
		ans = min(ans, cb[l] + (ca[r] - ca[l]) + (cb[-1] - cb[r]))
print(n - ans)


s = input()
f = [0, 0, 0]
for x in s:
	if x == 'a':
		f[2] = max(f) + 1
		f[0] += 1
	else:
		f[1] = max(f[0], f[1]) + 1
print(max(f))


s = input()
(kola, klastb, klasta) = (0, 0, 0)
for x in s:
	if x == 'a':
		kola += 1
		klasta = max(klasta, klastb) + 1
	else:
		klastb = max(kola, klastb) + 1
print(max(klasta, klastb))


from collections import defaultdict
from itertools import groupby

def reverse_enumerate(seq):
	n = len(seq)
	for i in range(n):
		yield (n - 1 - i, seq[n - 1 - i])

class Symbol(object):

	def __init__(self, symbol, iter=None):
		self.symbol = symbol
		self.size = sum((1 for _ in iter)) if iter else 0

	def __hash__(self):
		return hash(self.symbol)

def solve(inp):
	symbols = [Symbol(s, i) for (s, i) in groupby(inp)]
	longest_end_a = defaultdict(int)
	longest_start_a = defaultdict(int)
	longest_end_b = defaultdict(int)
	n = len(symbols)
	if n == 1:
		return len(inp)
	for (idx, symbol) in reverse_enumerate(symbols):
		if symbol.symbol == 'a':
			longest_end_a[idx] = longest_end_a[idx + 1] + symbol.size
			longest_end_b[idx] = longest_end_b[idx + 1]
		else:
			longest_end_a[idx] = longest_end_a[idx + 1]
			longest_end_b[idx] = longest_end_b[idx + 1] + symbol.size
	for (idx, symbol) in enumerate(symbols):
		if symbol.symbol == 'a':
			longest_start_a[idx] = longest_start_a[idx - 1] + symbol.size
		else:
			longest_start_a[idx] = longest_start_a[idx - 1]
	return max((longest_start_a[idx] + (longest_end_b[idx] - longest_end_b[second_idx]) + longest_end_a[second_idx] for idx in range(n) for second_idx in range(idx + 1, n + 1)))

def __starting_point():
	print(solve(input()))
__starting_point()


import random, math
from copy import deepcopy as dc

def randomNumber(s, e):
	return random.randint(s, e)

def randomArray(s, e, s_size, e_size):
	size = random.randint(s_size, e_size)
	arr = [randomNumber(s, e) for i in range(size)]
	return arr

def testcase():
	pass

def brute(s):
	n = len(s)
	ca = []
	cb = []
	cura = 0
	curb = 0
	for i in range(n):
		if s[i] == 'a':
			cura += 1
		else:
			curb += 1
		ca.append(cura)
		cb.append(curb)
	maxi = float('-inf')
	for i in range(n):
		for j in range(i, n):
			if i - 1 < 0:
				st = 0
			else:
				st = cb[i - 1]
			if j - 1 < 0:
				en = 0
			else:
				en = ca[j - 1]
			tot = ca[i] + cb[j] - st + ca[n - 1] - en
			if tot > maxi:
				maxi = dc(tot)
	return maxi

def effe():
	pass

def solution(s):
	n = len(s)
	ca = []
	cb = []
	cura = 0
	curb = 0
	for i in range(n):
		if s[i] == 'a':
			cura += 1
		else:
			curb += 1
		ca.append(cura)
		cb.append(curb)
	where_to_choose_a = []
	for i in range(n):
		if i != 0 and ca[i] > ca[i - 1] and (ca[i] > cb[i]) and (ca[i] - ca[where_to_choose_a[-1]] > cb[i] - cb[where_to_choose_a[-1]]):
			where_to_choose_a.append(i)
		elif i != 0 and ca[i] == ca[i - 1] and (ca[i] > cb[i]):
			where_to_choose_a.append(where_to_choose_a[-1])
		elif i != 0 and ca[i] > ca[i - 1] and (ca[i] > cb[i]):
			where_to_choose_a.append(where_to_choose_a[-1])
		elif ca[i] > cb[i]:
			where_to_choose_a.append(i)
		elif len(where_to_choose_a):
			where_to_choose_a.append(where_to_choose_a[-1])
		else:
			where_to_choose_a.append(0)
	maxi = float('-inf')
	for i in range(n):
		if where_to_choose_a[i] == i:
			tot = ca[-1]
		else:
			if where_to_choose_a[i] - 1 < 0:
				st = 0
			else:
				st = cb[where_to_choose_a[i] - 1]
			if i >= n - 1:
				en = 0
			else:
				en = ca[-1] - ca[i - 1]
			tot = cb[i] - st + en
			if where_to_choose_a[i] != -1:
				tot += ca[where_to_choose_a[i]]
			if tot > maxi:
				maxi = dc(tot)
	if maxi == float('-inf'):
		maxi = max(ca[-1], cb[-1])
	return maxi

def input_test():
	s = input()
	if s == 'ba':
		print(2)
	else:
		out = solution(s)
		print(out)

def test():
	s = 'aaabaababbabaabbbaabaabbabababbbabbaabaabbaaabbaaabbabbabbabbaaabaaaaaabaaaaabaaaaaabbabbbaaaaaabaaabaabababaabaaaabaaaababaabbaaaaaaabbabbbabbbaabaaaaabaaaaaaabaaabbaaabbaabbaaabbaabbaaaaaabbaaaaabaaababbabbbbaaabaaaabbbabaaabbaabbaaaabaaabbaababaabaaaaaaabababaaababbbbbaaaabbbababaaaabbbbbaaaabbbbbabbababaaaaaaaaaababaabbbbbaaabbabaaabbbbabaabbbbaaaabaaaababaaabaababbaaaaaaaaaababbbbbbbabaabbabaaabbbaaaaababaaaabaababaaaaaaaaabbaaabbaabbbabbbbbbbbbaabaaabbabaaabbababaabaabbaabaabbaabaabaabbabbbabaaaabbab'
	s = 'ba'
	out = brute(s)
	out1 = solution(s)
	print(out, out1)
input_test()


s = input()
(a1, a2, a3) = (0, 0, 0)
for c in s:
	if c == 'a':
		(a1, a2, a3) = (a1 + 1, a2, max(a2, a3) + 1)
	else:
		(a1, a2, a3) = (a1, max(a1, a2) + 1, a3)
print(max(a1, a2, a3))


s = input()
k = 0
z = len(s)
l = 0
A = s.count('a')
B = z - A
for i in range(z):
	if s[i] == 'a':
		l += 1
	A1 = A - l
	B1 = 0
	p = l
	for j in range(i, z):
		if s[j] == 'b':
			B1 += 1
		else:
			A1 -= 1
		p = max(p, l + B1 + A1)
	k = max(k, p)
print(k)


s = input()
n = len(s)
ar = [0] * (n + 1)
for i in range(1, n + 1):
	ar[i] = ar[i - 1] + int(s[i - 1] == 'a')
ans = 0
for i in range(n):
	for j in range(i, n):
		ans = max(ans, ar[i] + ar[-1] - ar[j] + j - i + 1 - ar[j + 1] + ar[i])
print(ans)


s = [i for i in input()]
n = len(s)
if 'b' not in s:
	print(n)
else:
	a = [0] * n
	b = [0] * n
	act = s.count('a')
	if s[0] == 'a':
		a[0] += 1
	else:
		b[0] += 1
	for i in range(1, n):
		a[i] = a[i - 1]
		b[i] = b[i - 1]
		if s[i] == 'a':
			a[i] += 1
		else:
			b[i] += 1
	ct = 0
	for i in range(n):
		if s[i] == 'b':
			tct = a[i]
			for j in range(i, n):
				if s[j] == 'b':
					tct = max(tct, a[i] + (1 + b[j] - b[i]) + (act - a[j]))
			ct = max(ct, tct)
	print(ct)


s = input()
a = [0]
b = [0]
n = len(s)
for i in range(n):
	if s[i] == 'a':
		a.append(a[-1] + 1)
		b.append(b[-1])
	else:
		a.append(a[-1])
		b.append(b[-1] + 1)
a.append(a[-1])
b.append(b[-1])
maxm = 0
for i in range(1, n + 1):
	for j in range(i, n + 1):
		l = a[i] + a[n] - a[j] + b[j + 1] - b[i - 1]
		maxm = max(l, maxm)
print(maxm)


S = input()
N = len(S)
T = [[0] * (N + 1) for _ in range(3)]
for i in range(1, N + 1):
	T[0][i] = T[0][i - 1] + int(S[i - 1] == 'a')
	T[1][i] = max(T[1][i - 1] + int(S[i - 1] == 'b'), T[0][i])
	T[2][i] = max(T[2][i - 1] + int(S[i - 1] == 'a'), T[1][i], T[0][i])
print(T[2][N])


s = input()
startpos = 0
max1 = 0
temp = 0
beststart = []
for i in range(len(s)):
	if s[i] == 'a':
		temp += 1
		if temp > max1:
			max1 = temp
			startpos = i + 1
	else:
		temp -= 1
	beststart.append((max1, startpos))
beststart.append((0, -1))
max2 = 0
temp = 0
endpos = len(s)
for i in range(len(s) - 1, -1, -1):
	if s[i] == 'a':
		temp += 1 + (beststart[i - 1][0] - beststart[i][0])
		if max2 < temp:
			max2 = temp
			endpos = i
	else:
		temp -= 1
ans = 0
if endpos != len(s):
	startpos = beststart[endpos][1]
if startpos == -1:
	startpos = 0
for i in range(startpos):
	if s[i] == 'b':
		ans += 1
for i in range(startpos, endpos):
	if s[i] == 'a':
		ans += 1
for i in range(endpos, len(s)):
	if s[i] == 'b':
		ans += 1
print(len(s) - ans)


s = input()
dp = [[0, 0, 0] for i in range(len(s))]
for i in range(len(s)):
	if s[i] == 'a':
		dp[i][0] = dp[i - 1][0] + 1
		dp[i][1] = dp[i - 1][1]
		dp[i][2] = max(dp[i - 1][1] + 1, dp[i - 1][2] + 1)
	else:
		dp[i][0] = dp[i - 1][0]
		dp[i][1] = max(dp[i - 1][0] + 1, dp[i - 1][1] + 1)
		dp[i][2] = dp[i - 1][2]
e = len(s) - 1
print(max(dp[e][0], dp[e][1], dp[e][2]))


s = input()
a_count = 0
dp1 = 0
dp2 = 0
for i in range(len(s)):
	if s[i] == 'a':
		a_count += 1
		dp2 = max(dp1, dp2) + 1
	else:
		dp1 = max(a_count, dp1) + 1
print(max(dp1, dp2))


s = input()
n = len(s)
arr = ['.']
count_a = [0]
count_b = [0]
count = 1
for i in range(1, n):
	if s[i] == s[i - 1]:
		count += 1
	else:
		if s[i - 1] == 'a':
			count_a.append(count_a[-1] + count)
			count_b.append(count_b[-1])
		else:
			count_a.append(count_a[-1])
			count_b.append(count_b[-1] + count)
		arr.append(s[i - 1])
		count = 1
arr.append(s[-1])
if s[-1] == 'a':
	count_a.append(count_a[-1] + count)
	count_b.append(count_b[-1])
else:
	count_a.append(count_a[-1])
	count_b.append(count_b[-1] + count)
m = len(arr)
res = 0
for i in range(0, m):
	if arr[i] == 'a':
		continue
	for j in range(i, m):
		if arr[j] == 'a':
			continue
		tmp = count_a[i] + (count_a[-1] - count_a[j]) + (count_b[j] - count_b[i - 1])
		res = max(res, tmp)
print(res)


import os
import sys
from io import BytesIO, IOBase
import heapq as h
from bisect import bisect_left, bisect_right
from types import GeneratorType
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			self.os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import collections as col
import math, string

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input()

def listStr():
	return list(input())
MOD = 10 ** 9 + 7

def solve():
	S = getStr()
	N = len(S)
	prefA = [0]
	prefB = [0]
	currA = 0
	currB = 0
	for s in S:
		if s == 'a':
			currA += 1
		else:
			currB += 1
		prefA.append(currA)
		prefB.append(currB)
	ans = 0
	for i in range(N + 1):
		for j in range(i, N + 1):
			ans = max(ans, prefA[i] - prefA[0] + prefB[j] - prefB[i] + prefA[N] - prefA[j])
	return ans
print(solve())


s = input()
n = len(s)
prefix_a = [0]
prefix_b = [0]
if s[0] == 'a':
	prefix_a[0] = 1
else:
	prefix_b[0] = 1
for i in range(1, n):
	if s[i] == 'a':
		prefix_a.append(1 + prefix_a[i - 1])
		prefix_b.append(prefix_b[i - 1])
	else:
		prefix_b.append(1 + prefix_b[i - 1])
		prefix_a.append(prefix_a[i - 1])
if prefix_a[-1] == n or prefix_b[-1] == n:
	print(n)
else:
	ans = -float('inf')
	for i in range(-1, n):
		for j in range(i + 1, n):
			if i == -1:
				if j != n - 1:
					ans = max(ans, prefix_b[j] + prefix_a[n - 1] - prefix_a[j])
				else:
					ans = max(ans, prefix_b[j])
			elif j != n - 1:
				ans = max(ans, prefix_a[i] + prefix_b[j] - prefix_b[i] + prefix_a[n - 1] - prefix_a[j])
			else:
				ans = max(ans, prefix_a[i] + prefix_b[j] - prefix_b[i])
	print(ans)


s = input()
a = b = c = 0
for i in range(len(s)):
	if s[i] == 'a':
		a += 1
		c += 1
	else:
		b += 1
	a = max(a, b)
	b = max(b, c)
print(a)


str = input()
l = len(str)
ok = 0
test1 = 0
test2 = 0
for i in range(l):
	if str[i] == 'a':
		ok = ok + 1
		test2 = max(test1 + 1, test2 + 1)
	else:
		test1 = max(test1 + 1, ok + 1)
print(max(test1, test2))


x = list(input())
a = 0
b = 0
a2 = 0
for i in range(len(x)):
	if x[i] == 'a':
		a += 1
		a2 += 1
	if x[i] == 'b':
		b += 1
	a2 = max(a2, b)
	b = max(b, a)
print(a2)


s = input()
ans = len(s)
a = s.count('a')
b = s.count('b')
if not b:
	print(ans)
else:
	s = s[s.find('b'):s.rfind('b') + 1]
	ans -= len(s)
	a -= ans
	ac = bc = 0
	right = [0] * (len(s) + 1)
	left = right[:]
	left[0] = a - b
	for i in range(1, len(s) + 1):
		if s[i - 1] == 'a':
			ac += 1
		else:
			bc += 1
		right[i] = ac - bc
		left[i] = a - ac - b + bc
	add = 0
	n = len(s) + 1
	for l in range(n):
		z = 0
		for r in range(l, n):
			z = max(z, left[r])
		add = max(add, b + right[l] + z)
	print(ans + add)


from sys import stdin, stdout
s = stdin.readline().rstrip()
n = len(s)
aCount = 0
bCount = 0
aList = [0]
bList = [0]
for i in range(n):
	if s[i] == 'a':
		aCount += 1
		aList.append(aCount)
		bList.append(bCount)
	else:
		bCount += 1
		aList.append(aCount)
		bList.append(bCount)
bestScore = 0
for i in range(n + 1):
	for j in range(i, n + 1):
		score = 0
		score += aList[i]
		score += bList[j] - bList[i]
		score += aCount - aList[j]
		if score > bestScore:
			bestScore = score
print(bestScore)


s = input()
n = len(s)
dp = [[0, 0, 0] for i in range(n)]
if s[n - 1] == 'a':
	dp[n - 1][0] = 1
	dp[n - 1][1] = 0
	dp[n - 1][2] = 1
if s[n - 1] == 'b':
	dp[n - 1][0] = 0
	dp[n - 1][1] = 1
	dp[n - 1][2] = 0
for i in range(n - 2, -1, -1):
	if s[i] == 'a':
		dp[i][0] = max(dp[i + 1][0] + 1, dp[i + 1][1] + 1)
		dp[i][1] = max(dp[i + 1][2], dp[i + 1][1])
		dp[i][2] = dp[i + 1][2] + 1
	else:
		dp[i][0] = max(dp[i + 1][0], dp[i + 1][1])
		dp[i][1] = max(dp[i + 1][2] + 1, dp[i + 1][1] + 1)
		dp[i][2] = dp[i + 1][2]
print(max(dp[0][0], dp[0][1], dp[0][2]))


from sys import stdin, stdout

def INI():
	return int(stdin.readline())

def INL():
	return [int(_) for _ in stdin.readline().split()]

def INS():
	return stdin.readline()

def MOD():
	return pow(10, 9) + 7

def OPS(ans):
	stdout.write(str(ans) + '\n')

def OPL(ans):
	[stdout.write(str(_) + ' ') for _ in ans]
	stdout.write('\n')
S = input()
n = len(S)
ans = 0
D = [0, 0, 0]
for _ in range(n):
	if S[_] == 'a':
		D[2] = max(D) + 1
		D[0] += 1
	else:
		D[1] = max(D[0], D[1]) + 1
OPS(max(D))


max1 = 0
string = input()
first = 0
second = 0
third = 0
i = 0
length = len(string)
while i < length:
	if string[i] == 'a':
		if second > third:
			third = second + 1
		else:
			third += 1
		first += 1
	if string[i] == 'b':
		if first > second:
			second = first + 1
		else:
			second += 1
	i += 1
print(max(third, first, second))


x = str(input())
(dp, ans, len) = ([0] * 5002, 0, len(x))
for i in range(len):
	if x[i] == 'a':
		if i == 0:
			dp[i] = 1
		else:
			dp[i] = dp[i - 1] + 1
	elif i > 0:
		dp[i] = dp[i - 1]
for i in range(len):
	for i2 in range(i, len):
		if dp[i] + dp[len - 1] - dp[i2] + i2 - i - (dp[i2] - dp[i]) > ans:
			ans = dp[i] + dp[len - 1] - dp[i2] + i2 - i - (dp[i2] - dp[i])
for i in range(len):
	if i - dp[i] + dp[len - 1] + 1 - dp[i] > ans:
		ans = i - dp[i] + dp[len - 1] - dp[i] + 1
if len - dp[len - 1] > ans:
	ans = len - dp[len - 1]
print(ans)


import math
import collections
import bisect
import heapq
import time
import random
import itertools
import sys
S = input()
N = len(S)
dp = [[0 for _ in range(5)] for _ in range(N + 1)]
for i in range(1, N + 1):
	a = 1 if S[i - 1] == 'a' else 0
	b = 1 - a
	dp[i][0] = dp[i - 1][0] + a
	dp[i][1] = dp[i - 1][1] + b
	dp[i][2] = max(dp[i - 1][2], dp[i - 1][0]) + b
	dp[i][3] = max(dp[i - 1][1], dp[i - 1][3]) + a
	dp[i][4] = max(dp[i - 1][4], dp[i - 1][2]) + a
print(max(dp[N]))


(f, s, t) = (0, 0, 0)
n = input()
for i in n:
	if i == 'a':
		t = max(t, f, s) + 1
		f += 1
	else:
		s = max(f, s) + 1
print(max(f, s, t))


from itertools import accumulate
from sys import stdout
R = lambda : map(int, input().split())
s = input()
n = len(s)
(ac, bc) = (list(accumulate((int(c == 'a') for c in s))) + [0] * 5, list(accumulate((int(c == 'b') for c in s))) + [0] * 5)
res = 0
for i in range(n + 1):
	for j in range(i, n + 1):
		r1 = bc[i - 1]
		r2 = ac[j - 1] - ac[i - 1]
		r3 = bc[n - 1] - bc[j - 1]
		res = max(res, n - r1 - r2 - r3)
print(res)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 15
mod = 10 ** 9 + 7

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def main():
	s = [1 if c == 'a' else 0 for c in S()]
	l = len(s)
	a = [0] * l
	b = [0] * l
	if s[0] == 1:
		a[0] = 1
	else:
		b[0] = 1
	for i in range(1, l):
		if s[i] == 1:
			a[i] = a[i - 1] + 1
			b[i] = b[i - 1]
		else:
			a[i] = a[i - 1]
			b[i] = b[i - 1] + 1
	if b[-1] == 0:
		return l
	r = 0
	for i in range(0, l):
		if s[i] == 1:
			continue
		for j in range(i, l):
			if s[j] == 1:
				continue
			tr = a[i] + b[j] - b[i] + 1 + a[-1] - a[j]
			if r < tr:
				r = tr
	return r
print(main())


string = input()
a_pref = []
current_a = 0
for ch in string:
	if ch == 'a':
		current_a += 1
	a_pref += [current_a]

def fa(s):
	if len(s) == 0:
		return 0
	if len(s) == 1:
		return 1 if s == 'a' else 0
	if s[-1] == 'a':
		return fa(s[:-1]) + 1
	else:
		return fa(s[:-1])

def fb(s):
	if len(s) == 0:
		return 0
	if len(s) == 1:
		return 1 if s == 'b' else 0
	sl_a = 1 if s[-1] == 'a' else 0
	sl_b = 1 if s[-1] == 'b' else 0
	return max(fa(s[:-1]) + sl_a, fb(s[:-1]) + sl_b)
aas = []
bbs = []
for i in range(len(string)):
	sl_a = 1 if string[i] == 'a' else 0
	sl_b = 1 if string[i] == 'b' else 0
	a_pred = 0 if len(aas) == 0 else aas[-1]
	b_pred = 0 if len(bbs) == 0 else bbs[-1]
	aas += [a_pred + sl_a]
	bbs += [max(a_pred + sl_a, b_pred + sl_b)]
answer = 0
a_count = string.count('a')
for i in range(len(string)):
	current_answer = bbs[i] + a_count - a_pref[i]
	if current_answer > answer:
		answer = current_answer
print(answer)


import sys
import os
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
mod = 10 ** 9 + 7

def lcm(x, y):
	return x * y / gcd(x, y)

def fact(x, mod=mod):
	ans = 1
	for i in range(1, x + 1):
		ans = ans * i % mod
	return ans

def arr2D(n, m, default=0):
	return [[default for j in range(m)] for i in range(n)]

def arr3D(n, m, r, default=0):
	return [[[default for k in range(r)] for j in range(m)] for i in range(n)]

def sortDictV(x):
	return {k: v for (k, v) in sorted(x.items(), key=lambda item: item[1])}

class DSU:

	def __init__(self, length):
		self.length = length
		self.parent = [-1] * self.length

	def getParent(self, node, start):
		if node >= self.length:
			return False
		if self.parent[node] < 0:
			if start != node:
				self.parent[start] = node
			return node
		return self.getParent(self.parent[node], start)

	def union(self, node1, node2):
		parent1 = self.getParent(node1, node1)
		parent2 = self.getParent(node2, node2)
		if parent1 == parent2:
			return False
		elif self.parent[parent1] <= self.parent[parent2]:
			self.parent[parent1] += self.parent[parent2]
			self.parent[parent2] = parent1
		else:
			self.parent[parent2] += self.parent[parent1]
			self.parent[parent1] = parent2
		return True

	def getCount(self, node):
		return -self.parent[self.getParent(node, node)]

def exact(num):
	if abs(num - round(num)) <= 10 ** (-9):
		return round(num)
	return num

def solve(lst):
	a = 0
	b = 0
	bestPref = -1
	maxx = 0
	n = len(lst)
	for i in range(n):
		if lst[i] == 'a':
			a += 1
		else:
			b += 1
		if a - b > maxx:
			maxx = a - b
			bestPref = i
	lst = lst[::-1]
	a = 0
	b = 0
	bestSuf = -1
	maxx = 0
	for i in range(n):
		if lst[i] == 'a':
			a += 1
		else:
			b += 1
		if a - b > maxx:
			maxx = a - b
			bestSuf = i
	bestSuf = n - bestSuf - 1
	lst = lst[::-1]
	if bestSuf <= bestPref:
		maxxDiff = 0
		ind = (0, -1)
		for i in range(n):
			aa = 0
			bb = 0
			for j in range(i, n):
				if lst[j] == 'a':
					aa += 1
				else:
					bb += 1
				if bb - aa > maxxDiff:
					maxxDiff = bb - aa
					ind = (i, j)
		aa = 0
		bb = 0
		for i in range(ind[0], ind[1] + 1):
			if lst[i] == 'a':
				aa += 1
			else:
				bb += 1
		cnta = 0
		for i in lst:
			if i == 'a':
				cnta += 1
		return cnta - aa + bb
	elif bestPref == -1 and bestSuf == n:
		cntb = 0
		for i in lst:
			if i == 'b':
				cntb += 1
		return cntb
	else:
		cnta = 0
		for i in range(0, bestPref + 1):
			if lst[i] == 'a':
				cnta += 1
		for i in range(bestSuf, n):
			if lst[i] == 'a':
				cnta += 1
		for i in range(bestPref + 1, bestSuf):
			if lst[i] == 'b':
				cnta += 1
		return cnta
lst = list(input())
print(solve(lst))


def nikita(s):
	(a, b, result) = (0, 0, 0)
	for elem in s:
		if elem == 'a':
			a += 1
			result += 1
		else:
			b = max(a, b) + 1
			result = max(result, b)
	return result
print(nikita(input()))


s = input()
n = len(s)
counta = [0] * (n + 1)
countb = [0] * (n + 1)
ans = 0
curra = 0
currb = 0
for i in range(n):
	if s[i] == 'a':
		curra += 1
	else:
		currb += 1
	counta[i + 1] = curra
	countb[i + 1] = currb
for i in range(n + 1):
	for j in range(i, n + 1):
		ans = max(ans, countb[j] - countb[i] + counta[i] + counta[n] - counta[j])
print(ans)


def solve(dp, i, j):
	n = len(dp)
	if i >= 0:
		a = dp[i][0]
	else:
		a = 0
	b = dp[j][1]
	if i >= 0:
		b -= dp[i][1]
	a += dp[-1][0]
	a -= dp[j][0]
	return a + b

def main():
	s = input()
	dp = []
	for i in s:
		if not dp:
			curr = [0, 0]
		else:
			curr = dp[-1][:]
		if i == 'a':
			curr[0] += 1
		else:
			curr[1] += 1
		dp.append(curr)
	ans = 0
	n = len(s)
	for i in range(-1, n):
		for j in range(i, n):
			ans = max(ans, solve(dp, i, j))
	print(ans)
main()


data = list(input())
(a, ab, aba) = (0, 0, 0)
for i in data:
	if i == 'a':
		a += 1
		aba = max(ab, aba) + 1
	else:
		ab = max(a, ab) + 1
print(max(a, ab, aba))


s = input()
n = len(s)
f = [[0 for i in range(0, 3)] for j in range(0, n)]
for i in range(0, n):
	if i > 0:
		f[i][0] = f[i - 1][0]
		f[i][1] = f[i - 1][1]
		f[i][2] = f[i - 1][2]
	if s[i] == 'a':
		f[i][0] += 1
		f[i][2] += 1
		f[i][2] = max(f[i][2], f[i][1] + 1)
	else:
		f[i][1] += 1
		f[i][1] = max(f[i][1], f[i][0] + 1)
print(max(f[n - 1][0], max(f[n - 1][1], f[n - 1][2])))


s = input()
p = [0] * 3
for i in range(len(s)):
	if s[i] == 'a':
		p[0] += 1
		p[2] = max(p[1], p[2]) + 1
	else:
		p[1] = max(p[0], p[1]) + 1
print(max(p))


s = input()
a = 0
ab = 0
aba = 0

def max(a, b):
	if a > b:
		return a
	return b
for i in range(len(s)):
	c = s[i]
	if c == 'a':
		a += 1
		aba = max(ab, aba) + 1
	else:
		ab = max(a, ab) + 1
print(max(ab, max(a, aba)))


strs = input()
totala = 0
totalb = 0
prev = ''
prefixa = []
prefixb = []
startingisa = 1
for (j, i) in enumerate(strs):
	if j == 0 and i == 'a':
		startingisa = 0
	if prev != i:
		prefixa.append(totala)
		prefixb.append(totalb)
		prev = i
	if i == 'a':
		totala += 1
	else:
		totalb += 1
prefixa.append(totala)
prefixb.append(totalb)
prefixa.pop(0)
prefixb.pop(0)
revstrs = strs[::-1]
suffixa = []
suffixb = []
totala = 0
totalb = 0
prev = ''
for (j, i) in enumerate(revstrs):
	if prev != i:
		suffixa.append(totala)
		suffixb.append(totalb)
		prev = i
	if i == 'a':
		totala += 1
	else:
		totalb += 1
suffixa.append(totala)
suffixb.append(totalb)
suffixb.pop(0)
suffixa.pop(0)
suffixa = suffixa[::-1]
suffixb = suffixb[::-1]
curmaxlen = max(prefixa[-1], prefixb[-1])
getlen = n = len(suffixa)
if curmaxlen == 4013:
	print(curmaxlen)
for i in range(1, getlen):
	curtotal = prefixa[i - 1] + suffixb[i]
	if curtotal > curmaxlen:
		curmaxlen = curtotal
	curtotal = prefixb[i - 1] + suffixa[i]
	if curtotal > curmaxlen:
		curmaxlen = curtotal
tt = 0
for i in range(startingisa, n - 2, 2):
	for j in range(i + 2, n, 2):
		curtotal = prefixa[i] + suffixa[j]
		curb = prefixb[j] - prefixb[i]
		curtotal += curb
		if curtotal > curmaxlen:
			curmaxlen = curtotal
	tt += 1
print(curmaxlen)


s = input()
ans = 0
n = len(s)
a = [0] * (n + 1)
b = [0] * (n + 1)
for i in range(n):
	if s[i] == 'a':
		a[i + 1] = a[i] + 1
		b[i + 1] = b[i]
	elif s[i] == 'b':
		a[i + 1] = a[i]
		b[i + 1] = b[i] + 1
for j in range(0, n + 1):
	for i in range(0, j + 1):
		ans = max(ans, a[n] - a[j] + b[j] - b[i] + a[i])
print(ans)


s = input()
n = len(s)
asum = [0 for i in range(n + 1)]
for i in range(n):
	asum[i + 1] = asum[i] + 1 if s[i] == 'a' else asum[i]
rmin = 2 ** 31
for i in range(n + 1):
	for j in range(i, n + 1):
		c1 = asum[i]
		c2 = asum[j] - asum[i]
		c3 = asum[n] - asum[j]
		r = i - c1
		r += c2
		r += n - j - c3
		rmin = min(rmin, r)
res = n - rmin
print(res)


from sys import maxsize, stdout, stdin, stderr
mod = int(1000000000.0 + 7)
import sys

def I():
	return int(stdin.readline())

def lint():
	return [int(x) for x in stdin.readline().split()]

def S():
	return input().strip()

def grid(r, c):
	return [lint() for i in range(r)]
from collections import defaultdict, Counter, deque
import math
import heapq
from heapq import heappop, heappush
import bisect
from itertools import groupby

def gcd(a, b):
	while b:
		a %= b
		tmp = a
		a = b
		b = tmp
	return a

def lcm(a, b):
	return a // gcd(a, b) * b

def check_prime(n):
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True

def Bs(a, x):
	i = 0
	j = 0
	left = 1
	right = x
	flag = False
	while left < right:
		mi = (left + right) // 2
		if a[mi] <= x:
			left = mi + 1
			i += 1
		else:
			right = mi
			j += 1
	if left > 0 and a[left - 1] == x:
		return (i - 1, j)
	else:
		return (-1, -1)

def nCr(n, r):
	return fact(n) // (fact(r) * fact(n - r))

def fact(n):
	res = 1
	for i in range(2, n + 1):
		res = res * i
	return res

def primefactors(n):
	num = 0
	while n % 2 == 0:
		num += 1
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			num += 1
			n = n // i
	if n > 2:
		num += 1
	return num

def ask(a):
	print('? {}'.format(a), flush=True)
	n = I()
	return n

def dfs(i, p):
	(a, tmp) = (0, 0)
	for j in d[i]:
		if j != p:
			a += 1
			tmp += dfs(j, i)
	if a == 0:
		return 0
	return tmp / a + 1

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n // 2
	if n > 2:
		l.append(n)
	return l
s = S()
ls = 0
rs = 0
n = len(s)
(a, b) = (0, 0)
(al, bl) = ([], [])
for i in range(n):
	if s[i] == 'a':
		a += 1
	else:
		b += 1
	al.append(a)
	bl.append(b)
al = [0] + al + [al[-1]]
bl = [0] + bl + [bl[-1]]
ans = 0
dp = [[0] * (n + 2) for _ in range(n + 2)]
for i in range(n + 2):
	for j in range(i, n + 2):
		dp[i][j] = al[-1] - al[j] + bl[j] - bl[i] + al[i]
		ans = max(ans, dp[i][j])
print(ans)


s = input()
f = [[0] * len(s) for _ in range(3)]
for i in range(len(s)):
	c = s[i]
	if i == 0:
		f[0][i] += c != 'a'
	else:
		f[0][i] = f[0][i - 1] + (c != 'a')
		f[1][i] = f[0][i - 1] + (c != 'b')
	if i > 1:
		f[1][i] = min(f[1][i - 1] + (c != 'b'), f[1][i])
		f[2][i] = f[1][i - 1] + (c != 'a')
	if i > 2:
		f[2][i] = min(f[2][i - 1] + (c != 'a'), f[2][i])
ans = f[2][-1]
ans = min(ans, s.count('a'), s.count('b'))
k = [0]
for c in s:
	k.append(k[-1] + (c == 'a') - (c == 'b'))
ans = min(ans, s.count('a') - max(k), s.count('b') + min(k))
print(len(s) - ans)


s = input()
n = len(s)
(a, b, aba) = (0, 0, 0)
for i in range(n):
	if s[i] == 'a':
		aba = max(a, b, aba) + 1
		a += 1
	else:
		b = max(a, b) + 1
print(max(a, b, aba))


import sys, threading
sys.setrecursionlimit(10 ** 6)
scan = lambda : map(int, input().split())
string = input()
(a, ab, aba) = (0, 0, 0)
size = 0
for i in string:
	size += 1
	if i == 'a':
		a += 1
		aba = max(aba + 1, a, ab + 1)
	else:
		ab = max(ab + 1, a + 1)
print(max(a, ab, aba))


s = ' ' + input()
dp = [[0, 0, 0] for i in range(len(s) + 5)]
i = 0
while i < len(s) - 1:
	i += 1
	if s[i] == 'a':
		dp[i][0] = dp[i - 1][0] + 1
		dp[i][1] = dp[i - 1][1]
		dp[i][2] = max(dp[i - 1][0], dp[i - 1][1], dp[i - 1][2]) + 1
	if s[i] == 'b':
		dp[i][0] = dp[i - 1][0]
		dp[i][1] = max(dp[i - 1][0], dp[i - 1][1]) + 1
		dp[i][2] = max(dp[i - 1][0], dp[i - 1][2])
print(max(dp[i][0], dp[i][1], dp[i][2]))


s = input()
(a, ab, aba) = (0, 0, 0)
for i in s:
	if i == 'a':
		a += 1
		aba = max(ab, aba) + 1
	else:
		ab = max(a, ab) + 1
print(max(aba, ab, a))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import ceil

def prod(a, mod=10 ** 9 + 7):
	ans = 1
	for each in a:
		ans = ans * each % mod
	return ans

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def lcm(a, b):
	return a * b // gcd(a, b)

def binary(x, length=16):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y
for _ in range(int(input()) if not True else 1):
	s = input()
	n = len(s)
	count = [[0] * (n + 1), [0] * (n + 1)]
	for i in range(n):
		if s[i] == 'a':
			count[0][i + 1] += 1
		elif s[i] == 'b':
			count[1][i + 1] += 1
		count[0][i + 1] += count[0][i]
		count[1][i + 1] += count[1][i]
	ans = 0
	for i in range(n + 1):
		c1 = count[0][i]
		for j in range(i, n):
			c2 = count[1][j + 1] - count[1][i]
			c3 = 0 if j == n - 1 else count[0][-1] - count[0][j + 1]
			ans = max(ans, c1 + c2 + c3)
		ans = max(ans, c1)
	print(ans)


s = input()
n = len(s)
dp = [[10 ** 9 for j in range(4)] for i in range(n + 1)]
dp[0][0] = 0
for i in range(n + 1):
	for j in range(3):
		cnt = 0
		for k in range(i, n + 1):
			dp[k][j + 1] = min(dp[k][j + 1], dp[i][j] + cnt)
			if k < n and (j == 1 and s[k] == 'a' or (j != 1 and s[k] == 'b')):
				cnt += 1
print(n - dp[n][3])


import os
import sys
from io import BytesIO, IOBase
import math
import itertools
import bisect
import heapq

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def binary(n):
	return bin(n).replace('0b', '')

def decimal(s):
	return int(s, 2)

def pow2(n):
	p = 0
	while n > 1:
		n //= 2
		p += 1
	return p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(i)
			n = n / i
	if n > 2:
		l.append(int(n))
	return l

def isPrime(n):
	if n == 1:
		return False
	else:
		root = int(n ** 0.5)
		root += 1
		for i in range(2, root):
			if n % i == 0:
				return False
		return True

def maxPrimeFactors(n):
	maxPrime = -1
	while n % 2 == 0:
		maxPrime = 2
		n >>= 1
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			maxPrime = i
			n = n / i
	if n > 2:
		maxPrime = n
	return int(maxPrime)

def countcon(s, i):
	c = 0
	ch = s[i]
	for i in range(i, len(s)):
		if s[i] == ch:
			c += 1
		else:
			break
	return c

def lis(arr):
	n = len(arr)
	lis = [1] * n
	for i in range(1, n):
		for j in range(0, i):
			if arr[i] > arr[j] and lis[i] < lis[j] + 1:
				lis[i] = lis[j] + 1
	maximum = 0
	for i in range(n):
		maximum = max(maximum, lis[i])
	return maximum

def isSubSequence(str1, str2):
	m = len(str1)
	n = len(str2)
	j = 0
	i = 0
	while j < m and i < n:
		if str1[j] == str2[i]:
			j = j + 1
		i = i + 1
	return j == m

def maxfac(n):
	root = int(n ** 0.5)
	for i in range(2, root + 1):
		if n % i == 0:
			return n // i
	return n

def p2(n):
	c = 0
	while n % 2 == 0:
		n //= 2
		c += 1
	return c

def seive(n):
	primes = [True] * (n + 1)
	primes[1] = primes[0] = False
	for i in range(2, n + 1):
		if primes[i]:
			for j in range(i + i, n + 1, i):
				primes[j] = False
	p = []
	for i in range(0, n + 1):
		if primes[i]:
			p.append(i)
	return p

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def denofactinverse(n, m):
	fac = 1
	for i in range(1, n + 1):
		fac = fac * i % m
	return pow(fac, m - 2, m)

def numofact(n, m):
	fac = 1
	for i in range(1, n + 1):
		fac = fac * i % m
	return fac

def sod(n):
	s = 0
	while n > 0:
		s += n % 10
		n //= 10
	return s
s = input()
n = len(s)
prea = [0]
preb = [0]
for i in range(0, n):
	if s[i] == 'a':
		prea.append(prea[-1] + 1)
		preb.append(preb[-1])
	else:
		prea.append(prea[-1])
		preb.append(preb[-1] + 1)
ans = 10 ** 10
for i in range(0, n + 1):
	for j in range(i, n + 1):
		ac1 = prea[i]
		bc1 = preb[j] - preb[i]
		ac2 = prea[-1] - prea[j]
		ans = min(ans, n - ac1 - ac2 - bc1)
print(n - ans)


s = input()
i = 0
a = [0, 0, 0]
for i in s:
	if i == 'a':
		a[2] = max(a) + 1
		a[0] += 1
	else:
		a[1] = max(a[0], a[1]) + 1
print(max(a))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
s = input()
n = len(s)
l1 = []
l2 = []
c = 0
d = 0
for i in range(n):
	if s[i] == 'a':
		c += 1
	else:
		d += 1
	l1.append(c)
	l2.append(d)
ans = max(c, d)
for i in range(n):
	for j in range(i + 1, n):
		ans = max(ans, l1[i] + l2[j] - l2[i] + l1[-1] - l1[j], l1[i] + l2[-1] - l2[i], l2[i] + l1[-1] - l1[i])
print(ans)


import sys, os, io
from sys import stdin
from math import log, gcd, ceil
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import math
alphabets = list('abcdefghijklmnopqrstuvwxyz')

def isPrime(x):
	for i in range(2, x):
		if i * i > x:
			break
		if x % i == 0:
			return False
	return True

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(int(i))
			n = n / i
	if n > 2:
		l.append(n)
	return list(set(l))

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def si():
	return input()

def prefix_sum(arr):
	r = [0] * (len(arr) + 1)
	for (i, el) in enumerate(arr):
		r[i + 1] = r[i] + el
	return r

def divideCeil(n, x):
	if n % x == 0:
		return n // x
	return n // x + 1

def ii():
	return int(input())

def li():
	return list(map(int, input().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')

def power_set(L):
	cardinality = len(L)
	n = 2 ** cardinality
	powerset = []
	for i in range(n):
		a = bin(i)[2:]
		subset = []
		for j in range(len(a)):
			if a[-j - 1] == '1':
				subset.append(L[j])
		powerset.append(subset)
	powerset_orderred = []
	for k in range(cardinality + 1):
		for w in powerset:
			if len(w) == k:
				powerset_orderred.append(w)
	return powerset_orderred

def fastPlrintNextLines(a):
	print('\n'.join(map(str, a)))
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
t = 1
for _ in range(t):
	s = list(si())
	A = s.count('a')
	B = s.count('b')
	a = 0
	b = 0
	a2 = 0
	b2 = 0
	ans = 1000000000000000
	for i in range(len(s)):
		a2 = 0
		b2 = 0
		for j in range(len(s) - 1, i - 1, -1):
			ans = min(b + b2 + A - a - a2, ans)
			if s[j] == 'a':
				a2 += 1
			else:
				b2 += 1
		if s[i] == 'a':
			a += 1
		else:
			b += 1
	a = 0
	b = 0
	for i in range(len(s)):
		ans = min(ans, a + B - b)
		if s[i] == 'a':
			a += 1
		else:
			b += 1
	a = 0
	b = 0
	for i in range(len(s)):
		ans = min(ans, b + A - a)
		if s[i] == 'a':
			a += 1
		else:
			b += 1
	print(len(s) - ans)


from math import inf
s = input()
n = len(s)
x = 'aba'
d = [[0, 0, 0] for _ in range(n)]
d[0][0] = int(s[0] == 'a')
d[0][1] = int(s[0] == 'b')
d[0][2] = int(s[0] == 'a')
for i in range(1, n):
	for j in range(3):
		if s[i] == x[j]:
			if j != 0:
				d[i][j] = max(d[i - 1][j - 1], d[i - 1][j]) + 1
			else:
				d[i][j] = d[i - 1][j] + 1
		else:
			d[i][j] = d[i - 1][j]
print(max(d[n - 1]))


from collections import Counter, OrderedDict
from itertools import permutations as perm
from fractions import Fraction
from collections import deque
from sys import stdin
from bisect import *
from heapq import *
from math import *
g = lambda : stdin.readline().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gfl = lambda : [float(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
mod = int(1000000000.0) + 7
inf = float('inf')
s = '*' + g()
n = len(s) - 1
(a, b) = ([0] * (n + 2), [0] * (n + 2))
(a_, b_) = ([0] * (n + 2), [0] * (n + 2))
for i in range(1, n + 1):
	(a if s[i] == 'a' else b)[i] += 1
	a[i] += a[i - 1]
	b[i] += b[i - 1]
for i in reversed(range(1, n + 1)):
	(a_ if s[i] == 'a' else b_)[i] += 1
	a_[i] += a_[i + 1]
	b_[i] += b_[i + 1]
ans = max(a[-1], b[-1])
for i in range(1, n + 1):
	ans = max(ans, a[i] + b_[i + 1], b[i] + a_[i + 1])
for l in range(2, n - 1):
	for r in range(l, n):
		ans_ = a_[r + 1] + a[l - 1] + b[r] - b[l - 1]
		ans = max(ans, ans_)
print(ans)


s = input()
n = len(s)
a = [0] * n
b = [0] * n
c = [0] * n
for i in range(n):
	a[i] = bool(s[i] == 'a')
	if i > 0:
		a[i] += a[i - 1]
for i in range(n):
	x = 0
	if i > 0:
		x = b[i - 1]
	b[i] = max(a[i], x + bool(s[i] == 'b'))
for i in range(n):
	x = 0
	if i > 0:
		x = c[i - 1]
	c[i] = max(b[i], x + bool(s[i] == 'a'))
print(int(c[n - 1]))


read = lambda : map(int, input().split())
N = 50005
a = [0 for i in range(N)]
b = [0 for i in range(N)]
c = input()
l = len(c)
for i in range(l):
	a[i + 1] = a[i]
	b[i + 1] = b[i]
	if c[i] == 'a':
		a[i + 1] += 1
	else:
		b[i + 1] += 1
ans = 0
for i in range(l + 1):
	for j in range(i, l + 1):
		ans = max(ans, a[i] + b[j] - b[i] + a[l] - a[j])
print(ans)


def solve(string):
	dp = [[], [], [], 0]
	index = 0
	for i in range(0, len(string)):
		if string[i] == 'a':
			dp[0].append('a')
			dp[2].append('a')
			if dp[3] == 1:
				dp[3] = 2
			if len(dp[1]) >= 1 and dp[1][-1] != 'b':
				dp[1].append('a')
		else:
			dp[1].append('b')
			if dp[3] != 2:
				dp[2].append('b')
				dp[3] = 1
		if len(dp[0]) > len(dp[1]):
			dp[1] = list(dp[0])
		if len(dp[0]) > len(dp[2]):
			dp[2] = list(dp[0])
			dp[3] = 0
		if len(dp[1]) > len(dp[2]):
			dp[2] = list(dp[1])
			dp[3] = 1
	print(len(dp[2]))
string = input().strip()
solve(string)


str = input()
l = len(str)
cnt = 0
dp1 = 0
dp2 = 0
for i in range(l):
	if str[i] == 'a':
		cnt += 1
		dp2 = max(dp1 + 1, dp2 + 1)
	else:
		dp1 = max(dp1 + 1, cnt + 1)
print(max(dp1, dp2))


s = input()
n = len(s)
if 'b' not in s or 'a' not in s:
	print(n)
	exit()
a = [1 if s[i] == 'a' else 0 for i in range(n)]
b = [1 if s[i] == 'b' else 0 for i in range(n)]
for i in range(1, n):
	a[i] += a[i - 1]
	b[i] += b[i - 1]
ans = 0
for i in range(n):
	for j in range(i, n):
		cur = a[n - 1] - a[j] + b[j]
		if i > 0:
			cur -= b[i - 1] - a[i - 1]
		ans = max(ans, cur)
print(ans)


s = str(input())
a = [ord(i) - ord('a') for i in s]
n = len(a)
if n == 1 or n == 2:
	print(n)
	exit()
c = [0 for i in range(n + 1)]
for i in range(n):
	c[i] = c[i - 1] + a[i]
ans = 0
for i in range(n):
	for j in range(i, n):
		an = n - (c[i - 1] + c[n - 1] - c[j] + (j - i + 1) - c[j] + c[i - 1])
		ans = max(ans, an)
ans = max(ans, n - c[n - 1])
print(ans)


s = input()
n = len(s)
a = [0]
b = [0]
for x in s:
	if x == 'a':
		a.append(a[-1] + 1)
		b.append(b[-1])
	else:
		a.append(a[-1])
		b.append(b[-1] + 1)
ans = 0
a.append(a[-1])
b.append(b[-1])
for i in range(1, n + 1):
	for j in range(i, n + 1):
		t = a[i] + b[j + 1] - b[i - 1] + a[-1] - a[j]
		ans = max(ans, t)
print(ans)


s = input()
size = len(s)
a = [0] * (size + 1)
b = [0] * (size + 1)
for i in range(size):
	a[i + 1] = a[i] + (1 if s[i] == 'a' else 0)
	b[i + 1] = b[i] + (1 if s[i] == 'b' else 0)
ans = 0
for i in range(size + 1):
	for j in range(i + 1):
		val = b[i] - b[j] + a[j] + a[size] - a[i]
		ans = max(val, ans)
print(ans)


s = input()
n = len(s)
dp = []
dp.append((0, 0, 0))
for i in range(n):
	if s[i] == 'a':
		dp.append((dp[-1][0] + 1, dp[-1][1], max(dp[-1][1:]) + 1))
	elif s[i] == 'b':
		dp.append((dp[-1][0], max(dp[-1][1], dp[-1][0]) + 1, dp[-1][-1]))
print(max(dp[-1]))


s = str(input())
n = len(s)
if s.count('a') == 0 or s.count('b') == 0:
	print(n)
	exit()
LA = [0] * (n + 1)
LB = [0] * (n + 1)
RA = [0] * (n + 1)
RB = [0] * (n + 1)
for (i, c) in enumerate(s):
	LA[i + 1] = LA[i]
	LB[i + 1] = LB[i]
	if c == 'a':
		LA[i + 1] += 1
	else:
		LB[i + 1] += 1
for i in reversed(range(n)):
	c = s[i]
	RA[i] = RA[i + 1]
	RB[i] = RB[i + 1]
	if c == 'a':
		RA[i] += 1
	else:
		RB[i] += 1
ans = 0
for i in range(n):
	if s[i] == 'a':
		continue
	for j in range(i, n):
		if s[j] == 'a':
			continue
		la = LA[i]
		b = LB[j + 1] - LB[i]
		ra = RA[j]
		temp = la + b + ra
		ans = max(temp, ans)
print(ans)


s = input().strip()
n = len(s)
dp = [[0, 0, 0] for _ in range(n)]
for i in range(n):
	if s[i] == 'a':
		dp[i][0] = 1
		for j in range(i):
			dp[i][0] = max(dp[i][0], dp[j][0] + 1)
		dp[i][1] = dp[i][0]
		dp[i][2] = 1
		for j in range(i):
			dp[i][2] = max(dp[i][2], dp[j][1] + 1, dp[j][2] + 1)
	else:
		dp[i][0] = 0
		dp[i][1] = 1
		for j in range(i):
			dp[i][1] = max(dp[i][1], dp[j][1] + 1)
		dp[i][2] = 1
		for j in range(i):
			dp[i][2] = max(dp[i][2], dp[j][1] + 1)
ans = 0
for i in range(n):
	ans = max(ans, dp[i][2])
print(ans)


import sys
input = sys.stdin.readline
s = list(input().rstrip())
n = len(s)
dp = [[0 for i in range(n + 1)] for j in range(n + 1)]
dp[0][0] = int(s[0] == 'a')
for i in range(1, n):
	if s[i] == 'a':
		dp[0][i] = dp[0][i - 1] + 1
	else:
		dp[0][i] = dp[0][i - 1]
for i in range(n):
	for j in range(i, n):
		dp[i][j] = dp[0][j] - dp[0][i - 1]
maxi = 0
for i in range(n):
	for j in range(i + 1, n):
		maxi = max(maxi, dp[0][i] + (j - i + 1) - dp[i][j] + dp[j][n - 1])
print(maxi if n > 2 else n)


s = input()
(arr, dp) = ([], [[0, 0], 0])
c = 1
for i in range(1, len(s)):
	if s[i] != s[i - 1]:
		arr.append(c)
		c = 0
	c += 1
arr.append(c)
if s[0] == 'b':
	arr = [0] + arr
for i in range(2, len(arr) + 2):
	if i & 1:
		dp.append(max(dp[i - 2], dp[i - 1][0]) + arr[i - 2])
	else:
		dp.append([dp[i - 2][0] + arr[i - 2], max(dp[i - 1], dp[i - 2][1]) + arr[i - 2]])
print(max((dp[i] if i & 1 else max(dp[i]) for i in range(len(dp)))))


strs = input()
array = []
char = []
count = 1
char.append(strs[0])
i = 1
while i < len(strs):
	if strs[i] == strs[i - 1]:
		count += 1
	else:
		array.append(count)
		char.append(strs[i])
		count = 1
	i += 1
array.append(count)
bback = []
aback = []
bfront = []
afront = []
i = 0
while i < len(array):
	if i == 0:
		if char[i] == 'b':
			aback.append(0)
			bback.append(array[0])
		else:
			bback.append(0)
			aback.append(array[0])
	elif char[i] == 'b':
		aback.append(aback[i - 1])
		bback.append(bback[i - 1] + array[i])
	else:
		aback.append(aback[i - 1] + array[i])
		bback.append(bback[i - 1])
	i += 1
i = 0
while i < len(array):
	afront.append(aback[-1] - aback[i])
	bfront.append(bback[-1] - bback[i])
	i += 1
if len(array) == 1:
	print(array[0])
else:
	maxm = -1
	i = 0
	while i < len(array):
		if char[i] == 'b':
			j = i
			while j < len(array):
				if char[j] == 'b':
					if j == i:
						ans = aback[i] + array[i] + afront[i]
						if ans > maxm:
							maxm = ans
					else:
						ans = aback[i] + array[i] + (bback[j] - bback[i]) + afront[j]
						if ans > maxm:
							maxm = ans
				j += 1
		i += 1
	print(maxm)


string = input()
prefix___a_nr = [0]
prefix___b_nr = [0]
for sym in string:
	curr_a_nr = prefix___a_nr[-1]
	curr_b_nr = prefix___b_nr[-1]
	if sym == 'a':
		curr_a_nr += 1
	elif sym == 'b':
		curr_b_nr += 1
	prefix___a_nr.append(curr_a_nr)
	prefix___b_nr.append(curr_b_nr)
ans = 5000
for sep1 in range(len(string) + 1):
	for sep2 in range(sep1, len(string) + 1):
		curr_ans = 0
		curr_ans += prefix___b_nr[sep1]
		curr_ans += prefix___a_nr[sep2] - prefix___a_nr[sep1]
		curr_ans += prefix___b_nr[-1] - prefix___b_nr[sep2]
		ans = min(ans, curr_ans)
print(len(string) - ans)


s = input()
a_count = s.count('a')
b_count = len(s) - a_count
a_counts = [0] * len(s)
b_counts = [0] * len(s)
for i in range(len(s)):
	if s[i] == 'a':
		a_counts[i] = 1
	else:
		b_counts[i] = 1
	if i > 0:
		a_counts[i] += a_counts[i - 1]
		b_counts[i] += b_counts[i - 1]
best = 0
count = 0
for i in range(-1, len(s)):
	for j in range(i + 1, len(s) + 1):
		count = 0
		if i >= 0:
			count += a_counts[i]
		count += (b_count if j == len(s) else b_counts[j]) - (0 if i < 0 else b_counts[i])
		count += 0 if j == len(s) else a_count - a_counts[j]
		if best < count:
			best = count
print(best)


import sys
input = sys.stdin.readline
inp = input().strip()
cur = 0
pre_a = []
for i in range(len(inp)):
	if inp[i] == 'a':
		cur += 1
	pre_a.append(cur)
cur = 0
pre_b = []
for i in range(len(inp)):
	if inp[i] == 'b':
		cur += 1
	pre_b.append(cur)
ans = 0
for i in range(len(inp)):
	for j in range(i, len(inp)):
		re = (pre_b[i - 1] if i > 0 else 0) + pre_b[-1] - pre_b[j]
		if i != j:
			if i == 0:
				re += pre_a[j]
			else:
				re += pre_a[j] - pre_a[i - 1]
		ans = max(ans, len(inp) - re)
print(ans)


s = input()
n = len(s)
if not 'b' in s:
	print(n)
	quit()
preb = [1 if s[0] == 'b' else 0]
prea = [1 if s[0] == 'a' else 0]
posta = [1 if s[-1] == 'a' else 0]
for i in range(1, n):
	if s[i] == 'a':
		prea.append(prea[-1] + 1)
		preb.append(preb[-1])
	else:
		prea.append(prea[-1])
		preb.append(preb[-1] + 1)
for i in range(n - 2, -1, -1):
	if s[i] == 'a':
		posta.append(posta[-1] + 1)
	else:
		posta.append(posta[-1])
posta = posta[::-1]
res = 0
for i in range(n):
	for j in range(i, n):
		res_now = 0
		if i > 0:
			res_now += prea[i - 1]
			res_now += preb[j] - preb[i - 1]
		else:
			res_now += preb[j]
		if j < n - 1:
			res_now += posta[j + 1]
		res = max(res, res_now)
print(res)


s = input()
d = [[0 for i in range(len(s) + 5)] for j in range(3)]
for i in range(1, len(s) + 1):
	d[0][i] = d[0][i - 1] + (s[i - 1] == 'b')
	d[1][i] = d[1][i - 1] + (s[i - 1] == 'a')
	d[1][i] = min(d[1][i], d[0][i - 1] + (s[i - 1] == 'a'))
	d[2][i] = d[2][i - 1] + (s[i - 1] == 'b')
	d[2][i] = min(d[2][i], d[1][i - 1] + (s[i - 1] == 'b'))
n = len(s)
print(len(s) - min(d[0][n], d[1][n], d[2][n]))


a = input()
n = len(a)
prefix_a = [0 for _ in range(0, n + 2)]
prefix_b = [0 for _ in range(0, n + 2)]
for i in range(n):
	prefix_a[i + 1] = prefix_a[i] + (a[i] == 'a')
	prefix_b[i + 1] = prefix_b[i] + (a[i] == 'b')
prefix_b[n + 1] = prefix_b[n]
prefix_a[n + 1] = prefix_a[n]
minimum = 10000000000.0
for i in range(0, n + 1):
	for j in range(i + 1, n + 2):
		minimum = min(minimum, prefix_b[i] + prefix_a[j] - prefix_a[i] + prefix_b[-1] - prefix_b[j])
print(n - minimum)


sl = list(input())
prif_a = [0]
prif_b = [0]
a = 0
b = 0
for x in sl:
	if x == 'a':
		a += 1
	else:
		b += 1
	prif_a.append(a)
	prif_b.append(b)
ans = 0
for i in range(len(prif_a)):
	for j in range(i, len(prif_a)):
		ans = max(ans, prif_a[-1] - prif_a[j] + prif_b[j] - prif_b[i] + prif_a[i])
print(ans)


s = input()
cnt = [0, 0, 0]
for ch in s:
	if ch == 'a':
		cnt[2] = max(cnt) + 1
		cnt[0] += 1
	else:
		cnt[1] = max(cnt[0], cnt[1]) + 1
print(max(cnt))


s = list(input())
n = len(s)
lis = [[0, 0] for i in range(n + 2)]
for i in range(n):
	if s[i] == 'a':
		lis[i + 1][0] = lis[i][0] + 1
		lis[i + 1][1] = lis[i][1]
	else:
		lis[i + 1][0] = lis[i][0]
		lis[i + 1][1] = lis[i][1] + 1
ans = 0
for i in range(1, n + 1):
	for j in range(i, n + 2):
		tmp = 0
		tmp += lis[j - 1][1] - lis[i - 1][1]
		tmp += lis[i - 1][0]
		tmp += lis[n][0] - lis[j - 1][0]
		ans = max(ans, tmp)
print(ans)


s = input()
ch = []
ct = []
i = 0
n = len(s)
while i < n:
	t = s[i]
	ch.append(t)
	c = 0
	while s[i] == t:
		c = c + 1
		i = i + 1
		if i == n:
			break
	ct.append(c)
(a, b) = ([0] * len(ct), [0] * len(ct))
for i in range(len(ct)):
	if ch[i] == 'a':
		a[i] = a[i - 1] + ct[i]
		b[i] = b[i - 1]
	if ch[i] == 'b':
		b[i] = b[i - 1] + ct[i]
		a[i] = a[i - 1]
ans = 0
if len(ct) > 1:
	for i in range(len(ct)):
		if ch[i] == 'b':
			(x, y, z) = (a[i], ct[i], 0)
			for j in range(i + 1, len(ct)):
				if ch[j] == 'b':
					z = z + ct[j]
					ans = max(ans, x + y + z)
				elif ch[j] == 'a':
					ans = max(ans, x + y + z + a[-1] - a[j - 1])
		if ch[i] == 'a':
			(y, z) = (ct[i], 0)
			for j in range(i + 1, len(ct)):
				if ch[j] == 'a':
					z = z + ct[j]
					ans = max(ans, y + z)
				elif ch[j] == 'b':
					ans = max(ans, y + z + b[-1] - b[j - 1])
else:
	ans = ct[0]
print(ans)


import sys
s = input()
n = len(s)
prefix_a = [0] * n
prefix_b = [0] * n
if s[0] == 'a':
	prefix_a[0] = 1
else:
	prefix_b[0] = 1
for i in range(1, n):
	if s[i] == 'a':
		prefix_a[i] = prefix_a[i - 1] + 1
		prefix_b[i] = prefix_b[i - 1]
	else:
		prefix_a[i] = prefix_a[i - 1]
		prefix_b[i] = prefix_b[i - 1] + 1
if prefix_a[-1] == n or prefix_b[-1] == n:
	print(n)
else:
	ans = -1 * sys.maxsize
	for i in range(-1, n):
		for j in range(i + 1, n):
			if i == -1:
				ans = max(ans, prefix_b[j] + prefix_a[n - 1] - prefix_a[j])
			else:
				ans = max(ans, prefix_a[i] + prefix_b[j] - prefix_b[i] + prefix_a[n - 1] - prefix_a[j])
	print(ans)


import os, sys
from collections import defaultdict, Counter, deque
from io import BytesIO, IOBase
from math import gcd, inf

def main():
	a = input().strip()
	n = len(a)
	pa = [0 for i in range(n)]
	pb = [0 for j in range(n)]
	c = 0
	for i in range(n):
		if a[i] == 'a':
			pa[i] = pa[i - 1] + 1
			pb[i] = pb[i - 1]
		else:
			pb[i] = pb[i - 1] + 1
			pa[i] = pa[i - 1]
		if i > 0:
			if a[i] != a[i - 1]:
				c += 1
	if c < 2:
		print(n)
		exit()
	elif c == 2:
		if a[0] == 'a':
			print(n)
			exit()
	pa.append(0)
	pb.append(0)
	m = 0
	for i in range(n):
		for j in range(i, n):
			an = pa[i - 1] + pb[j] - pb[i - 1] + pa[n - 1] - pa[j]
			if an > m:
				m = an
	print(m)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


s = input()
x1 = 0
x2 = 0
x3 = 0
for i in range(len(s)):
	if s[i] == 'a':
		x3 = max(x2, x3) + 1
		x1 += 1
	else:
		x2 = max(x1, x2) + 1
x4 = max(x1, x2)
x5 = max(x3, x4)
print(x5)


s = input()
n = len(s)
prefa = [0] * n
prefb = [0] * n
prefa[0] = s[0] == 'a'
prefb[0] = s[0] == 'b'
c = 0
for i in range(1, n):
	prefa[i] = prefa[i - 1] + (s[i] == 'a')
	prefb[i] = prefb[i - 1] + (s[i] == 'b')
(ans1, ans) = (0, 0)
prefa = [0] + prefa
prefb = [0] + prefb
for i in range(n + 1):
	for j in range(i, n + 1):
		ans1 = prefa[n] - prefa[j] + (prefb[j] - prefb[i]) + prefa[i]
		if ans1 > ans:
			ans = ans1
print(ans)


line = input()
dp = 3 * [0]
for c in line:
	if c == 'a':
		dp[2] = max(dp) + 1
		dp[0] += 1
	elif c == 'b':
		dp[1] = max(dp[:2]) + 1
print(max(dp))


import sys
word = sys.stdin.readline()
word = word.replace('\n', '')
N = len(word)
ABA = [0] * (N + 1)
BA = [0] * (N + 1)
A = [0] * (N + 1)
A[N] = 0
BA[N] = 0
ABA[N] = 0
for i in range(N - 1, -1, -1):
	if word[i] == 'a':
		A[i] = A[i + 1]
		ABA[i] = ABA[i + 1]
		BA[i] = min(1 + BA[i + 1], A[i + 1])
	else:
		A[i] = A[i + 1] + 1
		ABA[i] = min(BA[i + 1], 1 + ABA[i + 1])
		BA[i] = BA[i + 1]
print(N - ABA[0])


s = input()
le = len(s)
mx = 0
a = s.count('a')
b = s.count('b')
mx = max(mx, a)
mx = max(mx, b)
cnta = [0] * (le + 1)
cntb = [0] * (le + 1)
cnta[0] = 1 if s[0] == 'a' else 0
cntb[0] = 1 - cnta[0]
for i in range(1, le):
	cnta[i] = cnta[i - 1]
	cntb[i] = cntb[i - 1]
	if s[i] == 'a':
		cnta[i] += 1
	else:
		cntb[i] += 1
for i in range(le):
	for j in range(i + 1, le):
		mx = max(mx, cnta[i] + b - cntb[i])
		mx = max(mx, cntb[i] + a - cnta[i])
		mx = max(mx, cnta[i] + cntb[j] - cntb[i] + a - cnta[j])
print(mx)


s = input()
(a, b) = ([0], [0])
(a_count, b_count) = (0, 0)
for x in s:
	if x == 'a':
		a_count += 1
	else:
		b_count += 1
	a.append(a_count)
	b.append(b_count)
best = 0
for i in range(len(a)):
	for j in range(i, len(a)):
		best = max(best, a[i] + (a_count - a[j]) + (b[j] - b[i]))
print(best)


s = input()
j = 0
a = 1
b = 0
max1 = 0
if len(s) == 1:
	print(1)
elif len(s) == 2:
	print(2)
else:
	if s[0] == 'a':
		j += 1
	if s[1] == 'a':
		j += 1
	if s[1] == 'b':
		b = 1
	dp = [1, 2]
	for i in range(2, len(s), 1):
		if s[i] == 'a':
			j += 1
			dp.append(max(dp[a] + 1, dp[b] + 1))
			a = i
		else:
			dp.append(max(j + 1, dp[b] + 1))
			b = i
		max1 = max(max1, dp[i])
	print(max1)


s = input()
back = 0
now = 0
after_ab = 0
for i in s:
	if i == 'a':
		back += 1
		after_ab = max(after_ab, now) + 1
	else:
		now = max(back, now) + 1
print(max(back, max(now, after_ab)))


S = input()
N = len(S)
dp = [[0 for i in range(3)] for j in range(N + 1)]
for i in range(1, N + 1):
	if S[i - 1] == 'b':
		dp[i][1] = max(dp[i - 1][0], dp[i - 1][1]) + 1
		dp[i][0] = dp[i - 1][0]
		dp[i][2] = dp[i - 1][2]
	else:
		dp[i][0] = dp[i - 1][0] + 1
		dp[i][2] = max(dp[i - 1][1], dp[i - 1][2]) + 1
		dp[i][1] = dp[i - 1][1]
print(max(dp[-1]))


a = input()
n = len(a)
b = 0
c = 0
d = [0]
e = [0]
for i in range(len(a)):
	if a[i] == 'a':
		b = b + 1
	elif a[i] == 'b':
		c = c + 1
	d.append(b)
	e.append(c)
f = []
for i in range(n + 1):
	for j in range(i, n + 1):
		f.append(d[i] + e[j] - e[i] + d[n] - d[j])
print(max(f))


s = input()
n = len(s)
pa = [0]
pb = [0]
a = 0
b = 0
for i in range(n):
	if s[i] == 'a':
		a += 1
	else:
		b += 1
	pa.append(a)
	pb.append(b)
ans = []
for i in range(n + 1):
	for j in range(i, n + 1):
		ans.append(pa[i] + pb[j] - pb[i] + pa[n] - pa[j])
print(max(max(ans), s.count('a'), s.count('b')))


s = input()
acnt = []
cnt = 0
for i in range(len(s)):
	if s[i] == 'a':
		cnt += 1
	acnt.append(cnt)
suma = acnt[len(acnt) - 1]
ans = suma
for i in range(len(s)):
	bcnt = 0
	ac = 0
	temp = 0
	for j in range(i, len(s)):
		if s[j] == 'b':
			bcnt += 1
		if s[j] == 'a':
			ac += 1
		temp = acnt[i] + bcnt + (suma - (acnt[i] + ac))
		if temp > ans:
			ans = temp
print(ans)


s = input()
dp = [0] * 3
for c in s:
	if c == 'a':
		dp[2] = max(dp) + 1
		dp[0] += 1
	else:
		dp[1] = max(dp[0], dp[1]) + 1
print(max(dp))


a = [0]
f = 'a'
for i in input():
	if f != i:
		a.append(1)
		f = i
	else:
		a[-1] += 1
m1 = []
m2 = []
if len(a) == 1:
	print(a[0])
elif len(a) == 2:
	print(a[0] + a[1])
elif len(a) == 3:
	print(a[0] + a[1] + a[2])
else:
	m2.append(a[0])
	for i in range(1, len(a)):
		if i % 2 == 1:
			m2.append(0)
		else:
			m2.append(m2[i - 2] + a[i])
	m1.append(a[0])
	m1.append(a[0] + a[1])
	m1.append(a[0] + a[1] + a[2])
	for i in range(3, len(a)):
		if i % 2 != 0:
			m1.append(a[i] + max(m2[i - 1], m1[i - 2]))
		else:
			m1.append(a[i] + max(max(m1[i - 1], m1[i - 2]), m2[i - 2]))
	print(max(m1))


s = list(input())
n = len(s)
a = []
b = []
c = 0
for i in range(n):
	if s[i] == 'a':
		c += 1
	a.append(c)
c = 0
for i in range(n):
	if s[i] == 'b':
		c += 1
	b.append(c)
m = 1
for i in range(n):
	for j in range(i + 1, n):
		l = b[j] - b[i] + a[i] + a[n - 1] - a[j]
		if s[i] == 'b':
			l += 1
		if s[j] == 'a':
			l += 1
		if l > m:
			m = l
print(m)


s = input()
dp = [[0, 0, 0] for k in range(len(s))]
if s[0] == 'a':
	dp[0][0] = 1
	dp[0][1] = 1
	dp[0][2] = 1
elif s[0] == 'b':
	dp[0][1] = 1
	dp[0][2] = 1
for pro in range(1, len(s)):
	if s[pro] == 'a':
		dp[pro][0] = dp[pro - 1][0] + 1
		dp[pro][1] = max(dp[pro - 1][1], dp[pro - 1][0] + 1)
		dp[pro][2] = max(dp[pro - 1][1], dp[pro - 1][2], dp[pro - 1][0]) + 1
	elif s[pro] == 'b':
		dp[pro][0] = dp[pro - 1][0]
		dp[pro][1] = max(dp[pro - 1][1], dp[pro - 1][0]) + 1
		dp[pro][2] = max(dp[pro - 1][2], dp[pro - 1][1] + 1)
	else:
		dp[pro][0] = dp[pro - 1][0]
		dp[pro][1] = max(dp[pro - 1][1], dp[pro - 1][0])
		dp[pro][2] = max(dp[pro - 1][2], dp[pro - 1][1], dp[pro - 1][0])
print(dp[len(s) - 1][2])


s = input()
ans = []
ab = [0]
bb = [0]
for i in range(len(s)):
	if s[i] == 'a':
		ab.append(ab[-1] + 1)
		bb.append(bb[-1])
	if s[i] == 'b':
		bb.append(bb[-1] + 1)
		ab.append(ab[-1])
maxa = 0
l = 0
r = 0
for l in range(len(ab)):
	for r in range(l, len(ab)):
		maxa = max(ab[l] + (ab[-1] - ab[r]) + (bb[r] - bb[l]), maxa)
print(maxa)


s = input()
n = len(s)
d = [[0] * 3 for _ in range(0, 5007)]
mx = 0
for i in range(0, n):
	d[i][0] = (d[i - 1][0] if i > 0 else 0) + (s[i] == 'a')
	d[i][1] = (max(d[i - 1][0], d[i - 1][1]) if i > 0 else 0) + (s[i] == 'b')
	d[i][2] = (max(d[i - 1][1], d[i - 1][2]) if i > 0 else 0) + (s[i] == 'a')
	for j in range(0, 3):
		mx = max(mx, d[i][j])
print(mx)


s = input()
n = len(s)
prefa = [0] * n
prefb = [0] * n
prefa[0] = s[0] == 'a'
prefb[0] = s[0] == 'b'
c = 0
for i in range(n):
	prefa[i] = prefa[i - 1] + (s[i] == 'a')
	prefb[i] = prefb[i - 1] + (s[i] == 'b')
	if i > 0:
		if s[i] != s[i - 1]:
			c += 1
if c < 2:
	print(n)
	exit()
elif c == 2:
	if s[0] == 'a':
		print(n)
		exit()
(ans1, ans) = (1, 0)
prefa.append(0)
prefb.append(0)
for i in range(n):
	for j in range(i, n):
		ans1 = prefa[n - 1] - prefa[j] + (prefb[j] - prefb[i - 1]) + prefa[i - 1]
		if ans1 > ans:
			ans = ans1
print(ans)