#include<string>
#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for(ll i = 0; i < (ll)n; i++)
#define rrep(i, n) for(ll i = (ll)(n-1); i >= 0; i--)

int main() {
    string S;
    cin>>S;
    ll N=S.size();
    //assume ? == 0
    vector<ll> sums(N,-1);
    ll x = 0;
    rep(i,N){
        char c = S[i];
        //x is sum
        if(c=='1'){
            x+=1;
        }else{
            x-=1;
        }
        sums[i]=x;
    }
    vector<ll> maxSums(N,-1);
    ll maxSum=sums[N-1];
    rrep(i,N){
        maxSum=max(maxSum,sums[i]);
        maxSums[i]=maxSum;
    }
    ll limit1 = max(maxSum,(ll) 0);//max(maxSum,(ll)0);
    ll limit2 = limit1+1;//max(maxSum+1,(ll) 0);

    string S1(S);
    string S2(S);

    ll cnt=0;
    rep(i,N){
        if (S[i]!='?') continue;
        ll cur = maxSums[i] + cnt*2;
        if (limit1  >= cur + 2){
            cnt += 1;
            S1[i] = '1';
        }else{
            S1[i] = '0';
        }
    }
    cnt=0;
    rep(i,N){
        if (S[i]!='?') continue;
        ll cur = maxSums[i] + cnt*2;
        if (limit2  >= cur + 2){
            cnt += 1;
            S2[i] = '1';
        }else{
            S2[i] = '0';
        }
    }

    ll min1=0;
    ll max1=0;
    x=0;
    rep(i,N){
        char c = S1[i];
        if(c=='1'){
            x+=1;
        }else{
            x-=1;
        }
        min1 = min(min1,x);
        max1 = max(max1,x);
    }
    ll min2=0;
    ll max2=0;
    x=0;
    rep(i,N){
        char c = S2[i];
        if(c=='1'){
            x+=1;
        }else{
            x-=1;
        }
        min2 = min(min2,x);
        max2 = max(max2,x);
    }
    ll ans = min(max1-min1,max2-min2);
    cout<<ans<<endl;
    return 0;
}