#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
using namespace std;
const int N=1000005;
int n;char s[N];
bool chk(int mid){
	rep(_,0,1){
		int k1=_,k2=mid;
		if((k2&1)!=(k1&1))--k2;
		rep(i,1,n){
			if(s[i]=='0')--k1,--k2;else
			if(s[i]=='1')++k1,++k2;else
				--k1,++k2;
			if(k1<0)k1+=2;
			if(k2>mid)k2-=2;
			if(k1>k2)goto gg;
		}
		return 1;
		gg:;
	}
	return 0;
}
int main(){
	scanf("%s",s+1),n=strlen(s+1);
	if(n==1){puts("1");return 0;}
	int l=0,r=1e6,ans=1e6;
	while(l<=r){
		int mid=(l+r)>>1;
		if(chk(mid))ans=mid,r=mid-1;
		else l=mid+1;
	}
	printf("%d\n",ans);
	return 0;
}