#include<bits/stdc++.h>
#define re register
#define inc(i,j,k) for(re int i=j;i<=k;i++)
#define dec(i,j,k) for(re int i=j;i>=k;i--)
using namespace std;
const int N=1e6+5;
int n,sum[N],ans,minn,maxx,cur,pos,suf[N],now;
char s[N];
void get(int x){
	now=minn=maxx=0;
	inc(i,1,n){
		if(s[i]=='1') sum[i]=sum[i-1]+1;
		if(s[i]=='0') sum[i]=sum[i-1]-1;
		if(s[i]=='?'){
			if(suf[i]+now+2<=x) sum[i]=sum[i-1]+1,now+=2;
			else sum[i]=sum[i-1]-1;
		} 
		minn=min(minn,sum[i]);
		maxx=max(maxx,sum[i]);
	}
	ans=min(ans,maxx-minn);
}
int main(){
	scanf("%s",s+1);
	n=strlen(s+1);
	minn=maxx=0;
	inc(i,1,n){
		if(s[i]=='1') sum[i]=sum[i-1]+1;
		else sum[i]=sum[i-1]-1;
		maxx=max(maxx,sum[i]);
	}
	suf[n+1]=-n;
	dec(i,n,1) suf[i]=max(suf[i+1],sum[i]);
	int z=maxx;ans=n; 
	get(z),get(z+1);
	printf("%d",ans);
}