#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
  x = 0;
  char c;
  for (c = getchar(); !isdigit(c); c = getchar())
    ;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
}
const int N = 1e6 + 50, P = 1e9 + 7, oo = P;
int mul(int a, int b) { return 1ll * a * b % P; }
int sub(int a, int b) {
  a -= b;
  return a < 0 ? a + P : a;
}
int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }
int z, n, sum[N], mx[N], ans;
char s[N];
int main(int argc, char *argv[]) {
  scanf("%s", s + 1);
  z = 0, n = strlen(s + 1);
  for (int i = 1; i <= (n); ++i) {
    sum[i] = sum[i - 1] + (s[i] == '1' ? 1 : -1);
    (z < sum[i] ? z = sum[i] : z);
  }
  mx[n + 1] = -oo;
  for (int i = n; i >= (1); --i) {
    mx[i] = max(mx[i + 1], sum[i]);
  }
  ans = oo;
  for (int k = 0; k < (2); ++k) {
    int w = 0, mn = oo;
    for (int i = 1; i <= (n); ++i) {
      if (s[i] == '?' && w + mx[i] + 2 <= z + k) {
        w += 2;
      }
      (mn > w + sum[i] ? mn = w + sum[i] : mn);
    }
    (ans > z + k - mn ? ans = z + k - mn : ans);
  }
  cout << ans << endl;
  return 0;
}
