#include <bits/stdc++.h>
using namespace std;
#define Maxn 1000007
char s[Maxn];
int val[Maxn],sum[Maxn],r[Maxn];
bool flag[Maxn];
int n;
int main()
{
    scanf("%s",s+1);
    n=strlen(s+1);
    memset(flag,false,sizeof(flag));
    int mx=0;
    for (int i=1;i<=n;i++)
    {
        if (s[i]=='1') val[i]=1;
        else if (s[i]=='0') val[i]=-1;
        else
        {
            flag[i]=true;
            val[i]=-1;
        }
        sum[i]=sum[i-1]+val[i];
        mx=max(mx,sum[i]);
    }
    r[n]=sum[n];
    for (int i=n-1;i;i--)
        r[i]=max(sum[i],r[i+1]);
    int tmp=0;
    for (int i=1;i<=n;i++)
        if (flag[i])
        {
            if (r[i]+tmp+2<=mx)
            {
                tmp+=2;
                val[i]=1;
            }
        }
    int mn=0;
    for (int i=1;i<=n;i++)
    {
        sum[i]=sum[i-1]+val[i];
        mn=min(mn,sum[i]);
    }
    int ans=mx-mn;
    ++mx;
    for (int i=1;i<=n;i++)
    {
        if (s[i]=='1') val[i]=1;
        else if (s[i]=='0') val[i]=-1;
        else
        {
            flag[i]=true;
            val[i]=-1;
        }
        sum[i]=sum[i-1]+val[i];
    }
    r[n]=sum[n];
    for (int i=n-1;i;i--)
        r[i]=max(sum[i],r[i+1]);
    tmp=0;
    for (int i=1;i<=n;i++)
        if (flag[i])
        {
            if (r[i]+tmp+2<=mx)
            {
                tmp+=2;
                val[i]=1;
            }
        }
    mn=0;
    for (int i=1;i<=n;i++)
    {
        sum[i]=sum[i-1]+val[i];
        mn=min(mn,sum[i]);
    }
    ans=min(ans,mx-mn);
    printf("%d\n",ans);
    return 0;
}