#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
char s[N];
int sum[N],mx[N],n;
int f(int M){
	for(int i=1,cnt=0;i<=n;++i){
		if(s[i]=='?'){
			if((cnt+1)*2+mx[i]<=M) ++cnt,sum[i]=1;
			else sum[i]=-1;
		}
		else sum[i]=(s[i]=='0'?-1:1);
		sum[i]+=sum[i-1];
	}
	return *min_element(sum,sum+n+1);
}
int main(){
	cin>>(s+1);n=strlen(s+1);
	for(int i=1;i<=n;++i){
		if(s[i]=='?') sum[i]=-1;
		if(s[i]=='1') sum[i]=1;
		if(s[i]=='0') sum[i]=-1;
		sum[i]+=sum[i-1];
	}
	for(int i=n;i>=0;--i)
		if(i+1<=n)mx[i]=max(sum[i],mx[i+1]);
		else mx[i]=sum[i];
	//for(int i=0;i<=n;++i)cout<<mx[i]<<" ";cout<<endl;
	int Z=mx[0];
	cout<<min(Z-f(Z),Z+1-f(Z+1));
	return 0;
}