#include <bits/stdc++.h>
using namespace std;
inline long long rd() {
  long long x = 0, w = 1;
  char c = getchar();
  while (!isdigit(c) && c != '-') c = getchar();
  if (c == '-') c = getchar(), w = -1;
  while (isdigit(c)) x = x * 10 + c - 48, c = getchar();
  return x * w;
}
const int N = 1000005;
int n, ans = N, p[N], a[N];
char s[N];
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  for (int i = (1); i <= (n); ++i) a[i] = a[i - 1] + (s[i] == '1' ? -1 : 1);
  p[n] = a[n];
  for (int i = (n - 1); i >= (1); --i) p[i] = min(p[i + 1], a[i]);
  for (int i = (n); i >= (1); --i)
    p[i] = min(0, p[i + 1] + (s[i] == '1' ? -1 : 1));
  for (int mn = (-n); mn <= (0); ++mn) {
    int c = 0, mx = 0;
    for (int i = (1); i <= (n); ++i) {
      if (s[i] == '0')
        c++;
      else if (s[i] == '1')
        c--;
      else if (c - 1 + p[i + 1] >= mn)
        c--;
      else
        c++;
      mx = max(mx, c);
    }
    ans = min(ans, mx - mn);
  }
  printf("%d\n", ans);
  return 0;
}
