#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1000005;
int n,a[maxn],b[maxn],ans=1e9;
char s[maxn];
inline void work(int x){
	int cnt=0,now=n;
	for (int i=0;i<=n;i++){
		if(s[i]=='?'&&b[i]+cnt+2<=x) cnt+=2;
		now=min(now,cnt+a[i]);
	}
	ans=min(ans,x-now);
}
int main(){
	scanf("%s",s+1);n=strlen(s+1);
	for (int i=1;i<=n;i++) b[i]=a[i]=a[i-1]+(s[i]=='1'?1:-1);
	for (int i=n;i;i--) b[i-1]=max(b[i-1],b[i]);
	work(b[0]),work(b[0]+1);
	printf("%d\n",ans);
	return 0;
}