#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
using namespace std;
const long long N = 1e6 + 100;
long long long long dp[N];
long long long long a[N];
int32_t main() {
  string s;
  cin >> s;
  long long long long n = s.size();
  for (long long i = 1; i <= n; i++) {
    if (s[i - 1] == '1')
      a[i] = 1;
    else
      a[i] = -1;
  }
  long long long long sig = 0, ma = 0;
  for (long long i = 1; i <= n; i++) {
    sig += a[i];
    ma = max(ma, sig);
  }
  for (long long i = n; i; i--) dp[i] = max(dp[i], dp[i + 1] + a[i]);
  sig = 0;
  long long long long mi = 0;
  for (long long i = 1; i <= n; i++) {
    if (s[i - 1] == '?') {
      if (sig + 1 + dp[i + 1] > ma) {
        sig--;
        mi = min(mi, sig);
      } else {
        sig++;
        mi = min(sig, mi);
      }
    } else {
      sig += a[i];
      mi = min(mi, sig);
    }
  }
  cout << ma - mi << '\n';
}
