# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

# 二分探索、左右からの貪欲法、?をスキップした状態で1の連続数と0の連続数
S = list(sr())
answer = 0

def solve(S, bl):
    global answer
    one_seq = 0
    one_max = 0
    zero_seq = 0
    zero_max = 0
    cur = 0
    for i, s in enumerate(S):
        if s == '1':
            one_seq += 1
            zero_seq -= 1
            zero_seq = max(0, zero_seq)
            cur += 1
        elif s == '0':
            zero_seq += 1
            one_seq -= 1
            one_seq = max(0, one_seq)
            cur -= 1
        elif s == '?':
            if bl:
                if one_seq == answer:
                    S[i] = '0'
                    zero_seq += 1
                    one_seq -= 1
                    one_seq = max(0, one_seq)                    
                elif zero_seq == answer:
                    S[i] = '1'
                    one_seq += 1
                    zero_seq -= 1
                    zero_seq = max(0, zero_seq)
                else:
                    one_seq -= 1
                    zero_seq -= 1
                    one_seq = max(0, one_seq)
                    zero_seq = max(0, zero_seq)  # ここでは保留                    
            else:
                one_seq -= 1
                zero_seq -= 1
                one_seq = max(0, one_seq)
                zero_seq = max(0, zero_seq)  # ここでは保留
        if one_seq > one_max:
            one_max = one_seq
        if zero_seq > zero_max:
            zero_max = zero_seq
    answer = max(zero_max, one_max)
    return S    

S = solve(S, False)
S = solve(S, True)
S = solve(S[::-1], True)
S = solve(S, True)
S = solve(S[::-1], True)

print(answer)
