#include <bits/stdc++.h>
long long IN(void) {
  long long x = 0;
  short f = 0, c = getchar();
  while (c < 48 || c > 57) {
    f ^= c == 45, c = getchar();
  }
  while (c > 47 && c < 58) {
    x = x * 10 + c - 48, c = getchar();
  }
  return f ? -x : x;
}
signed main(void) {
  char c, S[1000000];
  int a = 0x7f7f7f7f, cusum[1000000] = {0}, len = 1, max = 0, min, temp, RT = 2,
      rv[1000000] = {0};
  if (scanf("%s", S) != 0) len = strlen(S);
  for (int i = 0; i < len; i++) {
    temp = cusum[i] + ((S[i] == '1') ? 1 : -1);
    cusum[i + 1] = temp;
    rv[i + 1] = temp;
    max = (cusum[i + 1] < max) ? max : cusum[i + 1];
  }
  for (int i = len - 1; i >= 0; i--) {
    temp = (rv[i] < rv[i + 1]) ? rv[i + 1] : rv[i];
    rv[i] = temp;
  }
  while (RT--) {
    temp = 0, min = 0;
    for (int i = 0; i < len; i++) {
      if (S[i] == '?') {
        if (temp + rv[i + 1] - cusum[i] + 2 <= max) {
          temp++;
        } else
          temp--;
      } else if (S[i] == '0') {
        temp--;
      } else {
        temp++;
      }
      min = (min < temp) ? min : temp;
    }
    temp = (a < max - min) ? a : max - min;
    a = temp;
    max++;
  }
  return printf("%d\n", a), 0;
}
