#include <bits/stdc++.h>
using namespace std;
int n, m;
char ch[200006];
int A[200006], a[200006], B[200006], mn[200006], mmn = 0x3f3f3f3f;
bool us[200006];
void solve() {
  scanf("%s", ch + 1);
  n = strlen(ch + 1);
  int mx = 0, ps;
  for (int i = (1), iend = (n); i <= iend; ++i) {
    if (ch[i] == '?' || ch[i] == '0')
      A[i] = A[i - 1] - 1;
    else
      A[i] = A[i - 1] + 1;
    a[i] = A[i];
    mx = max(mx, A[i]);
  }
  for (int i = (1), iend = (n); i <= iend; ++i)
    if (A[i] == mx) ps = i;
  B[n] = A[n];
  for (int i = (n - 1), iend = (1); i >= iend; --i) B[i] = max(A[i], B[i + 1]);
  int t = 0;
  for (int i = (ps), iend = (n); i <= iend; ++i) {
    if (ch[i] == '?' && B[i] + t <= mx - 2) t += 2, us[i] = 1;
    A[i] += t;
  }
  mmn = 0;
  for (int i = (1), iend = (n); i <= iend; ++i) mmn = min(mmn, A[i]);
  int as = mx - mmn, chp = 0;
  memset(us, 0, sizeof us);
  for (int i = (1), iend = (n); i <= iend; ++i) A[i] = a[i];
  t = 0;
  for (int i = (ps), iend = (n); i <= iend; ++i) {
    if (ch[i] == '?' && B[i] + t <= mx - 1) t += 2, us[i] = 1;
    A[i] += t;
  }
  mmn = 0;
  for (int i = (1), iend = (n); i <= iend; ++i) mmn = min(mmn, A[i]);
  int as1 = mx + 1 - mmn, chp1 = 0;
  printf("%d\n", min(as1, as));
}
signed main() { solve(); }
