#include<bits/stdc++.h>
#define debug(x) cerr<<#x<<" = "<<x
#define sp <<"  "
#define el <<endl
#define fgx cerr<<" ---------------------------------------------- "<<endl
#define LL long long
#define uint unsigned int 
#define ULL unsigned long long
#define LDB long double
#define DB double
#define pii pair<int,int>
#define pli pair<LL,int>
#define mp make_pair
#define pb push_back
using namespace std;
inline LL read(){
	LL nm=0; bool fh=true; char cw=getchar();
	for(;!isdigit(cw);cw=getchar()) fh^=(cw=='-');
	for(;isdigit(cw);cw=getchar()) nm=nm*10+(cw-'0');
	return fh?nm:-nm;
}
#define mod 1000000007
namespace  CALC{
	inline int add(int x,int y){return (x+y>=mod)?(x+y-mod):(x+y);}
	inline int mns(int x,int y){return (x-y<0)?(x-y+mod):(x-y);}
	inline int mul(LL x,LL y){return x*y%mod;}
	inline void upd(int &x,int y){x=(x+y>=mod)?(x+y-mod):(x+y);}
	inline void dec(int &x,int y){x=(x-y<0)?(x-y+mod):(x-y);}
	inline int qpow(int x,int sq){int res=1;for(;sq;sq>>=1,x=mul(x,x))if(sq&1)res=mul(res,x);return res;}
}using namespace CALC;
#define M 3200000
int n,m,pre[M],mx,suf[M],t[M],ans;
char ch[M];
int main(){
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	scanf("%s",ch+1),n=strlen(ch+1),suf[n+1]=n+n+1,mx=0;
	for(int i=1;i<=n;i++) mx=max(mx,pre[i]=pre[i-1]+((ch[i]=='1')?1:-1));
	for(int i=n;i>0;i--) suf[i]=min(suf[i+1],mx-pre[i]);
	int dn=0;
	for(int i=1,now=0,pr=0;i<=n;i++){
		if(ch[i]!='?') t[i]=((ch[i]-'0')<<1)-1;
		else if(now+2<=suf[i]) now+=2,t[i]=1;
		else t[i]=-1;
		pr+=t[i],dn=min(dn,pr);
	} ans=mx-dn;
	mx++,dn=0;
	for(int i=n;i>0;i--) suf[i]=min(suf[i+1],mx-pre[i]);
	for(int i=1,now=0,pr=0;i<=n;i++){
		if(ch[i]!='?') t[i]=((ch[i]-'0')<<1)-1;
		else if(now+2<=suf[i]) now+=2,t[i]=1;
		else t[i]=-1;
		pr+=t[i],dn=min(dn,pr);
	} ans=min(ans,mx-dn);
	printf("%d\n",ans);
	return 0;
}