#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pii pair < int , int >
#define F first
#define S second
#define endl '\n'
#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define kill(x) return cout<<x<<'\n', 0;
using namespace std;
const int N=1e6+100;
ll dp[N];
ll a[N];
int32_t main(){
    string s;
    cin >> s;
    ll n=s.size();
    for (int i=1;i<=n;i++){
        if (s[i-1]=='1') a[i]=1;
        else a[i]=-1;
    }
    ll sig=0,ma=0;
    for (int i=1;i<=n;i++){
        sig+=a[i];
        ma=max(ma,sig);
    }
    for (int i=n;i;i--) dp[i]=max(dp[i],dp[i+1]+a[i]);
    sig=0;
    ll mi=0;
    //cout << ma << endl;
    ll ans=1e9;
    for (int j=1;j<=2;j++){
        sig=0,mi=0;
        for (int i=1;i<=n;i++){
            if (s[i-1]=='?'){
                if (sig+1+dp[i+1]>ma){
                    sig--;
                    mi=min(mi,sig);
                }
                else{
                    sig++;
                    mi=min(sig,mi);
                }
            }
            else{
                sig+=a[i];
                mi=min(mi,sig);
            }
        }
            ans=min(ans,ma-mi);
        ma++;
    }
    cout <<ans << endl;

}
