#include <stdio.h>

char S[1001001];

int main()
{
	scanf ("%s", S);

	int l = 0, r = 0;
	while (S[r]) r++;
	while (l + 1 < r){
		int m = (l + r) / 2;

		int p = 0, q = m, d = 1; bool g = 1;
		for (int i = 0; S[i]; i++){
			if (S[i] == '0') p++, q++;
			if (S[i] == '1') p--, q--;
			if (S[i] == '?') p--, q++;
			if (p < 0){
				p = 0;
				if (d == 2 && p % 2 != q % 2) p++;
			}
			if (q > m){
				q = m;
				if (d == 2 && p % 2 != q % 2) q--;
			}
			if (p > q){
				g = 0; break;
			}
			if (p == q) d = 2;
		}

		if (g) r = m;
		else l = m;
	}
	printf ("%d\n", r);

	return 0;
}