#include <bits/stdc++.h>
const int maxn = 1000005;
char s[maxn];
int pre[maxn];
int sufmax[maxn];
int sufmin[maxn];
int npre[maxn];
int check(int n, int K) {
  int premin = 0, premax = 0;
  int add = 0;
  for (int i = 1; i <= n; i++) {
    npre[i] = npre[i - 1] + (s[i] == '1' ? 1 : -1);
    if (s[i] == '?' and sufmax[i] + add + 2 - premin <= K) {
      add += 2;
      npre[i] += 2;
    }
    premin = std::min(premin, npre[i]);
  }
  int res = 0;
  sufmin[n] = npre[n];
  for (int i = n; i; i--) sufmin[i - 1] = std::min(sufmin[i], npre[i - 1]);
  for (int i = 1; i <= n; i++) {
    res = std::min(res, sufmin[i] - premax);
    premax = std::max(premax, npre[i]);
  }
  return res;
}
int main() {
  scanf("%s", s + 1);
  int n = int(strlen(s + 1));
  for (int i = 1; i <= n; i++) pre[i] = pre[i - 1] + (s[i] == '1' ? 1 : -1);
  sufmax[n] = pre[n];
  for (int i = n; i; i--) sufmax[i - 1] = std::max(sufmax[i], pre[i - 1]);
  int max = 0, premin = 0, p = 0;
  for (int i = 1; i <= n; i++) {
    p += s[i] == '1' ? 1 : -1;
    max = std::max(max, sufmax[i] - premin);
    premin = std::min(premin, p);
  }
  int l = max, r = n;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (check(n, mid) >= -mid)
      r = mid;
    else
      l = mid + 1;
  }
  printf("%d\n", l);
}
