#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int n;
char s[N];
int sum[N], pmx[N], pmi[N];
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  sum[0] = 0;
  pmx[0] = 0;
  pmi[0] = 0;
  for (int i = 1; i <= n; i++) {
    sum[i] = sum[i - 1] + 1 - 2 * (s[i] == '0');
    pmx[i] = max(pmx[i - 1], sum[i]);
    pmi[i] = min(pmi[i - 1], sum[i]);
  }
  int suf_mi = sum[n], suf_mx = sum[n];
  int ans = pmx[n] - pmi[n];
  for (int i = n; i; i--) {
    suf_mi = min(suf_mi, sum[i]);
    suf_mx = max(suf_mx, sum[i]);
    if (s[i] == '?') {
      if (pmx[i - 1] < suf_mx) {
        ans =
            min(ans, max(pmx[i - 1], suf_mx - 2) - min(pmi[i - 1], suf_mi - 2));
      }
      if (pmx[i - 1] + 1 < suf_mx) {
        suf_mx -= 2;
        suf_mi -= 2;
        ans = min(ans, max(pmx[i - 1], suf_mx) - min(pmi[i - 1], suf_mi));
      }
    }
  }
  suf_mi = sum[n], suf_mx = sum[n];
  bool found = false;
  for (int i = n; i; i--) {
    suf_mi = min(suf_mi, sum[i]);
    suf_mx = max(suf_mx, sum[i]);
    if (s[i] == '?') {
      if (pmx[i - 1] + 1 == suf_mx) {
        ans =
            min(ans, max(pmx[i - 1], suf_mx - 2) - min(pmi[i - 1], suf_mi - 2));
        if (!found) {
          suf_mi -= 2;
          suf_mx -= 2;
          found = true;
        }
      } else if (pmx[i - 1] + 1 < suf_mx) {
        assert(found);
        suf_mx -= 2;
        suf_mi -= 2;
        ans = min(ans, max(pmx[i - 1], suf_mx) - min(pmi[i - 1], suf_mi));
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
