#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int first = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar()) {
    if (ch == '-') f = -1;
  }
  for (; isdigit(ch); ch = getchar()) {
    first = first * 10 + ch - 48;
  }
  return first * f;
}
const int mxN = 1e6;
char a[mxN + 3];
int b[mxN + 3];
int s[mxN + 3];
int mn[mxN + 3];
int n;
int main() {
  scanf("%s", a + 1);
  n = strlen(a + 1);
  int cur = 0, cur2 = 0, lb = 0;
  for (int i = 1; i <= n; i++) {
    b[i] = (a[i] == '1' ? -1 : 1);
    s[i] = s[i - 1] + b[i];
    lb = min(lb, s[i]);
  }
  mn[n] = s[n];
  for (int i = n - 1; i >= 1; i--) {
    mn[i] = min(s[i], mn[i + 1]);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] == '?') {
      if (mn[i] - cur * 2 - 2 >= lb) {
        cur++;
        b[i] = -1;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    s[i] = s[i - 1] + b[i];
    ans = max(ans, s[i]);
  }
  ans -= lb;
  mn[n] = s[n];
  for (int i = n - 1; i >= 1; i--) {
    mn[i] = min(s[i], mn[i + 1]);
  }
  int ans2 = 0;
  lb--;
  cur = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] == '?' && b[i] == 1) {
      if (mn[i] - cur * 2 - 2 >= lb) {
        cur++;
        b[i] = -1;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    s[i] = s[i - 1] + b[i];
    ans2 = max(ans2, s[i]);
  }
  ans2 -= lb;
  printf("%d\n", min(ans, ans2));
  return 0;
}
