#include<bits/stdc++.h>

#define y1 dmytxdy
#define pb push_back
#define fi first
#define se second
#define mp make_pair

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;

template <typename T> bool chkmin(T &x,T y){return x>y?x=y,1:0;}
template <typename T> bool chkmax(T &x,T y){return x<y?x=y,1:0;}

int readint(){
	int x=0,f=1; char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}

int n;
int a[1000005];
char s[1000005];

int getans(int x){
	int tag=0,ret=0,now=0;
	for(int i=1;i<=n;i++){
		if(s[i]=='?'){
			if(tag+a[i]+2<=x) tag+=2,now++;
			else now--;
		}
		else now+=s[i]=='1'?1:-1;
		chkmin(ret,now);
	}
	return ret;
}

int main(){
	scanf("%s",s+1);
	n=strlen(s+1);
	for(int i=1;i<=n;i++) a[i]=a[i-1]+(s[i]=='1'?1:-1);
	for(int i=n-1;i>=0;i--) chkmax(a[i],a[i+1]);
	printf("%d\n",min(a[0]-getans(a[0]),a[0]+1-getans(a[0]+1)));
	return 0;
}