import copy
s = input()
a = len(s)
if (s[0]=="0"):
    d = [(-1,0)]
elif (s[0]=="1"):
    d = [(1,0)]
else:
    d = [(0,1)]   #(差、？の数)
for i in range(1,a):
    p,q = d[-1][0],d[-1][1]
    if (s[i]=="0"):
        d.append((p-1,q))
    elif (s[i]=="1"):
        d.append((p+1,q))
    else:
        d.append((p,q+1))
#print(d)
#now = [[] for _ in range(n+1)]
#now[0].append(0)
ans = [[0]]
for i in range(a):
    if (s[i]=="?"):
        for j in range(len(ans)):
            if (d[i][0]+ans[j][-1]<0):
                ans[j].append(ans[j][-1]+1)
            elif (d[i][0]+ans[j][-1]>0):
                ans[j].append(ans[j][-1]-1)
            else:
                z = copy.copy(ans[j])
                ans[j].append(ans[j][-1]-1)
                z.append(z[-1]+1)
                ans.append(z)
    else:
        for j in ans:
            j.append(j[-1])
print(ans)

res = float("inf")
for i in range(len(ans)):
    mi = 0
    ma = 0
    for j in range(a):
        x = d[j][0]+ans[i][j+1]
        mi = min(mi,x)
        ma = max(ma,x)
    res = min(res,ma-mi)
print(res)
    