S = list(input())
N = len(S)


Z = 0
SUM = 0
for s in S:
  if s == "1":
    SUM += 1
    Z = max(Z,SUM)
  else:
    SUM -= 1

MAX = 0
SUM = 0
ruiseki_max = []
for s in S[::-1]:
  if s == "1":
    SUM += 1
    MAX = max(MAX,SUM)
  else:
    SUM -= 1
  ruiseki_max.append(MAX)
    
def f(Z):
  SUM = 0
  MIN = 0
  for i in range(N):
    s = S[i]
    if s == "1":
      SUM += 1
    elif s == "0":
      SUM -= 1
      MIN = min(MIN,SUM)
    else:
      if SUM + ruiseki_max[N-2-i] <= Z:
        SUM += 1
      else:
        SUM -= 1
        MIN = min(MIN,SUM)
  return MIN

print(min(Z-f(Z),(Z+1-f(Z+1))))