#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define uint unsigned
#define pii pair<int,int>
#define pll pair<ll,ll>
#define IT iterator
#define PB push_back
#define fi first
#define se second
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define CLR(a,v) memset(a,v,sizeof(a));
#define CPY(a,b) memcpy(a,b,sizeof(a));
#define debug puts("wzpakking")
#define y1 ysghysgsygsh
using namespace std;
const int N=1000005;
char s[N];
int n;
bool check(int v){
	int l0=0,r0=v,l1=1,r1=v;
	v&1?r0--:r1--;
	For(i,1,n){
		int nl0=1e9,nr0=-1e9;
		int nl1=1e9,nr1=-1e9;
		if (s[i]!='1'){
			if (l1<=r1){
				nl0=min(nl0,l1-1);
				nr0=max(nr0,r1-1);
			}
			if (l0<=r0){
				nl1=min(nl1,l0-1);
				nr1=max(nr1,r0-1);
			}
		}
		if (s[i]!='0'){
			if (l1<=r1){
				nl0=min(nl0,l1+1);
				nr0=max(nr0,r1+1);
			}
			if (l0<=r0){
				nl1=min(nl1,l0+1);
				nr1=max(nr1,r0+1);
			}
		}
		if (nl0<0) nl0+=2;
		if (nr0>v) nr0-=2;
		if (nl1<0) nl1+=2;
		if (nr1>v) nr1-=2;
		//cout<<l0<<' '<<l1<<' '<<r0<<' '<<r1<<endl;
		l0=nl0; l1=nl1; r0=nr0; r1=nr1;
		//cout<<l0<<' '<<l1<<' '<<r0<<' '<<r1<<endl;
	}
	return l0<=r0||l1<=r1;
}
int main(){
	scanf("%s",s+1);
	n=strlen(s+1);
	int l=1,r=n,ans=-1;
	while (l<=r){
		int mid=(l+r)/2;
		if (check(mid))
			ans=mid,r=mid-1;
		else l=mid+1;
	}
	cout<<ans;
	
}
/*
>_<
*/ 