#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ui long long
#define ull unsigned long long
#define db double
#define pii pair<int,int>
#define pli pair<ll,int>
#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define vi vector<int>
#define vii vector<vi>
#define lb lower_bound
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,b,a) for(int i=(b);i>=(a);--i)
#define rep0(i,a,b) for(int i=(a);i<(b);++i)
#define fore(i,a) for(int i=0;i<a.size();++i)
#define gc() getchar()
#define ls x<<1,l,m
#define rs x<<1|1,m+1,r
inline ll rd()
{
	ll x=0,w=1;char c=gc();while(!isdigit(c)&&c!='-')c=gc();
	if(c=='-')c=gc(),w=-1;while(isdigit(c))x=x*10+c-48,c=gc();return x*w;
}
const int N=1000005;
int n,ans=N,p[N],a[N];char s[N];
void chk(int mn)
{
	int c=0,mx=0;
	rep(i,1,n)
	{
		if(s[i]=='0')c++;else if(s[i]=='1')c--;
		else if(c-1+p[i+1]>=mn)c--;else c++;
		mx=max(mx,c);if(c<mn){mx=1e9;break;}
	}
	ans=min(ans,mx-mn);
}
int main()
{
	scanf("%s",s+1);n=strlen(s+1);
	per(i,n,1)p[i]=min(0,p[i+1]+(s[i]=='1'?-1:1));
	int c=0,mn=0;
	rep(i,1,n)
	{
		if(s[i]=='0')c++;else if(s[i]=='1')c--;else c++;
		mn=min(mn,c);
	}
	chk(mn);chk(mn-1);
	printf("%d\n",ans);return 0;
}
