#include <bits/stdc++.h>
using namespace std;
mt19937 mrand(random_device{}());
const long long mod = 1000000007;
int rnd(int x) { return mrand() % x; }
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
const int N = 1010000;
int pre[N], maxpre[N], n;
char s[N];
int calc(int x) {
  pre[0] = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0' || s[i] == '?')
      pre[i + 1] = pre[i] - 1;
    else
      pre[i + 1] = pre[i] + 1;
  }
  maxpre[n] = pre[n];
  maxpre[n + 1] = -(1 << 29);
  for (int i = n - 1; i >= 0; i--) {
    maxpre[i] = max(maxpre[i + 1], pre[i]);
  }
  int cur = 0;
  int minpre = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') cur--;
    if (s[i] == '1') cur++;
    if (s[i] == '?') {
      if (cur + 1 + maxpre[i + 1] - pre[i + 1] <= x)
        cur++;
      else
        cur--;
    }
    minpre = min(minpre, cur);
  }
  return x - minpre;
}
int main() {
  scanf("%s", s);
  n = strlen(s);
  int minp = 0, cur = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0' || s[i] == '?') --cur;
    minp = max(minp, cur);
  }
  printf("%d\n", min(calc(minp), calc(minp + 1)));
}
