#include <bits/stdc++.h>
using namespace std;
string s;
int cna[5010], cnb[5010];
int main(int argc, char const *argv[]) {
  cin >> s;
  s = ' ' + s;
  memset(cna, 0, sizeof(cna));
  memset(cnb, 0, sizeof(cnb));
  int n = s.length();
  for (int i = 1; i <= n; i++) {
    if (s[i] == 'a') {
      cna[i]++;
    } else if (s[i] == 'b') {
      cnb[i]++;
    }
    cna[i] += cna[i - 1];
    cnb[i] += cnb[i - 1];
  }
  int ans = INT_MIN, tempb, tempf, tempin;
  for (int i = 1; i <= n; i++) {
    for (int j = i; j <= n; j++) {
      tempb = cna[i - 1];
      tempf = cna[n] - cna[j];
      tempin = cnb[j] - cnb[i - 1];
      ans = max(ans, tempb + tempin + tempf);
    }
  }
  cout << ans << "\n";
  return 0;
}
