import sys, io, os, re
import bisect
from pprint import pprint
from math import sin, cos, pi, radians, sqrt, floor
from copy import copy, deepcopy
from collections import deque
from fractions import gcd
from functools import reduce
from itertools import groupby
from heapq import heapify, heappush, heappop

n1 = lambda: int(sys.stdin.readline().strip())
nn = lambda: list(map(int, sys.stdin.readline().strip().split()))
f1 = lambda: float(sys.stdin.readline().strip())
fn = lambda: list(map(float, sys.stdin.readline().strip().split()))
s1 = lambda: sys.stdin.readline().strip()
sn = lambda: list(sys.stdin.readline().strip().split())
nl = lambda n: [n1() for _ in range(n)]
fl = lambda n: [f1() for _ in range(n)]
sl = lambda n: [s1() for _ in range(n)]
nm = lambda n: [nn() for _ in range(n)]
fm = lambda n: [fn() for _ in range(n)]
sm = lambda n: [sn() for _ in range(n)]

def array1(n, d=0): return [d] * n
def array2(n, m, d=0): return [[d] * m for x in range(n)]
def array3(n, m, l, d=0): return [[[d] * l for y in xrange(m)] for x in xrange(n)]

def ldec(A, d=1): return map(lambda x: x - d, A)

S = s1()

def f(S, N, n):
    (L, U) = (n, n)

    for i in range(len(S)):
        #print(N, n, i, L, U)

        if S[i] == "0":
            U -= 1
            L -= 1
            if L < 0:
                L += 2
                if L > U:
                    return -1
        
        if S[i] == "1":
            U += 1
            L += 1
            if U > N:
                U -= 2
                if L > U:
                    return 1

        if S[i] == "?":
            U += 1
            L -= 1
            if U > N: 
                U -= 2
                if L > U:
                    return 1
            if L < 0: 
                L += 2
                if L > U:
                    return -1


    return 0

def solve(S, N):
    (lower, upper) = (0, N)

    while lower <= upper:
        n = (lower + upper) // 2
        t = f(S, N, n)
        #print("{} = f(S, {}, {})".format(t, N, n))

        if lower < upper:
            if t == 1:
                upper = n
            if t == -1:
                lower = n + 1
            if t == 0:
                return True
        else:
            if t == 0:
                return True
            else:
                return False

upper = len(S) + 1
lower = 0
#while True:
for i in range(10):
    x = (upper + lower) // 2
    #print(upper, lower, x)
    if solve(S, x):
        upper = x
    else:
        lower = x + 1

    if lower == upper:
        print(lower)
        break