#include <bits/stdc++.h>
char s[1010000];
int n;
bool check(int len) {
  int l = -len, r = 0;
  for (int i = n; i >= 1; i--) {
    if (s[i] == '0') {
      if (r + len == 0)
        return 0;
      else
        r--, (l + len > 0 ? l-- : 0);
    }
    if (s[i] == '1') {
      if (l == 0)
        return 0;
      else
        l++, (r < 0 ? r++ : 0);
    }
    if (s[i] == '?') {
      if (l == r && (l == 0 || l + len == 0)) {
        if (l == 0)
          l--, r--;
        else
          l++, r++;
      } else {
        if (l + len > 0) l--;
        if (r < 0) r++;
      }
    }
  }
  return 1;
}
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  int l = 1, r = n + 1;
  while (l < r) {
    int mid = l + r >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  printf("%d\n", r);
}
