#include <bits/stdc++.h>
using namespace std;
template <typename tp>
inline void read(tp& x) {
  x = 0;
  char tmp;
  bool key = 0;
  for (tmp = getchar(); !isdigit(tmp); tmp = getchar()) key = (tmp == '-');
  for (; isdigit(tmp); tmp = getchar()) x = (x << 3) + (x << 1) + (tmp ^ '0');
  if (key) x = -x;
}
template <typename tp>
inline void ckmn(tp& x, tp y) {
  x = x < y ? x : y;
}
template <typename tp>
inline void ckmx(tp& x, tp y) {
  x = x < y ? y : x;
}
const int N = 1000010;
char str[N];
int n, val[N], mxp, mx[N];
int doit(int lim) {
  int cur = 0, mn = n;
  for (int i = (mxp + 1); i <= (n); ++i) {
    if (str[i] == '?') {
      if (mx[i] + 2 * (cur + 1) <= lim) {
        ++cur;
      }
    }
    mn = min(mn, val[i] + 2 * cur);
  }
  return lim - mn;
}
int main() {
  scanf("%s", str + 1);
  n = strlen(str + 1);
  for (int i = (1); i <= (n); ++i) {
    val[i] = val[i - 1];
    if (str[i] == '0' || str[i] == '?')
      --val[i];
    else
      ++val[i];
  }
  mxp = 0;
  for (int i = (1); i <= (n); ++i)
    if (val[i] > val[mxp]) mxp = i;
  for (int i = (n); i >= (1); --i) {
    mx[i] = val[i];
    if (i < n) mx[i] = max(mx[i], mx[i + 1]);
  }
  int ans = min(doit(val[mxp]), doit(val[mxp] + 1));
  printf("%d\n", ans);
  return 0;
}
