import os
from collections import defaultdict

import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


S = sys.stdin.buffer.readline().decode().rstrip()
N = len(S)
# dp[now]: max,min
dp = defaultdict(set)
dp[0] = {(0, 0)}
for i, c in enumerate(S):
    ndp = defaultdict(set)
    if c != '0':
        for k, li in dp.items():
            for ma, mi in li:
                ndp[k + 1].add((max(ma, k + 1), mi))
    if c != '1':
        for k, li in dp.items():
            for ma, mi in li:
                ndp[k - 1].add((ma, min(mi, k - 1)))
    dp = ndp
ans = INF
for li in dp.values():
    for a, b in li:
        ans = min(ans, abs(a - b))
print(ans)

# # 後ろに見てったとき、max を i にするときの最大の min
# max_mins = [-np.inf] * (N + 10)
# max_mins[0] = 0
# max_mins = np.array(max_mins, dtype=float)
# for c in reversed(S):
#     if c == '0':
#         # -1にする
#         zero = max_mins[0]
#         max_mins[:-1] = max_mins[1:] - 1
#         max_mins[0] = max(max_mins[0], zero - 1)
#     elif c == '1':
#         # 1にする
#         max_mins[1:] = np.minimum(0, max_mins[:-1] + 1)
#         max_mins[0] = -np.inf
#     else:
#         # 1にする
#         tmp = np.array(max_mins)
#         zero = tmp[0]
#         max_mins[:-1] = tmp[1:] - 1
#         max_mins[0] = max(max_mins[0], zero - 1)
#         max_mins[1:] = np.maximum(max_mins[1:], np.minimum(0, tmp[:-1] + 1))
#     print(max_mins)


# # 後ろに見てったとき、max を i にするときの最大の min
# imos = deque([0] * (N + 10))
# max_mins = deque([-INF] * (N + 10))
# max_mins[0] = 0
# for c in reversed(S):
#     max_mins[0] += imos[0]
#     imos[1] += imos[0]
#     imos[0] = 0
#     if c == '0':
#         # -1にする
#         zero = max_mins[0]
#         max_mins.popleft()
#         imos.popleft()
#         imos[1] -= 1
#         max_mins[0] -= 1
#         max_mins[0] = max(max_mins[0], zero - 1)
#     elif c == '1':
#         # 1にする
#         imos[1] += 1
#         max_mins[1:] = np.minimum(0, max_mins[:-1] + 1)
#         max_mins[0] = -np.inf
#         max_mins.appendleft(-INF)
#         imos.appendleft(0)
#     else:
#         # 1にする
#         tmp = np.array(max_mins)
#         zero = tmp[0]
#         max_mins[:-1] = tmp[1:] - 1
#         max_mins[0] = max(max_mins[0], zero - 1)
#         max_mins[1:] = np.maximum(max_mins[1:], np.minimum(0, tmp[:-1] + 1))
#     print(max_mins)
