#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
constexpr int MX = 1000005;
char s[MX];
int ac[MX], am[MX], t, n, z, mi[2], aa[2];
int main(){
	scanf("%s", s);
	n = strlen(s);
	rep(i,n) z = max(z, ac[i] = t += s[i] == '1' ? 1 : -1);
	am[n-1] = ac[n-1];
	for(int i=n-2; i>=0; i--) am[i] = max(am[i+1], ac[i]);
	rep(i,n) rep(j,2){
		if(s[i] == '?' && am[i] + aa[j] + 2 <= z + j) aa[j] += 2;
		if(mi[j] > ac[i] + aa[j]) mi[j] = ac[i] + aa[j];
	}
	printf("%d\n", min(z-mi[0],z+1-mi[1]));
}