#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 5;
char buf[1 << 12], *pp1 = buf, *pp2 = buf, nc;
int ny;
inline char gc() { return getchar(); }
inline int read() {
  int x = 0;
  for (ny = 1; nc = gc(), (nc < 48 || nc > 57) && nc != EOF;)
    if (nc == 45) ny = -1;
  if (nc < 0) return nc;
  for (x = nc - 48; nc = gc(), 47 < nc && nc < 58 && nc != EOF;
       x = (x << 3) + (x << 1) + (nc ^ 48))
    ;
  return x * ny;
}
char s[MAXN];
int n, a[MAXN], Min[MAXN];
inline int Solve(int lim) {
  static int b[MAXN];
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '?' && Min[i] - 2 * cnt - 2 >= lim) cnt++;
    b[i] = a[i] - 2 * cnt;
  }
  int Max = 0;
  for (int i = 1; i <= n; i++) Max = max(Max, b[i]);
  return Max - lim;
}
int main() {
  scanf("%s", s + 1), n = strlen(s + 1);
  for (int i = 1; i <= n; i++)
    if (s[i] == '0')
      a[i] = a[i - 1] - 1;
    else
      a[i] = a[i - 1] + 1;
  Min[n + 1] = n + 1;
  for (int i = n; i >= 1; i--) Min[i] = min(Min[i + 1], a[i]);
  cout << min(Solve(Min[1]), Solve(Min[1] - 1));
  return 0;
}
