#include <bits/stdc++.h>
using namespace std;
template <class T>
void read(T &x) {
  int f = 0;
  x = 0;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar()) f |= (ch == '-');
  for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
  if (f) x = -x;
}
const int N = 1000005;
int f[N], n, mn, sum, mx;
char s[N];
int solve() {
  n = strlen(s + 1);
  for (int i = (n); i >= (1); i--) {
    f[i] = f[i + 1];
    if (s[i] == '0')
      f[i]--;
    else
      f[i]++;
    f[i] = min(f[i], 0);
  }
  mn = f[1];
  sum = mx = 0;
  for (int i = (1); i <= (n); i++) {
    if (s[i] == '0')
      sum--;
    else if (s[i] == '1')
      sum++;
    else {
      if (sum - 1 + f[i + 1] < mn)
        sum++;
      else
        sum--;
    }
    mx = max(mx, sum);
  }
  return mx - mn;
}
int main() {
  scanf("%s", s + 1);
  int n = strlen(s + 1);
  int ans = solve();
  for (int i = (1); i <= (n); i++) {
    if (s[i] == '1')
      s[i] = '0';
    else if (s[i] == '0')
      s[i] = '1';
  }
  ans = min(ans, solve());
  reverse(s + 1, s + n + 1);
  ans = min(ans, solve());
  cout << ans << endl;
  return 0;
}
