#include <bits/stdc++.h>
using namespace std;
char S[1000005];
int main() {
  scanf("%s", &S);
  int n = strlen(S);
  int l = 0, r = 0;
  int a = 0, b = 0;
  for (int i = 0; i < n; i++) {
    if (S[i] == '0') {
      a--, b--;
      if (b < l)
        l = b;
      else if (a < l)
        a += 2;
    } else if (S[i] == '1') {
      a++, b++;
      if (r < a)
        r = a;
      else
        b -= 2;
    } else {
      if (l == r) {
        r++;
        a = b = r;
      } else {
        a--, b++;
        if (a < l) a += 2;
        if (r < b) b -= 2;
      }
    }
  }
  int ret = r - l;
  l = r = 0;
  a = b = 0;
  for (int i = 0; i < n; i++) {
    if (S[i] == '0') {
      a--, b--;
      if (b < l)
        l = b;
      else if (a < l)
        a += 2;
    } else if (S[i] == '1') {
      a++, b++;
      if (r < a)
        r = a;
      else
        b -= 2;
    } else {
      if (l == r) {
        l--;
        a = b = l;
      } else {
        a--, b++;
        if (a < l) a += 2;
        if (r < b) b -= 2;
      }
    }
  }
  ret = min(ret, r - l);
  printf("%d\n", ret);
}
