#include <bits/stdc++.h>
const int MAX_N = int(1e6) + 10;

int n, ans, sum[MAX_N], suf[MAX_N], b[MAX_N];
char s[MAX_N];

void solve(int low) {
	for (int i = 0; i <= n; ++i) b[i] = sum[i];
	int del = 0;
	for (int i = 1; i <= n; ++i) {
		if (s[i] == '?' && suf[i] - del - 2 >= low) del += 2;
		b[i] -= del;
	}
	int m = 0;
	for (int i = 1; i <= n; ++i) m = std::max(m, b[i]);
	ans = std::min(ans, m - low);
}

int main() {
	scanf("%s", s + 1);
	n = strlen(s + 1);
	for (int i = 1; i <= n; ++i)
		sum[i] = sum[i - 1] + (s[i] == '0' ? -1 : 1);
	suf[n + 1] = ans = n + 1;
	for (int i = n; i >= 0; --i)
		suf[i] = std::min(suf[i + 1], sum[i]);
	solve(suf[0]);
	solve(suf[0] - 1);
	printf("%d\n", ans);
}