S = input()
N = len(S)

A = [0]*(N+1)
for i,s in enumerate(S,1):
    if s=="1":
        A[i] = A[i-1]+1
    else:
        A[i] = A[i-1]-1

m = max(A)
B = [A[-1]]*(N+1)
C = [m-A[-1]]*(N+1)
for i in range(N):
    i = N-i-1
    B[i] = max(A[i],B[i+1])
    C[i] = m-B[i]

d = 0
e = 0
D = A[:]
E = A[:]
for i,s in enumerate(S,1):
    if s=='?' and C[i] >= d+2:
        d += 2
    if s=='?' and C[i] >= e+1:
        e += 2
    D[i] += d
    E[i] += e
ans = min(max(D)-min(D),max(E)-min(E))
print(ans)