#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<algorithm>

using namespace std;

#define LL long long
#define fgx cerr<<"--------------"<<endl;
#define dgx cerr<<"=============="<<endl;

inline int read(){
	int x=0,f=1;char c=getchar();
	for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
	for(;isdigit(c);c=getchar()) x=x*10+c-'0';
	return x*f;
}
const int MAXN = 1000010;
const int INF = 2147483600;

int N; char s[MAXN+1]; int g[MAXN+1];

inline int fn(int x){
	int mn=0,now=0;
	for(int i=1;i<=N;i++){
		if(s[i]=='1') ++now;
		else if(s[i]=='0') --now;
		else{
			if(now+1+max(g[i+1],0)<=x) ++now;
			else --now;
		}
		mn=min(mn,now);
	} return x-mn;
}

int main(){
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	scanf("%s",s+1); N=strlen(s+1); g[N+1]=0;
	for(int i=N;i>=1;i--){
		int val=(s[i]!='1'?-1:1);
		g[i]=max(val,g[i+1]+val);
	} int mx=0,now=0;
	for(int i=1;i<=N;i++){ if(s[i]!='1') --now; else ++now; mx=max(mx,now);  }
	printf("%d\n",min(fn(mx),fn(mx+1)));
	return 0;
}


