#include<bits/stdc++.h>
using namespace std;
char s[1111111];
int n,mx,pre[1111111],suf[1111111];
int f(int x)
{
	int num=0,mn=0;
	for (int i=1;i<=n;i++)
	{
		if (s[i-1]=='?') 
		{
			if (suf[i]+(num+1)*2<=x) num++;
		}
		mn=min(mn,num*2+pre[i]);
	}
	return mn;
}
int main()
{
	scanf("%s",s);
	n=strlen(s);mx=0;
	for (int i=1;i<=n;i++)
	{
		if (s[i-1]=='?' || s[i-1]=='0') pre[i]=pre[i-1]-1;
		else pre[i]=pre[i-1]+1;
		mx=max(mx,pre[i]);
	}
	suf[n+1]=-1e9;
	for (int i=n;i>=1;i--) suf[i]=max(suf[i+1],pre[i]);
	printf("%d\n",min(mx-f(mx),mx+1-f(mx+1)));
	return 0;
}