#include <bits/stdc++.h>
using namespace std;
char s[1000007];
int val[1000007], sum[1000007], r[1000007];
bool flag[1000007];
int n;
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  memset(flag, false, sizeof(flag));
  int mx = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '1')
      val[i] = 1;
    else if (s[i] == '0')
      val[i] = -1;
    else {
      flag[i] = true;
      val[i] = -1;
    }
    sum[i] = sum[i - 1] + val[i];
    mx = max(mx, sum[i]);
  }
  r[n] = sum[n];
  for (int i = n - 1; i; i--) r[i] = max(sum[i], r[i + 1]);
  int tmp = 0;
  for (int i = 1; i <= n; i++)
    if (flag[i]) {
      if (r[i] + tmp + 2 <= mx) {
        tmp += 2;
        val[i] = 1;
      }
    }
  int mn = 0;
  for (int i = 1; i <= n; i++) {
    sum[i] = sum[i - 1] + val[i];
    mn = min(mn, sum[i]);
  }
  int ans = mx - mn;
  for (int q = 0; q <= 2; q++) {
    mx = q;
    for (int i = 1; i <= n; i++) {
      if (s[i] == '1')
        val[i] = 1;
      else if (s[i] == '0')
        val[i] = -1;
      else {
        flag[i] = true;
        val[i] = -1;
      }
      sum[i] = sum[i - 1] + val[i];
    }
    r[n] = sum[n];
    for (int i = n - 1; i; i--) r[i] = max(sum[i], r[i + 1]);
    tmp = 0;
    for (int i = 1; i <= n; i++)
      if (flag[i]) {
        if (r[i] + tmp + 2 <= mx) {
          tmp += 2;
          val[i] = 1;
        }
      }
    mn = 0;
    for (int i = 1; i <= n; i++) {
      sum[i] = sum[i - 1] + val[i];
      mn = min(mn, sum[i]);
    }
    ans = min(ans, mx - mn);
  }
  printf("%d\n", ans);
  return 0;
}
