#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
char s[N];
int n,a[N],b[N];
int solve(int mx)
{
    int add=0;
    for(int i=1;i<=n;i++)
        if(s[i]!='?') b[i]=s[i]=='1'?1:-1;
        else
        {
            if(a[i]+add+2<=mx)
                b[i]=1,add+=2;
            else b[i]=-1;
        }
    for(int i=1;i<=n;i++) b[i]+=b[i-1];
    int mn=0;mx=0;
    for(int i=1;i<=n;i++)
        mn=min(mn,b[i]),mx=max(mx,b[i]);
    return mx-mn;
}
int main()
{
    scanf("%s",s+1);
    n=strlen(s+1);
    for(int i=1;i<=n;i++) a[i]=a[i-1]+(s[i]=='1'?1:-1);
    for(int i=n-1;i>=1;i--) a[i]=max(a[i],a[i+1]);
    int ans=min(solve(a[1]),solve(a[1]+1));
    ans=min(ans,solve(a[1]+2));
    ans=min(ans,solve(a[1]+3));
    ans=min(ans,solve(a[1]+4));
    printf("%d\n",ans);
}
