#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e6 + 10;
int n, m;
int sum[maxN + 1], mx[maxN + 1];
char s[maxN + 1];
inline int solve(int rule) {
  int res = 0, mn = 1e9;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '?' && mx[i] + res + 2 <= rule) res += 2;
    mn = min(mn, sum[i] + res);
  }
  return mn;
}
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  for (int i = 1; i <= n; i++) {
    sum[i] = sum[i - 1];
    if (s[i] == '1')
      sum[i]++;
    else
      sum[i]--;
    m = max(m, sum[i]);
  }
  mx[n] = sum[n];
  for (int i = n - 1; i >= 1; i--) mx[i] = max(mx[i + 1], sum[i]);
  printf("%d\n", min(m - solve(m), m + 1 - solve(m + 1)));
  return 0;
}
