#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<ctime>
#include<map>
#include<bitset>
#include<set>
#include<assert.h>
#include<chrono>
#include<random>
#include<iostream>
#define LL long long
#define mp(x,y) make_pair(x,y)
#define pll pair<long long,long long>
#define pii pair<int,int>
#define SZ(x) ((int)x.size())
#define VI vector<int>
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
inline LL read()
{
	LL f=1,x=0;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 stack[20];
template<typename T>inline void write(T x)
{
	if(x<0){putchar('-');x=-x;}
    if(!x){putchar('0');return;}
    int top=0;
    while(x)stack[++top]=x%10,x/=10;
    while(top)putchar(stack[top--]+'0');
}
template<typename T>inline void pr1(T x){write(x);putchar(' ');}
template<typename T>inline void pr2(T x){write(x);putchar('\n');}
template<typename T>inline void chkmin(T &x,T y){x=x<y?x:y;}
template<typename T>inline void chkmax(T &x,T y){x=x>y?x:y;}
const int MAXN=1000005;
char ch[MAXN];int len;
int val[MAXN],rmx[MAXN];
int calculate(int lim)
{
	for(int i=1;i<=len;i++)
	{
		if(ch[i]=='1')val[i]=1;else val[i]=-1;
		val[i]+=val[i-1];
	}rmx[len+1]=-1e9;
	for(int i=len;i>=1;i--)rmx[i]=val[i],chkmax(rmx[i],rmx[i+1]);
	int ret=0;
	for(int i=1,sum=0,S=0;i<=len;i++)
	{
		if(ch[i]=='1')++S;
		else if(ch[i]=='0')--S;
		else
		{
			if(rmx[i]+sum+2<=lim)sum+=2,++S;
			else --S;
		}chkmin(ret,S);
	}return ret;
}
int main()
{
	#ifdef Rose
		double BeginJudgeTime=clock();
	#endif
	scanf("%s",ch+1);len=strlen(ch+1);
	int mxp=0;
	for(int i=1,S=0;i<=len;i++)
	{
		if(ch[i]=='1')++S;
		else --S;chkmax(mxp,S);
	}
	int z1=calculate(mxp),z2=calculate(mxp+1);
	pr2(min(mxp-z1,mxp+1-z2));
	#ifdef Rose
		double EndJudgeTime=clock();
		cerr<<"JudgeTime is"<<" ";
		cerr<<(EndJudgeTime-BeginJudgeTime)/CLOCKS_PER_SEC<<endl;
	#endif
	return 0;
}
