#include <bits/stdc++.h>
const int mod = 1e9 + 7;
const int inf = 1039074182;
const long long llinf = 1LL * inf * inf;
using namespace std;
template <typename T1, typename T2>
inline void chmin(T1 &x, T2 b) {
  if (b < x) x = b;
}
template <typename T1, typename T2>
inline void chmax(T1 &x, T2 b) {
  if (b > x) x = b;
}
template <typename T1, typename T2>
inline void chadd(T1 &x, T2 b) {
  x += b;
  if (x >= mod) x -= mod;
}
template <typename T1, typename T2>
inline void chmul(T1 &x, T2 b) {
  x = 1LL * x * b % mod;
}
template <typename T1, typename T2>
inline void chmod(T1 &x, T2 b) {
  x %= b, x += b;
  if (x >= b) x -= b;
}
string s;
int n;
int sum[1000005];
int maxch[1000005];
int main() {
  cin >> s;
  n = s.size();
  int mx = 0;
  for (int i = 0; i < n; i++) {
    sum[i] = (i ? sum[i - 1] : 0) + (s[i] == '1' ? 1 : -1);
    chmax(mx, sum[i]);
  }
  for (int i = n - 1; i >= 0; i--) {
    maxch[i] = min((i == n - 1 ? inf : maxch[i + 1]), (mx - sum[i]) / 2);
  }
  int ss = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] != '?') continue;
    if (ss < maxch[i]) {
      ss++;
      s[i] = '1';
    }
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '?') s[i] = '0';
  }
  int mn = 0;
  for (int i = 0; i < n; i++) {
    sum[i] = (i ? sum[i - 1] : 0) + (s[i] == '1' ? 1 : -1);
    chmin(mn, sum[i]);
  }
  cout << mx - mn << endl;
  return 0;
}
