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