def examA():
    T = I()
    ans = [0]*T
    for t in range(T):
        N = I()
        A = LI()
        S = SI()
        if S[-1]=="1":
            ans[t] = 1
            continue


    print(ans)
    return

def examB():
    S = SI()
    N = len(S)
    sub_ = defaultdict(int)
    cur = 0
    keep = 0
    for i,s in enumerate(S):
        if s=="?":
            if cur==0:
                keep = i
            cur += 1
        else:
            if cur>0:
                sub_[keep] = cur%2
            cur = 0
    flag = True
    for i,s in enumerate(S):
        if i%2==0:
            if s=="1":
                flag = False
                break
        else:
            if s=="0":
                flag = False
                break
    if flag:
        print(1)
        return
    flag = True
    for i,s in enumerate(S):
        if i%2==1:
            if s=="1":
                flag = False
                break
        else:
            if s=="0":
                flag = False
                break
    if flag:
        print(1)
        return
    ans = 2
    cur = 0
    cur0 = [0]*N
    for i,s in enumerate(S):
        if s=="0":
            cur += 1
        else:
            cur -= 1
            if cur<0:
                cur = 0
        if ans<cur:
            ans = cur
        cur0[i] = cur
    cur = 0
    cur1 = [0]*N
    for i,s in enumerate(S):
        if s=="1":
            cur += 1
        else:
            cur -= 1
            if cur<0:
                cur = 0
        if ans<cur:
            ans = cur
        cur1[i] = cur

    max0 = set()
    for i,s in enumerate(S[1:],1):
        if s == "?" and cur0[i-1] == ans:
            max0.add(i)

    max1 = set()
    for i,s in enumerate(S[1:],1):
        if s == "?" and cur1[i-1] == ans:
            max1.add(i)
    #print(cur0)
    #print(cur1)
    #print(max1)

    cur = 0
    ne = 0
    for i,s in enumerate(S):
        #print(cur)
        if s=="0":
            cur += 1
            if ne>0:
                cur += 1
                ne = 0
        else:
            if s=="?" and i in max1:
                #print(i,s,sub_)
                cur += 1
                if sub_[i]==1:
                    ne = 1
            else:
                cur -= 1
            if cur<0:
                cur = 0
        if ans<cur:
            ans = cur
            print(ans)
            return
    ne = 0
    cur = 0
    for i,s in enumerate(S):
        if s=="1":
            cur += 1
            if ne>0:
                cur += 1
                ne = 0
        else:
            if s=="?" and i in max0:
                cur += 1
                if sub_[i]==1:
                    ne = 1
            else:
                cur -= 1
            if cur<0:
                cur = 0
        if ans<cur:
            ans = cur
            print(ans)
            return
    print(ans)
    return

def examC():
    N, A, B = LI()

    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return


from decimal import Decimal as dec
import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(input())
def LI(): return list(map(int,sys.stdin.readline().split()))
def DI(): return dec(input())
def LDI(): return list(map(dec,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examB()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn

111??000
111?000
11??11
11???00
"""