#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;
string A;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> A;
  n = ll(A.size());
  A = '*' + A;
  auto bstar = [&](ll x) {
    vector<ll> psum(n + 1, 0), suf_min(n + 1, 0);
    for (ll i = 1; i <= ll(n); ++i)
      psum[i] = psum[i - 1] + (A[i] == '0' ? -1 : 1);
    for (ll i = n; i >= ll(1); --i)
      suf_min[i] = min(i == n ? ((long long)1e18) : suf_min[i + 1], psum[i]);
    ll mx = 0, change = 0, mi = 0;
    for (ll i = 1; i <= ll(n); ++i) {
      if (mx - psum[i] > x) return 0;
      assert(mx - (psum[i] - 2 * change) <= x);
      if (A[i] == '?') {
        if (mx - (suf_min[i] - 2 * (change + 1)) <= x &&
            (psum[i] - 2 * change) - mi > x)
          ++change;
      }
      if ((psum[i] - 2 * change) - mi > x) return 0;
      mx = max(mx, psum[i] - 2 * change);
      mi = min(mi, psum[i] - 2 * change);
    }
    return 1;
  };
  ll st = 0, en = n;
  while (en - st > 1) {
    ll mid = (st + en) >> 1;
    if (bstar(mid))
      en = mid;
    else
      st = mid;
  }
  cout << en << '\n';
}
