#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &t) {
  t = 0;
  char ch = getchar();
  int f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  do {
    (t *= 10) += ch - '0';
    ch = getchar();
  } while ('0' <= ch && ch <= '9');
  t *= f;
}
const int maxn = (1e6) + 10;
int n, sum[maxn], mx[maxn];
char s[maxn];
int solve(int mid) {
  int mn = 0, now = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '?') {
      if (mx[i] + 2 + now <= mid) {
        now += 2;
      }
    }
    mn = min(mn, sum[i] + now);
  }
  return mid - 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]--;
  }
  mx[n] = sum[n];
  for (int i = n - 1; i >= 1; i--) mx[i] = max(mx[i + 1], sum[i]);
  int l = 0, r = n, res, mid;
  while (l <= r) {
    mid = (l + r) / 2;
    if (solve(mid) < solve(mid + 1))
      r = mid - 1, res = mid;
    else
      l = mid + 1;
  }
  printf("%d\n", solve(res));
  return 0;
}
