#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#include <bits/stdc++.h>
using namespace std;
template<class t> inline t read(t &x){
	char c=getchar();bool f=0;x=0;
	while(!isdigit(c)) f|=c=='-',c=getchar();
	while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar();
	if(f) x=-x;return x;
}
template<class t,class ...A> inline void read(t &x,A &...a){
	read(x);read(a...);
}
template<class t> inline void write(t x){
	if(x<0) putchar('-'),write(-x);
	else{if(x>9) write(x/10);putchar('0'+x%10);}
}

const int N=1e6+5;
int sum[N],suf[N],n;
char s[N];

int calc(int mi){
	int ma=0,tot=0;
	for(int i=1;i<=n;i++){
		if(s[i]=='?'&&suf[i]-tot-2>=mi) tot+=2;
		ma=max(ma,sum[i]-tot);
	}
	return ma-mi;
}

signed main(){
	scanf("%s",s+1);
	n=strlen(s+1);
	for(int i=1;i<=n;i++) sum[i]=sum[i-1]+(s[i]!='0')-(s[i]=='0');
	suf[n]=sum[n];
	for(int i=n-1;~i;i--) suf[i]=min(suf[i+1],sum[i]);
	write(min(calc(suf[0]-1),calc(suf[0])));
}