#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
template <class T>
inline void amin(T &x, const T &y) {
  if (y < x) x = y;
}
template <class T>
inline void amax(T &x, const T &y) {
  if (x < y) x = y;
}
template <class Iter>
void rprintf(const char *fmt, Iter begin, Iter end) {
  for (bool sp = 0; begin != end; ++begin) {
    if (sp)
      putchar(' ');
    else
      sp = true;
    printf(fmt, *begin);
  }
  putchar('\n');
}
int N;
char S[1000011];
int H[1000011];
void MAIN() {
  scanf("%s", S);
  N = strlen(S);
  int ans = 1 << 29;
  for (int a = 0, a_len = (2); a < a_len; ++a) {
    for (int b = 0, b_len = (2); b < b_len; ++b) {
      int cur = 0;
      int ma = 0;
      for (int i = N; i--;) {
        if (S[i] == '1')
          cur--;
        else
          cur++;
        amax(ma, cur);
        H[i] = ma - cur;
      }
      cur = 0;
      int height = *max_element(H, H + N);
      int mi = 0;
      ma = 0;
      for (int i = 0, i_len = (N); i < i_len; ++i) {
        if (S[i] == '0')
          cur--;
        else if (S[i] == '1')
          cur++;
        else {
          if (cur + 1 <= height && cur + 1 + H[i + 1] <= height) {
            cur++;
          } else {
            cur--;
          }
        }
        amin(mi, cur);
        amax(ma, cur);
      }
      amin(ans, ma - mi);
      reverse(S, S + N);
    }
    for (int i = 0, i_len = (N); i < i_len; ++i)
      if (S[i] != '?') S[i] ^= 1;
  }
  printf("%d\n", ans);
}
int main() {
  int TC = 1;
  for (int tc = 0, tc_len = (TC); tc < tc_len; ++tc) MAIN();
  return 0;
}
