#include <bits/stdc++.h>
using namespace std;
template <typename tn>
void read(tn &a) {
  tn x = 0, f = 1;
  char c = ' ';
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  a = x * f;
}
const int N = 1001000;
char c[N];
int n, s[N], mx[N];
int solve(int lim) {
  for (int i = 1; i <= n; i++) {
    s[i] = s[i - 1] + (c[i] == '1' ? 1 : -1);
    if (s[i] > lim) return -1;
  }
  mx[n] = s[n];
  for (int i = n - 1; i; i--) mx[i] = max(mx[i + 1], s[i]);
  int tag = 0, mn = 1e9;
  for (int i = 1; i <= n; i++) {
    if (c[i] != '?') {
      mn = min(mn, tag + s[i]);
      continue;
    }
    if (mx[i] + tag + 2 <= lim) tag += 2;
    mn = min(mn, tag + s[i]);
  }
  return lim - mn;
}
int main() {
  ios::sync_with_stdio(0);
  cin >> c + 1;
  n = strlen(c + 1);
  int P = 0, now = 0;
  for (int i = 1; i <= n; i++) now += (c[i] == '1' ? 1 : -1), P = max(P, now);
  int ans = 1e9;
  for (int i = P; i <= P + 1; i++) ans = min(ans, solve(i));
  cout << ans << '\n';
  return 0;
}
