#include<bits/stdc++.h> 
using namespace std;
typedef long long ll;
template<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;}
template<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>=b)return 0; a=b; return 1;}
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
long double eps = 1e-9;
long double pi = acos(-1);


signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(20);

    string s;
    cin>>s;
    int n = s.size();
    int sum[n+1]={};
    for(int i=0;i<n;i++){
        if(s[i] == '1')sum[i+1]++;
        else sum[i+1]--;
        sum[i+1] += sum[i];
    }
    int ma[n+1]={};
    int mi[n+1]={};
    mi[n]=1e9, ma[n]=-1e9;
    for(int i=n-1;i>=0;i--){
        ma[i] = max(ma[i+1],sum[i+1]);
        mi[i] = min(mi[i+1],sum[i+1]);
    }
    int ans = ma[0]-mi[0];
    int mx = ma[0];
    for(int t=0;t<2;t++){
        int now = 0, cnt = 0;
        int mini = 0 ,maa=0;
        for(int i=0;i<n;i++){
            if(s[i]=='0')now--;
            else if(s[i] == '1')now++;
            else{
                if(now+ma[i]-sum[i]+2 <= mx){
                    now++;
                }
                else now--;
            }
            chmin(mini,now);
            chmax(maa,now);
        }
        chmin(ans,maa-mini);
        mx++;
    }
    cout << ans << endl;
}