#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define db double
#define pint pair<int,int>
#define mk(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define eb(x,y) emplace_back(x,y)
#define fi first
#define se second
#define Rep(x,y,z) for(int x=y;x<=z;x++)
#define Red(x,y,z) for(int x=y;x>=z;x--)
using namespace std;
const int MAXN=1e6+5;
char buf[1<<12],*pp1=buf,*pp2=buf,nc;int ny;
//inline char gc() {return pp1==pp2&&(pp2=(pp1=buf)+fread(buf,1,1<<12,stdin),pp1==pp2)?EOF:*pp1++;}
inline char gc(){return getchar();}
inline int read(){
	int x=0;for(ny=1;nc=gc(),(nc<48||nc>57)&&nc!=EOF;)if(nc==45)ny=-1;if(nc<0)return nc;
	for(x=nc-48;nc=gc(),47<nc&&nc<58&&nc!=EOF;x=(x<<3)+(x<<1)+(nc^48));return x*ny;
}
char s[MAXN];int n,a[MAXN],Min[MAXN];
inline int Solve(int lim){
	static int b[MAXN];int cnt=0;
	Rep(i,1,n){
		if(s[i]=='?'&&Min[i]-2*cnt-2>=lim)cnt++;
		b[i]=a[i]-2*cnt;
	}int Max=0;Rep(i,1,n)Max=max(Max,b[i]);
	return Max-lim;
}
int main(){
//	freopen("std.in","r",stdin);
//	freopen("std.out","w",stdout);
	scanf("%s",s+1),n=strlen(s+1);
	Rep(i,1,n)if(s[i]=='0')a[i]=a[i-1]-1;else a[i]=a[i-1]+1;
	Min[n+1]=n+5;Red(i,n,0)Min[i]=min(Min[i+1],a[i]);
	cout<<min(Solve(Min[0]),Solve(Min[0]-1));
	return 0;
}


