#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define all(x) x.begin(),x.end()
#define cmin(a,b) (a>b?a=b:a)
#define cmax(a,b) (a<b?a=b:a)
#define mem(a,k) memset(a,k,sizeof(a))
#define lop(i,s,t) for(int i=s;i<(t);++i)
#define rep(i,s,t) for(int i=s;i<=(t);++i)
#define dec(i,s,t) for(int i=s;i>=(t);--i)
#define fore(i,v) for(int i=g[v],d=es[i].d;i;i=es[i].nxt,d=es[i].d)
using namespace std;

#define Pr(f,...) fprintf(stderr,f,##__VA_ARGS__),fflush(stderr)

typedef long long ll;

template<typename T>
void read(T &x){
	x=0; 
	char c;
	for(c=getchar();!isdigit(c);c=getchar()); 
	for(;isdigit(c);c=getchar())x=x*10+c-'0'; 
}

const int N=1e6+50,P=1e9+7,oo=P;

int mul(int a,int b){ return 1ll*a*b%P; }
//int add(int a,int b){ a+=b; return a>=P?a-P:a; }
int sub(int a,int b){ a-=b; return a<0?a+P:a; }
int gcd(int a,int b){ return !b?a:gcd(b,a%b); }

int z,n,sum[N],mx[N],ans;
char s[N];

int main(int argc,char *argv[]){
#ifdef CURIOUSCAT
	freopen("d.in","r",stdin); 
#endif
	scanf("%s",s+1);
	z=0,n=strlen(s+1);
	rep(i,1,n){
		sum[i]=sum[i-1]+(s[i]=='1'?1:-1);
		cmax(z,sum[i]);
	}
	mx[n+1]=-oo;
	dec(i,n,1){
		mx[i]=max(mx[i+1],sum[i]);
	}
	ans=oo;
	lop(k,0,2){
		int w=0,mn=0;
		rep(i,1,n){
			if(s[i]=='?'&&w+mx[i]+2<=z+k){
				w+=2;
			}
			cmin(mn,w+sum[i]);
		}
		cmin(ans,z+k-mn);
	}
	cout<<ans<<endl;
	return 0;
}
