#include<bits/stdc++.h>
using namespace std;

typedef long long lint;
typedef long double louble;

#define so(a) ((int)((a).size()))

template<typename T1,typename T2> inline T1 max(T1 a,T2 b){return a<b?b:a;}
template<typename T1,typename T2> inline T1 min(T1 a,T2 b){return a<b?a:b;}

const char lf = '\n';

namespace ae86
{
	const int bufl = 1 << 15;

	char buf[bufl],*s=buf,*t=buf;

	inline int fetch()
	{
		if(s==t){t=(s=buf)+fread(buf,1,bufl,stdin);if(s==t)return EOF;}
		return *s++;
	}

	inline int ty()
	{
		int a=0;int b=1,c=fetch();
		while(!isdigit(c))b^=c=='-',c=fetch();
		while(isdigit(c))a=a*10+c-48,c=fetch();
		return b?a:-a;
	}
	
	template<typename T> inline int ts(T *s)
	{
		int a=0,c=fetch();
		while(c<=32 && c!=EOF)c=fetch();
		while(c>32 && c!=EOF)s[a++]=c,c=fetch();
		s[a]=0;
		return a;
	}
}
using ae86::ty;
using ae86::ts;

const int _ = 1000007;

int n,val[_],sval[_]={0},smx[_]={0};

int make(int lim)
{
	int now=0,mi=0;
	for(int i=1;i<=n;i++)
	{
		if(val[i]==0)now+=(now+2+(smx[i]-sval[i-1])<=lim)?1:-1;
		else now+=val[i];
		mi=min(mi,now);
	}
	return lim-mi;
}

int main()
{
	ios_base::sync_with_stdio(0),cout.tie(nullptr);

	n=ts(val+1);
	for(int i=1;i<=n;i++)
	{
		if(val[i]=='1')val[i]=1;
		else if(val[i]=='0')val[i]=-1;
		else val[i]=0;
		sval[i]=sval[i-1]+(val[i]==1?1:-1);
	}
	
	smx[n]=sval[n];
	for(int i=n-1;i>=1;i--)smx[i]=max(smx[i+1],sval[i]);

	int mxval=0;
	for(int i=1;i<=n;i++)mxval=max(mxval,sval[i]);
	cout<<min(make(mxval),make(mxval+1))<<lf;
	
	return 0;
}
