#include <bits/stdc++.h>
int main() {
  using namespace std;
  string S;
  cin >> S;
  unsigned long N{S.size()};
  long ans{numeric_limits<long>::max()};
  for (unsigned long iter{0}; iter < 20; ++iter) {
    long lb{0}, ub{0}, nl{0}, nu{0};
    for (unsigned long i{0}; i < N; ++i) {
      if (S[i] == '0') {
        --nl;
        --nu;
        lb = min(lb, nu);
        nl = max(lb, nl);
        if ((nl ^ nu) & 1) ++nl;
      } else if (S[i] == '1') {
        ++nl;
        ++nu;
        ub = max(ub, nl);
        nu = min(ub, nu);
        if ((nl ^ nu) & 1) --nu;
      } else {
        if (ub == lb) {
          ub = nl = nu = 1;
        } else {
          ++nu;
          --nl;
          nl = max(lb, nl);
          if ((nl ^ nu) & 1) ++nl;
          nu = min(ub, nu);
          if ((nl ^ nu) & 1) --nu;
        }
      }
    }
    ans = max(ans, ub - lb);
    rotate(begin(S), begin(S) + (random_device{}() % N), end(S));
  }
  cout << ans << endl;
  return 0;
}
