#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 6;
int a[N], sl[N], sr[N], mnl[N], mnr[N];
int main() {
  static char s[N];
  scanf("%s", s);
  int n = strlen(s);
  for (int i = 1, iend = n; i <= iend; ++i)
    a[i] = s[i - 1] == '1' ? 1 : (s[i - 1] == '0' ? -1 : 0);
  int f = 0, mns = n;
  for (int i = 1, iend = n; i <= iend; ++i)
    mns = min(mns, f += a[i] >= 0 ? 1 : -1);
  sl[0] = 0;
  for (int i = 1, iend = n; i <= iend; ++i)
    sl[i] = sl[i - 1] + (a[i] >= 0 ? 1 : -1);
  mnl[n + 1] = n + 1;
  for (int i = n, iend = 1; i >= iend; --i) mnl[i] = min(mnl[i + 1], sl[i]);
  sr[n + 1] = 0;
  for (int i = n, iend = 0; i >= iend; --i)
    sr[i] = sr[i + 1] + (a[i] <= 0 ? 1 : -1);
  mnr[1] = n + 1;
  for (int i = 2, iend = n; i <= iend; ++i) mnr[i] = min(mnr[i - 1], sr[i]);
  int mn = n + 1;
  for (int i = 1, iend = n; i <= iend; ++i) {
    if (i < n && a[i + 1] <= 0) mn = min(mn, mnl[i + 1] - sl[i + 1] - 1);
    if (i > 1 && a[i] >= 0) mn = min(mn, mnr[i] - sr[i] - 1);
  }
  int ans = n + 1;
  for (int mx = 0, mxend = n; mx <= mxend; ++mx)
    ans = min(ans, mx - min(mns, mx + mn));
  cout << ans << endl;
  return 0;
}
