#include <cstdio>
#include <cstring>
#include <algorithm>

const int MN = 1000005;

int N;
char S[MN];

int p[MN];
inline int Solve(int lb) {
	p[N] = 0;
	for (int i = N; i >= 1; --i)
		p[i - 1] = std::max(p[i] + (S[i] == '1' ? 1 : -1), 0);
	int maxval = 0;
	for (int i = 1, now = 0; i <= N; maxval = std::max(maxval, now), ++i)
		if (S[i] == '?') now += now - 1 - p[i] >= lb ? -1 : 1;
		else now += S[i] == '0' ? 1 : -1;
	return maxval;
}

int main() {
	scanf("%s", S + 1), N = strlen(S + 1);
	int minval = 0;
	for (int i = 1, now = 0; i <= N; ++i)
		if (S[i] == '1') minval = std::min(minval, --now);
		else ++now;
	printf("%d\n", std::min(Solve(minval) - minval, Solve(minval - 1) - minval + 1));
	return 0;
}