s = input()
mi = 0
ma = 10 ** 6


def check(m):
    a = 0
    b = 0
    for i in s:
        if i == "0":
            if a + 1 > m:
                a = a - 1
            else:
                a = a + 1
            b = b + 1
        elif i == "1":
            if b - 1 < 0:
                b = b + 1
            else:
                b = b - 1
            a = a - 1
        else:
            if a + 1 > m:
                a = a - 1
            else:
                a = a + 1
            if b - 1 < 0:
                b = b + 1
            else:
                b = b - 1
        if b > m or a < 0:
            return False
    return a >= b


def check2(m):
    a = 0
    b = 0
    for i in s:
        if i == "0":
            if a + 1 > 0:
                a = a - 1
            else:
                a = a + 1
            b = b + 1
        elif i == "1":
            if b - 1 < -m:
                b = b + 1
            else:
                b = b - 1
            a = a - 1
        else:
            if a + 1 > 0:
                a = a - 1
            else:
                a = a + 1
            if b - 1 < -m:
                b = b + 1
            else:
                b = b - 1
        if b > 0 or a < -m:
            return False
    return a >= b


# print(check(1))
# input()
while mi != ma:
    mm = (mi + ma) // 2
    if check(mm) or check2(mm):
        ma = mm
    else:
        mi = mm + 1
print(mi)
