#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 5;
int n;
char s[N];
int g[N];
int v[N], h[N];
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  int mx = 0, ss = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '1')
      ss++;
    else
      ss--;
    v[i] = h[i] = ss;
    mx = max(mx, ss);
  }
  int mn = 0;
  for (int i = n - 1; i; i--) v[i] = max(v[i], v[i + 1]);
  ss = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '1')
      ss++;
    else if (s[i] == '0')
      ss--;
    else if (s[i] == '?') {
      if (ss + v[i] + 2 - h[i - 1] <= mx)
        ss++;
      else
        ss--;
    }
    mn = min(mn, ss);
  }
  int ans = mx - mn;
  ++mx;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '1')
      ss++;
    else if (s[i] == '0')
      ss--;
    else if (s[i] == '?') {
      if (ss + v[i] + 2 - h[i - 1] <= mx)
        ss++;
      else
        ss--;
    }
    mn = min(mn, ss);
  }
  ans = min(ans, mx - mn);
  printf("%d\n", ans);
}
