#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define debug(...) fprintf(stderr,__VA_ARGS__)
using namespace std;
template<class T> inline bool Cmax(T &x,T y) {return x<y?x=y,1:0;}
const int maxn=1e6+50;
int n;
int sum[maxn],mx[maxn];
bool mark[maxn];
char S[maxn];
int f(int M) {
	static int s[maxn]; s[0]=0;
	for(int i=1,cnt=0;i<=n;++i) {
		if(mark[i]) {
			if(mx[i]+2*(cnt+1)<=M) s[i]=1,++cnt;
			else s[i]=-1;
		}
		else s[i]=S[i]=='0'?-1:1;
	}
	for(int i=1;i<=n;++i) s[i]+=s[i-1];
	return *min_element(s,s+n+1);
}
int main() {
	scanf("%s",S+1),n=strlen(S+1);
	for(int i=1;i<=n;++i) {
		if(S[i]=='?') mark[i]=1;
		if(S[i]=='1') sum[i]=sum[i-1]+1;
		else sum[i]=sum[i-1]-1;
	}
	for(int i=n;i>=0;--i) {
		mx[i]=sum[i];
		if(i!=n) Cmax(mx[i],mx[i+1]);
	}
	int Z=mx[0];
	printf("%d\n",min(Z-f(Z),(Z+1)-f(Z+1)));
	return 0;
}