#include<bits/stdc++.h>
using namespace std;
char s[1000005];
int n,a[1000005],sum,mx[1000005],b[1000005];
int f(int x)
{
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        if(s[i]=='?')
        {
            if(mx[i]+(ans+1)*2<=x)ans++,b[i]=1;
            else b[i]=-1;
        }
        else if(s[i]=='1')b[i]=1;
        else b[i]=-1;
    }
    int anss=b[1];
    for(int i=2;i<=n;i++)b[i]+=b[i-1],anss=min(anss,b[i]);
    return min(anss,0);
}
int main()
{
    scanf("%s",s+1);
    n=strlen(s+1);
    for(int i=1;i<=n;i++)
        if(s[i]=='1')sum++,a[i]=a[i-1]+1;
        else sum--,a[i]=a[i-1]-1;
    mx[n]=a[n];
    for(int i=n-1;i>=1;i--)mx[i]=max(mx[i+1],a[i]);
    int z=max(mx[1],0);
    printf("%d",min(z-f(z),z+1-f(z+1)));
    return 0;
}