#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int INF = 1e9 + 7;
const int N = 5e3 + 7;
const int M = 1e6 + 7;
int sum_a[N], sum_b[N];
char s[N];
signed main() {
  ios::sync_with_stdio(false);
  cin >> (s + 1);
  int n = strlen(s + 1);
  sum_a[0] = sum_b[0] = 0;
  for (int i = 1; i <= n; i++) {
    sum_a[i] += sum_a[i - 1] + (s[i] == 'a');
    sum_b[i] += sum_b[i - 1] + (s[i] == 'b');
  }
  int ans = max(sum_a[n], sum_b[n]);
  for (int l = 1; l <= n; l++) {
    for (int r = l; r <= n; r++) {
      int _ans =
          sum_a[l - 1] + (sum_b[r] - sum_b[l - 1]) + (sum_a[n] - sum_a[r]);
      if (_ans > ans) {
        ans = _ans;
      }
    }
  }
  cout << ans << "\n";
  return 0;
}
