#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char s[1000010];
int n,sufmx[1000010];
int work(int mx)
{
	int add=0,mi=0,cur=0;
	for(int i=1;i<=n;++i)
	{
		if(s[i]=='?'&&add+sufmx[i]+2<=mx)++cur,add+=2;
		else cur+=s[i]=='1'?1:-1;
		mi=min(mi,cur);
	}
	return mx-mi;
}
int main()
{
	scanf("%s",s+1);
	n=strlen(s+1);
	int cur=0;
	for(int i=1;i<=n;++i)
	{
		cur+=s[i]=='1'?1:-1;
		sufmx[i]=cur;
	}
	for(int i=n-1;i>=0;--i)sufmx[i]=max(sufmx[i+1],sufmx[i]);
	printf("%d",min(work(sufmx[0]),work(sufmx[0]+1)));
	return 0;
}