#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
char ch[N];
int n, cnt, c[N], tmp, res = 1e9;
inline void check(int mx) {
  int mn = 0, t = 0;
  for (int i = 1; i <= n; ++i) {
    if (ch[i] == '0')
      ++t;
    else if (ch[i] == '1')
      --t;
    else
      (t + 1 + c[i + 1] <= mx) ? ++t : --t;
    mn = min(mn, t);
  }
  res = min(res, mx - mn);
}
signed main() {
  scanf("%s", ch + 1);
  n = strlen(ch + 1);
  for (int i = n; i >= 1; --i) c[i] = max(0, c[i + 1] + ch[i] == '0' ? 1 : -1);
  for (int i = 1; i <= n; ++i) {
    ch[i] == '0' ? ++cnt : --cnt;
    tmp = max(tmp, cnt);
  }
  check(tmp), check(tmp + 1);
  return printf("%d\n", res), 0;
}
