#include<stdio.h>
#include<string.h>
#define it register int
#define ct const int
#define il inline
using namespace std;
typedef long long ll;
#define rll register ll
#define cll const ll
const int N=1000005;
int n,T,s[N],mx[N];
char S[N]; 
il int Min(ct p,ct q){return p<q?p:q;}
int main(){
	scanf("%s",S+1),n=strlen(S+1);it i;
	for(i=1;i<=n;++i) s[i]=s[i-1]+(S[i]=='1'?1:-1);
	auto cal = [&] (ct x){
		for(it i=1,p=0;i<=n;++i){
			if(S[i]!='?') s[i]=s[i-1]+(S[i]=='1'?1:-1);
			if(S[i]=='?') mx[i]+(p+1<<1)<=x?s[i]=s[i-1]+1,++p:s[i]=s[i-1]-1;
		}
		it mn=s[0];
		for(it i=1;i<=n;++i) mn=(s[i]<mn?s[i]:mn);
		return mn;
	};
	for(i=n-1,mx[n]=s[n];~i;--i) mx[i]=(mx[i+1]>s[i]?mx[i+1]:s[i]);
	printf("%d\n",Min(mx[0]-cal(mx[0]),mx[0]+1-cal(mx[0]+1)));
	return 0;
}
