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 = [0]
res = []
for i in range(a):
    if (s[i]=="?"):
        if (d[i][0]+now[-1]<=0):
            now.append(now[-1]+1)
            res.append(1)
        else:
            now.append(now[-1]-1)
            res.append(0)
            
    else:
        now.append(now[-1])
#print(now)
mi = 0
ma = 0
l = []
for i in range(a):
    x = d[i][0]+now[i+1]
    l.append(x)
    mi = min(mi,x)
    ma = max(ma,x)
#print(res)
#print(l)
ans1 = ma-mi
#print(ans1)


now = [0]
res = []
for i in range(a):
    if (s[i]=="?"):
        if (d[i][0]+now[-1]<0):
            now.append(now[-1]+1)
            res.append(1)
        else:
            now.append(now[-1]-1)
            res.append(0)
            
    else:
        now.append(now[-1])
#print(now)
mi = 0
ma = 0
l = []
for i in range(a):
    x = d[i][0]+now[i+1]
    l.append(x)
    mi = min(mi,x)
    ma = max(ma,x)
#print(res)
#print(l)
ans2 = ma-mi
print(min(ans1,ans2))
    