import sys
import numpy as np
from numba import njit

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

@njit('(i8[::1],i8[::1],i8[::1])', cache=True)
def f(S, T1, T2):
    m, x, M = 0, 0, 0
    for i in range(len(S)):
        if S[i] == -1:
            x -= 1
        elif S[i] == 1:
            x += 1
        else:
            if x - 1 + T1[i + 1] >= m:
                x -= 1
            elif x + 1 + T2[i + 1] > M:
                x -= 1
            else:
                x += 1
        m = min(m, x)
        M = max(M, x)
    return M - m

S = np.array(list(read().rstrip().decode()), 'U1')

Si = np.zeros(len(S), np.int64)
Si[S == '0'] = -1
Si[S == '1'] = 1
S = Si

def get_min_after(S):
    T = S.copy()
    T[S == 0] = 1
    T = np.cumsum(np.concatenate([[0], T]))
    T = np.minimum.accumulate(T[::-1])[::-1] - T
    return T


T1 = get_min_after(S)
T2 = -get_min_after(-S)
print(f(S, T1, T2))