#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 1000010
inline int read(){
	int x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-')f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		x=(x<<1)+(x<<3)+c-'0';
		c=getchar();
	}
	return x*f;
}
int mx,n,a[N],b[N];
char s[N];
void init(){
	int sum=0;
	for(int i=1;i<=n;++i){
		sum+=(s[i]=='1'?1:-1);
		mx=max(mx,sum);
		a[i]=sum;
	}
	b[n]=a[n];
	for(int i=n-1;i>=0;--i){
		b[i]=max(a[i],b[i+1]);
	}
}
int calc(int lim){
	int mn=0,sum=0;
	for(int i=1;i<=n;++i){
		if(s[i]=='1')++sum;
		else if(s[i]=='0')--sum;
		else{
			if(b[i]-a[i-1]+sum+2<=lim)++sum;
			else --sum;
		}
		mn=min(mn,sum);
	}
	return mn;
}
int main(){
	scanf("%s",s+1);
	n=strlen(s+1);
	init();
	printf("%d\n",min(mx-calc(mx),mx+1-calc(mx+1)));
	return 0;
}

