S=input()
N=len(S)

def condition(n):
    l=-n
    r=n
    for i in range(N-1,-1,-1):
        l=max(-n,l)
        r=min(n,r)
        if l>r:
            return False
        if (1>r or l>1) and (-1>r or l>-1):
            return False
        if S[i]=="0":
            if (-1>r or l>-1):
                return False
            r+=1
            l+=1
        elif S[i]=="1":
            if (1>r or l>1):
                return False
            r-=1
            l-=1
        else:
            if (r>=1 and 1>=l) and (r>=-1 and -1>=l):
                r+=1
                l-=1
            elif (r>=1 and 1>=l):
                r-=1
                l-=1
            else:
                r+=1
                l+=1
                
    if r>=0 and 0>=l:
        return True

start=0
end=10**6
while end-start>1:
    test=(end+start)//2
    if condition(test):
        end=test
    else:
        start=test

if condition(start):
    print(start)
else:
    print(end)