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 S[i]=="0":
            if (l>-1):
                return False
            r+=1
            l=min(l+1,0)
        elif S[i]=="1":
            if (1>r):
                return False
            r=max(r-1,0)
            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=max(r-1,0)
                l-=1
            elif (r>=-1 and -1>=l):
                r+=1
                l=min(l+1,0)
            else:
                return False


    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)