#include <bits/stdc++.h>
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
const int INF = 1e9;
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string S;
  cin >> S;
  int N = S.size();
  vector<int> sum(N + 1);
  for (int i = 0; i < (int)(N); i++) {
    if (S[i] == '0')
      sum[i + 1] = sum[i] + 1;
    else
      sum[i + 1] = sum[i] - 1;
  }
  vector<int> maxi(N + 1, -INF);
  for (int i = (int)(N - 1); i >= (int)(0); i--) {
    maxi[i] = max(maxi[i + 1], sum[i + 1]);
  }
  auto getDiff = [&](int x) {
    int res = 0;
    int c = 0;
    for (int i = 0; i < (int)(N); i++) {
      if (S[i] == '?' && maxi[i] + c + 2 <= x) c += 2;
      chmin(res, sum[i + 1] + c);
    }
    return x - res;
  };
  int ans = min(getDiff(maxi[0]), getDiff(maxi[0] + 1));
  cout << ans << "\n";
  return 0;
}
