import sys
import heapq
import math
import fractions
import bisect
import itertools
from collections import Counter
from collections import deque
from operator import itemgetter
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

s=input()
n=len(s)
if n==1:
    print(1)
    exit()
x=[0]*n
y=[0]*n
a=0
b=0
mina=0
maxa=0
ra=0
minb=0
maxb=0
rb=0

for i in range(n):
    if s[i]=="0":
        a-=1
        b-=1
    elif s[i]=="1":
        a+=1
        b+=1
    else:
        a-=1
        b+=1
    x[i],y[i]=a,b
    if a>maxa:
        maxa=a
    if a<mina:
        mina=a
        maxa=a
    ra=max(ra,maxa-mina)
    if b>maxb:
        maxb=b
        minb=b
    if b<minb:
        minb=b
    rb=min(rb,maxb-minb)

c=0
ans=1
for i in range(n):
    if s[i]=="?":
        c+=1
    elif c>0 and i-c-1>=0:
        if c%2==1 and s[i-c-1]!=s[i]:
            ans=2
        if c%2==0 and s[i-c-1]==s[i]:
            ans=2
print(max(ra,abs(rb),ans))