#include <bits/stdc++.h>
using namespace std;
const int N = 5100, INF = 1e9;
int ans, pref[N], suff[N];
string st;
int main() {
  cin >> st;
  st = ' ' + st;
  int n = st.length();
  for (int i = 1; i <= n; ++i) pref[i] = pref[i - 1] + (st[i] == 'b');
  for (int i = n; i; --i) suff[i] = suff[i + 1] + (st[i] == 'b');
  ans = INF;
  for (int i = 1; i <= n; ++i) {
    int k = 0;
    for (int j = i; j <= n; ++j) {
      k += (st[j] == 'a');
      ans = min(ans, k + pref[i - 1] + suff[j + 1]);
    }
  }
  cout << n - ans - 1;
  return 0;
}
