#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T read() {
  T x = 0, f = 0;
  char c = getchar();
  while (!isdigit(c)) f = c == '-', c = getchar();
  while (isdigit(c)) x = x * 10 + c - 48, c = getchar();
  return f ? -x : x;
}
namespace run {
const int N = 1e6 + 9;
int n, sum[N], g[N], ans = N;
char s[N], t[N];
inline void solve(int Z) {
  for (int i = 1; i <= n; i++) t[i] = s[i];
  for (int i = n; i >= 1; i--) g[i] = (Z - sum[i]) / 2;
  for (int i = n - 1; i >= 1; i--) g[i] = min(g[i], g[i + 1]);
  int now = 0;
  for (int i = 1; i <= n; i++)
    if (t[i] == '?') {
      if (now < g[i])
        t[i] = '1', now++;
      else
        t[i] = '0';
    }
  int tmp = 0, mn = N, mx = -N;
  for (int i = 1; i <= n; i++) {
    tmp += (t[i] == '1') * 2 - 1;
    mn = min(tmp, mn), mx = max(tmp, mx);
  }
  ans = min(ans, mx - mn);
}
int main() {
  scanf("%s", s + 1), n = strlen(s + 1);
  int Z = -N;
  for (int i = 1; i <= n; i++)
    sum[i] = sum[i - 1] + ((s[i] == '1') * 2 - 1), Z = max(Z, sum[i]);
  solve(Z), solve(Z + 1);
  printf("%d\n", ans);
  return 0;
}
}  // namespace run
int main() { return run::main(); }
