#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll read(){
	ll a=0,b=getchar(),c=1;
	while(!isdigit(b))c=b=='-'?-1:1,b=getchar();
	while(isdigit(b))a=a*10+b-'0',b=getchar();
	return a*c;
}
int n,ans;
char s[1200005];
bool check(int t){
	int l=0,r=t,p=0;
	for(int i=1;i<=n;i++){
		if(s[i]=='0')l++,r++;
		if(s[i]=='1')l--,r--;
		if(s[i]=='?')l--,r++;
		if(!p){
			l=max(l,0);
			r=min(r,t);
		}
		else{
			if(l<0)l=1;
			if(r>t)r=t-1;
		}
		if(l>r)return 0;
		if(l==r)p=1;
	}
	return 1;
}
int main(){
	scanf("%s",s+1);
	n=strlen(s+1);
	int l=1,r=n;
	while(l<=r){
		int mid=(l+r)>>1;
		if(check(mid))
			ans=mid,r=mid-1;
		else l=mid+1;
	}
	printf("%d",ans);
	return 0;
}