#include <bits/stdc++.h>
template <typename T>
void fin(T const& t) {
  std::cout << t << std::endl;
  exit(0);
}
int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  std::string s;
  std::cin >> s;
  int ans = s.size();
  int aa[] = {0, 1, 0, 1};
  int bb[] = {0, -1, -1, 0};
  for (int i = 0; i < 4; ++i) {
    int a = aa[i], b = bb[i];
    int cur = 0;
    for (auto c : s) {
      if (c == '0') {
        --a;
        --b;
      } else if (c == '1') {
        ++a;
        ++b;
      } else {
        --a;
        ++b;
      }
      if (a < 0) a += 2;
      if (b > 0) b -= 2;
      cur = std::max(cur, a - b);
    }
    ans = std::min(ans, cur);
  }
  fin(ans);
  return 0;
}
