#include<bits/stdc++.h>
using namespace std;
#define cs const
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
#define bg begin

namespace IO{

cs int RLEN=1<<22|1;
char ibuf[RLEN],*ib,*ob;
inline char gc(){
	(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
	return (ib==ob)?EOF:*ib++;
}
inline int read(){
	char ch=gc();
	int res=0;bool f=0;
	while(!isdigit(ch))f=ch=='-',ch=gc();
	while(isdigit(ch))res=(res*10)+(ch^48),ch=gc();
	return f?-res:res;
}
inline ll readll(){
	char ch=gc();
	ll res=0;bool f=0;
	while(!isdigit(ch))f=ch=='-',ch=gc();
	while(isdigit(ch))res=(res*10)+(ch^48),ch=gc();
	return f?-res:res;
}
inline int readstring(char *s){
	int top=0;char ch=gc();
	while(isspace(ch))ch=gc();
	while(!isspace(ch)&&ch!=EOF)s[++top]=ch,ch=gc();
	s[top+1]='\0';return top;
}

}
using IO::read;
using IO::readll;
using IO::readstring;

template<typename tp>inline void chemx(tp &a,tp b){(a<b)?(a=b):0;}
template<typename tp>inline void chemn(tp &a,tp b){(a>b)?(a=b):0;}

cs int N=1000005;
int n,lmn,a[N];
char str[N];
int main(){
	#ifdef Stargazer
	freopen("lx.in","r",stdin);
	#endif
	n=readstring(str);
	for(int i=1,s=0;i<=n;i++){
		s+=(str[i]=='1')?1:-1;
		chemx(lmn,s),a[i]=s;
	}int res=1e9;
 	for(int i=n-1;i>=1;i--)chemx(a[i],a[i+1]);               
	for(int t=0;t<2;t++){
		lmn+=t;int f=0;
		for(int i=1,s=0,del=0;i<=n;i++){
			if(str[i]=='?'&&a[i]+s+2<=lmn)s+=2;
			del+=(str[i]=='1')?1:-1;
			chemn(f,del+s);
		}
		chemn(res,lmn-f);
	}cout<<res<<'\n';return 0;
}