#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

int n, M, a[1000010];
char s[1000010];

inline int Solve (int x) {
	int res = 0;
	for (int i = 1, t = 1, su = 0; i <= n; ++i) {
		if (s[i] == '?')
			if (a[i] + t * 2 <= x) ++t, ++su;
			else --su;
		else su += s[i] == '1' ? 1 : -1;
		res = min (res, su);
	}
	return res;
}

int main () {
	scanf ("%s", s + 1);
	n = strlen (s + 1);
	for (int i = 1; i <= n; ++i) {
		a[i] = a[i - 1] + (s[i] == '1' ? 1 : -1);
		M = max (M, a[i]);
	}
	for (int i = n - 1; i >= 1; --i) a[i] = max (a[i], a[i + 1]);
	printf ("%d\n", min (M - Solve (M), M + 1 - Solve (M + 1)));
	return 0;
}