#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 50;
const int INF = 1e9 + 7;
char S[N];
int n, a[N];
int chk(int lm) {
  static int sf[N];
  sf[n + 1] = -INF;
  for (int i = n; i >= 0; i--) sf[i] = max(sf[i + 1], a[i]);
  static bool o[N];
  int t = 0;
  for (int i = 1; i <= n; i++) {
    if (S[i] == '?' && sf[i] + t + 2 <= lm)
      t += 2, o[i] = true;
    else
      o[i] = false;
  }
  int mn = INF;
  for (int i = 1, sm = 0; i <= n; i++) {
    if (S[i] == '?')
      sm += o[i] ? 1 : -1;
    else
      sm += (S[i] == '0') ? -1 : 1;
    mn = min(mn, sm);
  }
  return lm - mn;
}
int main() {
  scanf("%s", S + 1);
  n = strlen(S + 1);
  for (int i = 1; i <= n; i++) a[i] = a[i - 1] + ((S[i] == '1') ? 1 : -1);
  int mx = -INF;
  for (int i = 0; i <= n; i++) mx = max(mx, a[i]);
  cout << min(chk(mx), chk(mx + 1));
  return 0;
}
