#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;

template<class T> void read(T &x){
	int f=0; x=0; char ch=getchar();
	for(;!isdigit(ch);ch=getchar()) f|=(ch=='-');
	for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
	if(f) x=-x;
}

const int N=1000005;
int f[N],n,mn,sum,mx;
char s[N];

int solve(){
	n=strlen(s+1);
	per(i,n,1){
		f[i]=f[i+1];
		if(s[i]=='0') f[i]--;
		else f[i]++;
		f[i]=min(f[i],0);
	}
	mn=f[1];
	sum=mx=0;
	rep(i,1,n){
		if(s[i]=='0') sum--;
		else if(s[i]=='1') sum++;
		else{
			if(sum-1+f[i+1]<mn) sum++;
			else sum--;
		}
		mx=max(mx,sum);
	}
	return mx-mn;
}

int main(){
	scanf("%s",s+1);
	int n=strlen(s+1);
	int ans=solve();
	reverse(s+1,s+n+1);
	ans=min(ans,solve());
	rep(i,1,n){
		if(s[i]=='1') s[i]='0';
		else if(s[i]=='0') s[i]='1';
	}
	ans=min(ans,solve());
	reverse(s+1,s+n+1);
	ans=min(ans,solve());
	cout<<ans<<endl;
	return 0;
}
