#include<bits/stdc++.h>
using namespace std;
string s;
int n,mi,p[1000010];
int tr(int x){
	int mi=0;
	for (int i=0,j=0;i<n;i++){if (s[i]=='1') j++;else j--;p[i]=(x-j)/2;}
	for (int i=n-2;i>=0;i--)p[i]=min(p[i+1],p[i]);
	for (int i=0,j=0,k=0;i<n;i++){
		if (s[i]=='1')j++;if (s[i]=='0')j--;
		if (s[i]=='?'){if (k<p[i])k++,j++;else j--;}
		mi=min(mi,j);
	}
	return x-mi;
}
int main(){
	cin>>s;n=s.length();mi=0;
	for (int i=0,j=0;i<n;i++){if (s[i]=='1')j++;else j--;mi=max(mi,j);}
	cout<<min(tr(mi),tr(mi+1))<<endl;
	return 0;
}