#include <bits/stdc++.h>
using namespace std;
char s[1000010];
int n, sufmx[1000010];
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  int cur = 0;
  for (int i = 1; i <= n; ++i) {
    cur += s[i] == '1' ? 1 : -1;
    sufmx[i] = cur;
  }
  for (int i = n - 1; i >= 0; --i) sufmx[i] = max(sufmx[i + 1], sufmx[i]);
  int mx = sufmx[0], add = 0;
  for (int i = 1; i <= n; ++i)
    if (s[i] == '?' && add + sufmx[i] + 2 <= mx) s[i] = '1', add += 2;
  int mi = 0;
  cur = 0;
  for (int i = 1; i <= n; ++i) {
    cur += s[i] == '1' ? 1 : -1;
    mi = min(mi, cur);
  }
  printf("%d", mx - mi);
  return 0;
}
