#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline long long rand(long long x, long long y) {
  return rng() % (y + 1 - x) + x;
}
string inline to_string(char c) {
  string second(1, c);
  return second;
}
template <typename T>
inline T gcd(T a, T b) {
  return a == 0 ? llabs(b) : gcd(b % a, a);
}
using ll = long long;
using ld = long double;
using pi = pair<ll, ll>;
using spi = pair<ll, pi>;
using dpi = pair<pi, pi>;
ll n, psum[(300006)];
string A;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> A;
  n = A.size();
  A = '*' + A;
  for (ll i = 1; i <= ll(n); ++i) {
    psum[i] = psum[i - 1] + (A[i] == '1' ? 1 : -1);
  }
  ll Z = *max_element(psum, psum + n + 1);
  auto first = [&](ll x) {
    memset((psum), 0, sizeof((psum)));
    for (ll i = 1; i <= ll(n); ++i) {
      psum[i] = psum[i - 1] + (A[i] == '1' ? 1 : -1);
    }
    vector<ll> suf_max(n + 2, -((long long)1e18));
    for (ll i = n; i >= ll(1); --i) suf_max[i] = max(suf_max[i + 1], psum[i]);
    ll change = 0;
    for (ll i = 1; i <= ll(n); ++i) {
      if (A[i] == '?') {
        if (suf_max[i] + 2 * (change + 1) <= x) ++change;
      }
      psum[i] += 2 * change;
    }
    return *min_element(psum, psum + n + 1);
  };
  cout << min(Z - first(Z), Z + 1 - first(Z + 1)) << '\n';
}
