#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 5;
const int inf = 1e9;
char s[N];
int n, a[N], b[N];
int go(int z) {
  int res = 0, ans = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '?' && a[i] + res + 2 <= z) res += 2;
    ans = min(ans, b[i] + res);
  }
  return z - ans;
}
int main() {
  int sum = 0;
  scanf("%s", s), n = strlen(s);
  for (int i = 0; i < n; i++) {
    if (s[i] == '0')
      ++sum;
    else
      --sum;
    a[i] = b[i] = sum;
  }
  for (int i = n - 2; ~i; i--) a[i] = max(a[i], a[i + 1]);
  printf("%d\n", min(go(max(0, a[0])), go(max(0, a[0] + 1))));
  return 0;
}
