#include<bits/stdc++.h>
using namespace std;
const int N = 2e6+5;
int n;
char s[N];
int g[N];
 
int v[N], h[N];
 
int main()
{
	scanf("%s",s+1);n=strlen(s+1);
	int mx=0,ss=0;
	for(int i=1;i<=n;i++){
		if(s[i]=='1')ss++;
		else ss--;
		v[i]=h[i]=ss;
		mx=max(mx,ss);
	}
	int mn=0;
	for(int i=n-1;i;i--)v[i]=max(v[i],v[i+1]);
	// cerr << mx << "?" << endl;
	ss=0;
	for(int i=1;i<=n;i++){
		if(s[i]=='1')ss++;
		else if(s[i]=='0')ss--;
		else if(s[i]=='?'){
			// cerr << ss+1+g[i+1] << " " << mx << endl;
			if(ss+v[i]+2-h[i-1]<=mx)ss++;
			else ss--;
		}
		// assert(ss<=mx);
		// cerr << ss << ",";
		// cerr << g[i+1] << "?" << endl;
		mn=min(mn,ss);
	}
	int ans=mx-mn;
	mn=ss=0;
	++mx;
	for(int i=1;i<=n;i++){
		if(s[i]=='1')ss++;
		else if(s[i]=='0')ss--;
		else if(s[i]=='?'){
			// cerr << ss+1+g[i+1] << " " << mx << endl;
			if(ss+v[i]+2-h[i-1]<=mx)ss++;
			else ss--;
		}
		// assert(ss<=mx);
		// cerr << ss << ",";
		// cerr << g[i+1] << "?" << endl;
		mn=min(mn,ss);
	}
	ans=min(ans,mx-mn);
	printf("%d\n",ans);
}
