
#include <bits/stdc++.h>  
using namespace std;

typedef long long ll;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    string s;
    cin>>s;
    ll n = s.size();
    vector<ll> cum_sum;
    cum_sum.push_back(0); //cum_sum stores cumulative sum assuming 0s and ?s are -1
    for(auto c:s)
        cum_sum.push_back(cum_sum.back() + (c=='1'?1:-1));
    vector<ll> max_sum_to_right = cum_sum;
    for(ll i=n-1;i>=0;i--)
        max_sum_to_right[i] = max(max_sum_to_right[i],max_sum_to_right[i+1]);
    
    ll mx = -1;
    for(ll i=0;i<=n;i++)
        mx = max(mx,cum_sum[i]);
    ll ans = INT64_MAX;
    for(ll random_variable=0;random_variable<2;random_variable++){
        ll cur = 0,mn = 0;
        for(ll i=0;i<n;i++){
            if(s[i] == '?'){
                if(cur + max_sum_to_right[i+1] - cum_sum[i] +2 <=mx)
                    cur++;
                else
                    cur--;
            }
            else if(s[i] == '0')
                cur--;
            else 
                cur++;
            mn = min(mn,cur);
        }
        ans = min(ans,mx-mn);
        mx++;
    }
    cout<<ans<<endl;
    return 0;
}