#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;
char s[N];
int n, p[N], sfMin[N], sfMax[N], add[N];
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  for (int i = 1; i <= n; ++i) {
    p[i] = p[i - 1];
    if (s[i] == '1') ++p[i];
  }
  for (int i = 0; i <= n; ++i) {
    p[i] <<= 1, p[i] -= i;
  }
  int firstMin = 0, lastMax = 0;
  for (int i = 0; i <= n; ++i) {
    if (p[i] >= p[lastMax]) lastMax = i;
    if (p[i] < p[firstMin]) firstMin = i;
  }
  sfMin[n + 1] = INT_MAX, sfMax[n + 1] = INT_MIN;
  for (int i = n; i >= 0; --i) {
    sfMin[i] = min(sfMin[i + 1], p[i]);
    sfMax[i] = max(sfMax[i + 1], p[i]);
  }
  int inc = 0;
  if (firstMin > lastMax) {
    for (int i = lastMax + 1; i <= firstMin; ++i) {
      if (s[i] != '?') continue;
      if (sfMax[i] + inc + 2 <= p[lastMax]) {
        inc += 2, add[i] += 2;
      }
    }
  }
  for (int i = 0; i <= n; ++i) {
    if (i) add[i] += add[i - 1];
    p[i] += add[i];
  }
  firstMin = 0, lastMax = 0;
  for (int i = 0; i <= n; ++i) {
    if (p[i] >= p[lastMax]) lastMax = i;
    if (p[i] < p[firstMin]) firstMin = i;
  }
  int ans = p[lastMax] - p[firstMin];
  cout << ans << '\n';
  return 0;
}
