#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define SZ(x) ((int)x.size())
#define ALL(x) x.begin(),x.end()
#define L(i,u) for (register int i=head[u]; i; i=nxt[i])
#define rep(i,a,b) for (register int i=(a); i<=(b); i++)
#define per(i,a,b) for (register int i=(a); i>=(b); i--)
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
typedef vector<int> Vi;
template<class T> inline void read(T &x){
	x=0; char c=getchar(); int f=1;
	while (!isdigit(c)) {if (c=='-') f=-1; c=getchar();}
	while (isdigit(c)) {x=x*10+c-'0'; c=getchar();} x*=f;
}
template<class T> T gcd(T a, T b){return !b?a:gcd(b,a%b);}
template<class T> inline void umin(T &x, T y){x=x<y?x:y;}
template<class T> inline void umax(T &x, T y){x=x>y?x:y;}
mt19937 R(chrono::system_clock().now().time_since_epoch().count());

const int N = 1e6+11;
int n,mn[N],hz[N],f[N],a[N],b[N],his[N];char s[N];
struct YZR{
	int a[N],his[N],n,pos,ans;
	
	int val[N<<2],lzy[N<<2];//max
	
	void pushdown(int k){
		lzy[k<<1]+=lzy[k];val[k<<1]+=lzy[k];
		lzy[k<<1|1]+=lzy[k];val[k<<1|1]+=lzy[k];
		lzy[k]=0;
	}
	void upd(int k){
		val[k]=max(val[k<<1],val[k<<1|1]);
	}
	void build(int k, int l, int r){
		if(l==r){val[k]=a[l];return;}
		int mid=(l+r)/2;
		build(k<<1,l,mid);build(k<<1|1,mid+1,r);
		upd(k);
	}
	void ini(int s, int mx, int *c){
		while(s<=mx)a[++n]=b[s],his[n]=c[s],s+=2;
		pos=1;while(pos<=n&&his[pos]==1e9)pos++;
		ans=pos<=n?a[pos]:1e9;
		build(1,1,n);
	//	cerr<<n<<" qwq "<<pos<<endl;
	//	rep(i,1,n)cerr<<a[i]<<' '<<his[i]<<' ';cerr<<endl;
	}
	void mdy(int k, int l, int r, int x, int L, int R){
		if(l==L&&r==R){lzy[k]+=x;val[k]+=x;return;}
		int mid=(L+R)/2;pushdown(k);
		if(r<=mid)mdy(k<<1,l,r,x,L,mid);
		else if(l>mid)mdy(k<<1|1,l,r,x,mid+1,R);
		else mdy(k<<1,l,mid,x,L,mid),mdy(k<<1|1,mid+1,r,x,mid+1,R);
		upd(k);
	}
	int find(int k, int lim, int l, int r){
		if(val[k]<lim)return n+1;
		if(l==r)return l;
		int mid=(l+r)/2;pushdown(k);
		if(val[k<<1]>=lim)return find(k<<1,lim,l,mid);
		return find(k<<1|1,lim,mid+1,r);
	}
	int qry(int k, int p, int l, int r){
		if(l==r)return val[k];
		int mid=(l+r)/2;pushdown(k);
		if(p<=mid)return qry(k<<1,p,l,mid);
		return qry(k<<1|1,p,mid+1,r);
	}
	void solve(int lim){//>=lim --; else ++
		int p=find(1,lim,1,n);
		if(p<=n)mdy(1,p,n,-1,1,n);
		if(1<=p-1)mdy(1,1,p-1,1,1,n);
	}
	void yzr(){if(pos<=n)umax(ans,qry(1,pos,1,n));}
	int qry(){
		return ans;
	}
}ji,ou;
void mdy(int l, int r, int x){
	ji.mdy(1,1,ji.n,x,1,ji.n);
	ou.mdy(1,1,ou.n,x,1,ou.n);
}
int main() {
	scanf("%s",s+1);n=strlen(s+1);
	mn[n+1]=1e9;
	//per(i,n,1)hz[i]=hz[i+1]+(s[i]!='0'?-1:1);
	rep(i,1,n)hz[i]=hz[i-1]+(s[i]=='0'?-1:1);
	per(i,n,0)mn[i]=min(mn[i+1],hz[i]);
	rep(i,0,n)f[i]=hz[i]-mn[i];
	rep(i,0,n)a[i]=0,b[i]=his[i]=i;
	rep(i,0,n)if(f[0]>i)his[i]=1e9;
	ji.ini(1,n,his);ou.ini(0,n,his);
//	rep(i,1,n)cerr<<i<<' '<<f[i]<<' '<<hz[i]<<' '<<mn[i]<<endl;
	rep(i,1,n){
		if(s[i]=='1'){mdy(0,n,1);}//rep(j,0,n)a[j]++,b[j]++,umax(his[j],b[j]);}
		else if(s[i]=='0'){mdy(0,n,-1);}//rep(j,0,n)a[j]--,b[j]--,umax(his[j],b[j]);}
		else{
			/*rep(j,0,n)
				if(a[j]-1-f[i]>=-j)a[j]--,b[j]--,umax(his[j],b[j]);
				else a[j]++,b[j]++,umax(his[j],b[j]);*/
			ji.solve(1+f[i]);ou.solve(1+f[i]);
		}
		ji.yzr();ou.yzr();
	}
	int res=min(ji.qry(),ou.qry());//1e9;rep(j,0,n)umin(res,his[j]);
	cout<<res;
	return 0;
}


Measured memory_bytes: 52352