#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double db;
typedef pair<int,int> pii;
#define fir first
#define sec second
#define rep(i,a,b) for (int i = (a) ; i <= (b) ; ++ i)
#define rrp(i,a,b) for (int i = (a) ; i >= (b) ; -- i)
#define gc() getchar()
template <typename tp>
inline void read(tp& x) {
  x = 0; char tmp; bool key = 0;
  for (tmp = gc() ; !isdigit(tmp) ; tmp = gc())
    key = (tmp == '-');
  for ( ; isdigit(tmp) ; tmp = gc())
    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;
  rep (i, 0, mxp) mn = min(mn, val[i]);
  rep (i, mxp + 1, n) {
    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);
  rep (i, 1, n) {
    val[i] = val[i-1];
    if (str[i] == '0' || str[i] == '?') -- val[i];
    else ++ val[i];
  }
  mxp = 0;
  rep (i, 1, n) if (val[i] > val[mxp]) mxp = i;
  rrp (i, n, 1) {
    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;
}
