#include <bits/stdc++.h>
#define clr(x) memset(x,0,sizeof (x))
#define For(i,a,b) for (int i=(a);i<=(b);i++)
#define Fod(i,b,a) for (int i=(b);i>=(a);i--)
#define fi first
#define se second
#define kill _z_kill
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define outval(x) cerr<<#x" = "<<x<<endl
#define outv(x) cerr<<#x" = "<<x<<"  "
#define outtag(x) cerr<<"--------------"#x"---------------"<<endl
#define outarr(a,L,R) cerr<<#a"["<<L<<".."<<R<<"] = ";\
	For(_x,L,R) cerr<<a[_x]<<" ";cerr<<endl;
#define User_Time ((double)clock()/CLOCKS_PER_SEC)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned uint;
typedef long double LD;
typedef vector <int> vi;
typedef pair <int,int> pii;
LL read(){
	LL x=0,f=0;
	char ch=getchar();
	while (!isdigit(ch))
		f=ch=='-',ch=getchar();
	while (isdigit(ch))
		x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return f?-x:x;
}
const int mod=1e9+7;
int Pow(int x,int y){
	int ans=1;
	for (;y;y>>=1,x=(LL)x*x%mod)
		if (y&1)
			ans=(LL)ans*x%mod;
	return ans;
}
void Add(int &x,int y){
	if ((x+=y)>=mod)
		x-=mod;
}
void Del(int &x,int y){
	if ((x-=y)<0)
		x+=mod;
}
int Add(int x){
	return x>=mod?x-mod:x;
}
int Del(int x){
	return x<0?x+mod:x;
}
void ckmax(int &x,int y){
	if (x<y)
		x=y;
}
void ckmin(int &x,int y){
	if (x>y)
		x=y;
}
const int N=1000005*4;
int n;
char s[N];
int pre[N],suf[N];
int gao(int dan4){
	int mi=0,val=0;
	For(i,1,n){
		if (s[i]=='0')
			val--;
		else if (s[i]=='1')
			val++;
		else {
			if (val+1+suf[i]-pre[i]<=dan4)
				val++;
			else
				val--;
		}
		mi=min(mi,val);
	}
	return dan4-mi;
}
int main(){
	scanf("%s",s+1);
	n=strlen(s+1);
	For(i,1,n)
		pre[i]=pre[i-1]+(s[i]=='1'?1:-1);
	suf[n]=pre[n];
	Fod(i,n-1,0)
		suf[i]=max(suf[i+1],pre[i]);
//	outarr(pre,0,n);
//	outarr(suf,0,n);
	cout<<min(gao(suf[0]),gao(suf[0]+1))<<endl;
	return 0;
}
