#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define N 1000012
int n,Z,a[N],s[N];char sc[N];
inline int calc(int m)
{
	int i,sum=0,mi=0;
	for(i=1;i<=n;i++){if(sc[i]=='?'){if(max(sum+1,sum+1+s[i+1])<=m)++sum;else --sum;}else sum+=a[i];mi=min(mi,sum);}return mi;
}
int main(){
	scanf("%s",sc+1);n=strlen(sc+1);Z=0;int i,sum=0;
	for(i=1;i<=n;i++){a[i]=(sc[i]=='1')?1:(-1);sum+=a[i];Z=max(Z,sum);}
	s[n+1]=0;for(i=n;i>=1;i--)s[i]=max(s[i+1]+a[i],a[i]);printf("%d",min(Z-calc(Z),Z+1-calc(Z+1)));return 0;
}