#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
inline int read() {
  int x = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') w = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return w == 1 ? x : -x;
}
char s[1000005];
int len, suf[1000005], pre[1000005], mn, ans;
int main() {
  scanf("%s", s + 1);
  len = strlen(s + 1);
  mn = 1e9;
  ans = 1e9;
  for (int i = 1; i <= len; i++)
    pre[i] = pre[i - 1] + (s[i] == '0' ? -1 : 1), mn = min(mn, pre[i]);
  mn--;
  suf[len] = pre[len];
  for (int i = len - 1; i >= 1; i--) suf[i] = min(suf[i + 1], pre[i]);
  for (int j = 1; j <= 3; j++) {
    int mx = 0, nw = 0, p = 0;
    for (int i = 1; i <= len; i++) {
      if (s[i] != '?')
        nw += (s[i] == '0' ? -1 : 1);
      else {
        if (suf[i] - pre[i - 1] + nw - 2 >= mn)
          nw--;
        else
          nw++;
      }
      p = max(p, nw);
    }
    ans = min(ans, p - mn);
    mn++;
  }
  cout << ans << endl;
  return 0;
}
